dba4226e9c784aafb698acaaced999c3dc8561f8
[online_analyze.git] / online_analyze.c
1 /*
2  * Copyright (c) 2011 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *              notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *              notice, this list of conditions and the following disclaimer in the
12  *              documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *              may be used to endorse or promote products derived from this software
15  *              without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "postgres.h"
31
32 #include "pgstat.h"
33 #include "catalog/namespace.h"
34 #include "commands/vacuum.h"
35 #include "executor/executor.h"
36 #include "nodes/nodes.h"
37 #include "nodes/parsenodes.h"
38 #include "storage/bufmgr.h"
39 #include "utils/builtins.h"
40 #include "utils/hsearch.h"
41 #include "utils/memutils.h"
42 #include "utils/lsyscache.h"
43 #include "utils/guc.h"
44 #if PG_VERSION_NUM >= 90200
45 #include "catalog/pg_class.h"
46 #include "nodes/primnodes.h"
47 #include "tcop/utility.h"
48 #include "utils/rel.h"
49 #include "utils/relcache.h"
50 #include "utils/timestamp.h"
51 #if PG_VERSION_NUM >= 90500
52 #include "nodes/makefuncs.h"
53 #if PG_VERSION_NUM >= 100000
54 #include "utils/varlena.h"
55 #include "utils/regproc.h"
56 #endif
57 #endif
58 #endif
59
60 #ifdef PG_MODULE_MAGIC
61 PG_MODULE_MAGIC;
62 #endif
63
64 static bool online_analyze_enable = true;
65 static bool online_analyze_verbose = true;
66 static double online_analyze_scale_factor = 0.1;
67 static int online_analyze_threshold = 50;
68 static int online_analyze_capacity_threshold = 100000;
69 static double online_analyze_min_interval = 10000;
70 static int online_analyze_lower_limit = 0;
71
72 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
73 #if PG_VERSION_NUM >= 90200
74 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
75 #endif
76
77 typedef enum CmdKind
78 {
79         CK_SELECT = CMD_SELECT,
80         CK_UPDATE = CMD_UPDATE,
81         CK_INSERT = CMD_INSERT,
82         CK_DELETE = CMD_DELETE,
83         CK_TRUNCATE,
84         CK_CREATE
85 } CmdKind;
86
87 typedef enum
88 {
89         OATT_ALL                = 0x03,
90         OATT_PERSISTENT = 0x01,
91         OATT_TEMPORARY  = 0x02,
92         OATT_NONE               = 0x00
93 } OnlineAnalyzeTableType;
94
95 static const struct config_enum_entry online_analyze_table_type_options[] =
96 {
97         {"all", OATT_ALL, false},
98         {"persistent", OATT_PERSISTENT, false},
99         {"temporary", OATT_TEMPORARY, false},
100         {"none", OATT_NONE, false},
101         {NULL, 0, false},
102 };
103
104 static int online_analyze_table_type = (int)OATT_ALL;
105
106 typedef struct TableList {
107         int             nTables;
108         Oid             *tables;
109         char    *tableStr;
110 } TableList;
111
112 static TableList excludeTables = {0, NULL, NULL};
113 static TableList includeTables = {0, NULL, NULL};
114
115 typedef struct OnlineAnalyzeTableStat {
116         Oid                             tableid;
117         bool                    rereadStat;
118         PgStat_Counter  n_tuples;
119         PgStat_Counter  changes_since_analyze;
120         TimestampTz             autovac_analyze_timestamp;
121         TimestampTz             analyze_timestamp;
122 } OnlineAnalyzeTableStat;
123
124 static  MemoryContext   onlineAnalyzeMemoryContext = NULL;
125 static  HTAB    *relstats = NULL;
126
127 static void relstatsInit(void);
128
129 #if PG_VERSION_NUM < 100000
130 static int
131 oid_cmp(const void *a, const void *b)
132 {
133         if (*(Oid*)a == *(Oid*)b)
134                 return 0;
135         return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
136 }
137 #endif
138
139 static const char *
140 tableListAssign(const char * newval, bool doit, TableList *tbl)
141 {
142         char            *rawname;
143         List            *namelist;
144         ListCell        *l;
145         Oid                     *newOids = NULL;
146         int                     nOids = 0,
147                                 i = 0;
148
149         rawname = pstrdup(newval);
150
151         if (!SplitIdentifierString(rawname, ',', &namelist))
152                 goto cleanup;
153
154         if (doit)
155         {
156                 nOids = list_length(namelist);
157                 newOids = malloc(sizeof(Oid) * (nOids+1));
158                 if (!newOids)
159                         elog(ERROR,"could not allocate %d bytes",
160                                  (int)(sizeof(Oid) * (nOids+1)));
161         }
162
163         foreach(l, namelist)
164         {
165                 char    *curname = (char *) lfirst(l);
166 #if PG_VERSION_NUM >= 90200
167                 Oid             relOid = RangeVarGetRelid(makeRangeVarFromNameList(
168                                                         stringToQualifiedNameList(curname)), NoLock, true);
169 #else
170                 Oid             relOid = RangeVarGetRelid(makeRangeVarFromNameList(
171                                                         stringToQualifiedNameList(curname)), true);
172 #endif
173
174                 if (relOid == InvalidOid)
175                 {
176 #if PG_VERSION_NUM >= 90100
177                         if (doit == false)
178 #endif
179                         elog(WARNING,"'%s' does not exist", curname);
180                         continue;
181                 }
182                 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
183                 {
184 #if PG_VERSION_NUM >= 90100
185                         if (doit == false)
186 #endif
187                                 elog(WARNING,"'%s' is not an table", curname);
188                         continue;
189                 }
190                 else if (doit)
191                 {
192                         newOids[i++] = relOid;
193                 }
194         }
195
196         if (doit)
197         {
198                 tbl->nTables = i;
199                 if (tbl->tables)
200                         free(tbl->tables);
201                 tbl->tables = newOids;
202                 if (tbl->nTables > 1)
203                         qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
204         }
205
206         pfree(rawname);
207         list_free(namelist);
208
209         return newval;
210
211 cleanup:
212         if (newOids)
213                 free(newOids);
214         pfree(rawname);
215         list_free(namelist);
216         return NULL;
217 }
218
219 #if PG_VERSION_NUM >= 90100
220 static bool
221 excludeTablesCheck(char **newval, void **extra, GucSource source)
222 {
223         char *val;
224
225         val = (char*)tableListAssign(*newval, false, &excludeTables);
226
227         if (val)
228         {
229                 *newval = val;
230                 return true;
231         }
232
233         return false;
234 }
235
236 static void
237 excludeTablesAssign(const char *newval, void *extra)
238 {
239         tableListAssign(newval, true, &excludeTables);
240 }
241
242 static bool
243 includeTablesCheck(char **newval, void **extra, GucSource source)
244 {
245         char *val;
246
247         val = (char*)tableListAssign(*newval, false, &includeTables);
248
249         if (val)
250         {
251                 *newval = val;
252                 return true;
253         }
254
255         return false;
256 }
257
258 static void
259 includeTablesAssign(const char *newval, void *extra)
260 {
261         tableListAssign(newval, true, &excludeTables);
262 }
263
264 #else /* PG_VERSION_NUM < 90100 */
265
266 static const char *
267 excludeTablesAssign(const char * newval, bool doit, GucSource source)
268 {
269         return tableListAssign(newval, doit, &excludeTables);
270 }
271
272 static const char *
273 includeTablesAssign(const char * newval, bool doit, GucSource source)
274 {
275         return tableListAssign(newval, doit, &includeTables);
276 }
277
278 #endif
279
280 static const char*
281 tableListShow(TableList *tbl)
282 {
283         char    *val, *ptr;
284         int             i,
285                         len;
286
287         len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
288         ptr = val = palloc(len);
289         *ptr ='\0';
290         for(i=0; i<tbl->nTables; i++)
291         {
292                 char    *relname = get_rel_name(tbl->tables[i]);
293                 Oid             nspOid = get_rel_namespace(tbl->tables[i]);
294                 char    *nspname = get_namespace_name(nspOid);
295
296                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
297                         continue;
298
299                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
300                                                                                                         (i==0) ? "" : ", ",
301                                                                                                         nspname, relname);
302         }
303
304         return val;
305 }
306
307 static const char*
308 excludeTablesShow(void)
309 {
310         return tableListShow(&excludeTables);
311 }
312
313 static const char*
314 includeTablesShow(void)
315 {
316         return tableListShow(&includeTables);
317 }
318
319 static bool
320 matchOid(TableList *tbl, Oid oid)
321 {
322         Oid     *StopLow = tbl->tables,
323                 *StopHigh = tbl->tables + tbl->nTables,
324                 *StopMiddle;
325
326         /* Loop invariant: StopLow <= val < StopHigh */
327         while (StopLow < StopHigh)
328         {
329                 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
330
331                 if (*StopMiddle == oid)
332                         return true;
333                 else  if (*StopMiddle < oid)
334                         StopLow = StopMiddle + 1;
335                 else
336                         StopHigh = StopMiddle;
337         }
338
339         return false;
340 }
341
342 #if PG_VERSION_NUM >= 90500
343 static RangeVar*
344 makeRangeVarFromOid(Oid relOid)
345 {
346         return makeRangeVar(
347                                 get_namespace_name(get_rel_namespace(relOid)),
348                                 get_rel_name(relOid),
349                                 -1
350                         );
351
352 }
353 #endif
354
355 static void
356 makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
357 {
358         TimestampTz                             now = GetCurrentTimestamp();
359         Relation                                rel;
360         OnlineAnalyzeTableType  reltype;
361         bool                                    found = false,
362                                                         newTable = false;
363         OnlineAnalyzeTableStat  *rstat,
364                                                         dummyrstat;
365         PgStat_StatTabEntry             *tabentry = NULL;
366
367         if (relOid == InvalidOid)
368                 return;
369
370         if (naffected == 0)
371                 /* return if there is no changes */
372                 return;
373         else if (naffected < 0)
374                 /* number if affected rows is unknown */
375                 naffected = 0;
376
377         rel = RelationIdGetRelation(relOid);
378         if (rel->rd_rel->relkind != RELKIND_RELATION)
379         {
380                 RelationClose(rel);
381                 return;
382         }
383
384         reltype =
385 #if PG_VERSION_NUM >= 90100
386                 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
387 #else
388                 (rel->rd_istemp || rel->rd_islocaltemp)
389 #endif
390                         ? OATT_TEMPORARY : OATT_PERSISTENT;
391
392         RelationClose(rel);
393
394         /*
395          * includeTables overwrites excludeTables
396          */
397         switch(online_analyze_table_type)
398         {
399                 case OATT_ALL:
400                         if (get_rel_relkind(relOid) != RELKIND_RELATION ||
401                                 (matchOid(&excludeTables, relOid) == true &&
402                                 matchOid(&includeTables, relOid) == false))
403                                 return;
404                         break;
405                 case OATT_NONE:
406                         if (get_rel_relkind(relOid) != RELKIND_RELATION ||
407                                 matchOid(&includeTables, relOid) == false)
408                                 return;
409                         break;
410                 case OATT_TEMPORARY:
411                 case OATT_PERSISTENT:
412                 default:
413                         /*
414                          * skip analyze if relation's type doesn't not match
415                          * online_analyze_table_type
416                          */
417                         if ((online_analyze_table_type & reltype) == 0 ||
418                                 matchOid(&excludeTables, relOid) == true)
419                         {
420                                 if (matchOid(&includeTables, relOid) == false)
421                                         return;
422                         }
423                         break;
424         }
425
426         /*
427          * Do not store data about persistent table in local memory because we
428          * could not track changes of them: they could be changed by another
429          * backends. So always get a pgstat table entry.
430          */
431         if (reltype == OATT_TEMPORARY)
432                 rstat = hash_search(relstats, &relOid, HASH_ENTER, &found);
433         else
434                 rstat = &dummyrstat; /* found == false for following if */
435
436         if (!found)
437         {
438                 MemSet(rstat, 0, sizeof(*rstat));
439                 rstat->tableid = relOid;
440                 newTable = true;
441         }
442
443         Assert(rstat->tableid == relOid);
444
445         elog(NOTICE,"makeAnalyze op:%d %u", operation, naffected);
446
447         if (operation != CK_TRUNCATE &&
448                 (found == false || rstat->rereadStat == true))
449         {
450
451                 rstat->rereadStat = false;
452
453                 tabentry = pgstat_fetch_stat_tabentry(relOid);
454
455                 if (tabentry)
456                 {
457                         rstat->n_tuples = tabentry->n_dead_tuples + tabentry->n_live_tuples;
458                         rstat->changes_since_analyze =
459 #if PG_VERSION_NUM >= 90000
460                                 tabentry->changes_since_analyze;
461 #else
462                                 tabentry->n_live_tuples + tabentry->n_dead_tuples -
463                                         tabentry->last_anl_tuples;
464 #endif
465                         rstat->autovac_analyze_timestamp =
466                                 tabentry->autovac_analyze_timestamp;
467                         rstat->analyze_timestamp = tabentry->analyze_timestamp;
468                 }
469         }
470
471         if (newTable ||
472                 /* force analyze from after truncate */
473                 operation == CK_TRUNCATE || (
474                 /* do not analyze too often, if both stamps are exceeded the go */
475                 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
476                 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
477                 /* do not analyze too small tables */
478                 rstat->n_tuples + rstat->changes_since_analyze + naffected > online_analyze_lower_limit &&
479                 /* be in sync with relation_needs_vacanalyze */
480                 ((double)(rstat->changes_since_analyze + naffected)) >=
481                          online_analyze_scale_factor * ((double)rstat->n_tuples) +
482                          (double)online_analyze_threshold))
483         {
484 #if PG_VERSION_NUM < 90500
485                 VacuumStmt                              vacstmt;
486 #else
487                 VacuumParams                    vacstmt;
488 #endif
489                 TimestampTz                             startStamp, endStamp;
490
491
492                 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
493
494                 memset(&vacstmt, 0, sizeof(vacstmt));
495
496                 vacstmt.freeze_min_age = -1;
497                 vacstmt.freeze_table_age = -1; /* ??? */
498
499 #if PG_VERSION_NUM < 90500
500                 vacstmt.type = T_VacuumStmt;
501                 vacstmt.relation = NULL;
502                 vacstmt.va_cols = NIL;
503 #if PG_VERSION_NUM >= 90000
504                 vacstmt.options = VACOPT_ANALYZE;
505                 if (online_analyze_verbose)
506                         vacstmt.options |= VACOPT_VERBOSE;
507 #else
508                 vacstmt.vacuum = vacstmt.full = false;
509                 vacstmt.analyze = true;
510                 vacstmt.verbose = online_analyze_verbose;
511 #endif
512 #else
513                 vacstmt.multixact_freeze_min_age = -1;
514                 vacstmt.multixact_freeze_table_age = -1;
515                 vacstmt.log_min_duration = -1;
516 #endif
517
518                 if (online_analyze_verbose)
519                         startStamp = GetCurrentTimestamp();
520
521                 analyze_rel(relOid,
522 #if PG_VERSION_NUM < 90500
523                         &vacstmt
524 #if PG_VERSION_NUM >= 90018
525                         , true
526 #endif
527                         , GetAccessStrategy(BAS_VACUUM)
528 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
529                         , true
530 #endif
531 #else
532                         makeRangeVarFromOid(relOid),
533                         VACOPT_ANALYZE | ((online_analyze_verbose) ? VACOPT_VERBOSE : 0),
534                         &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
535 #endif
536                 );
537
538                 if (online_analyze_verbose)
539                 {
540                         long    secs;
541                         int             microsecs;
542
543                         endStamp = GetCurrentTimestamp();
544                         TimestampDifference(startStamp, endStamp, &secs, &microsecs);
545                         elog(INFO, "analyze \"%s\" took %.02f seconds",
546                                 get_rel_name(relOid),
547                                 ((double)secs) + ((double)microsecs)/1.0e6);
548                 }
549
550                 rstat->autovac_analyze_timestamp = now;
551                 rstat->changes_since_analyze = 0;
552
553                 switch(operation)
554                 {
555                         case CK_INSERT:
556                                 rstat->n_tuples += naffected;
557                                 rstat->rereadStat = false;
558                                 break;
559                         case CK_UPDATE:
560                                 rstat->n_tuples += naffected;
561                                 rstat->rereadStat = true;
562                                 break;
563                         case CK_DELETE:
564                                 rstat->rereadStat = true;
565                                 break;
566                         case CK_TRUNCATE:
567                                 rstat->n_tuples = 0;
568                                 rstat->rereadStat = false;
569                                 break;
570                         default:
571                                 break;
572                 }
573
574                 /* update last analyze timestamp in local memory of backend */
575                 if (tabentry)
576                 {
577                         tabentry->analyze_timestamp = now;
578                         tabentry->changes_since_analyze = 0;
579                 }
580 #if 0
581                 /* force reload stat for new table */
582                 if (newTable)
583                         pgstat_clear_snapshot();
584 #endif
585         }
586         else
587         {
588 #if PG_VERSION_NUM >= 90000
589                 if (tabentry)
590                         tabentry->changes_since_analyze += naffected;
591 #endif
592                 switch(operation)
593                 {
594                         case CK_INSERT:
595                                 rstat->changes_since_analyze += naffected;
596                                 rstat->n_tuples += naffected;
597                                 break;
598                         case CK_UPDATE:
599                                 rstat->changes_since_analyze += 2 * naffected;
600                                 rstat->n_tuples += naffected;
601                         case CK_DELETE:
602                                 rstat->changes_since_analyze += naffected;
603                                 break;
604                         case CK_TRUNCATE:
605                                 rstat->changes_since_analyze = rstat->n_tuples;
606                                 rstat->n_tuples = 0;
607                                 break;
608                         default:
609                                 break;
610                 }
611         }
612
613         /* Reset local cache if we are over limit */
614         if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
615                 relstatsInit();
616 }
617
618 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
619 void
620 onlineAnalyzeHooker(QueryDesc *queryDesc)
621 {
622         int64   naffected = -1;
623
624         if (queryDesc->estate)
625                 naffected = queryDesc->estate->es_processed;
626
627         if (online_analyze_enable && queryDesc->plannedstmt &&
628                         (queryDesc->operation == CMD_INSERT ||
629                          queryDesc->operation == CMD_UPDATE ||
630                          queryDesc->operation == CMD_DELETE
631 #if PG_VERSION_NUM < 90200
632                          || (queryDesc->operation == CMD_SELECT &&
633                                  queryDesc->plannedstmt->intoClause)
634 #endif
635                          ))
636         {
637 #if PG_VERSION_NUM < 90200
638                 if (queryDesc->operation == CMD_SELECT)
639                 {
640                         Oid     relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
641
642                         makeAnalyze(relOid, queryDesc->operation, naffected);
643                 }
644                 else
645 #endif
646                 if (queryDesc->plannedstmt->resultRelations &&
647                                  queryDesc->plannedstmt->rtable)
648                 {
649                         ListCell        *l;
650
651                         foreach(l, queryDesc->plannedstmt->resultRelations)
652                         {
653                                 int                             n = lfirst_int(l);
654                                 RangeTblEntry   *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
655
656                                 if (rte->rtekind == RTE_RELATION)
657                                         makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
658                         }
659                 }
660         }
661
662         if (oldExecutorEndHook)
663                 oldExecutorEndHook(queryDesc);
664         else
665                 standard_ExecutorEnd(queryDesc);
666 }
667
668 #if PG_VERSION_NUM >= 90200
669 static void
670 onlineAnalyzeHookerUtility(
671 #if PG_VERSION_NUM >= 100000
672                                                    PlannedStmt *pstmt,
673 #else
674                                                    Node *parsetree,
675 #endif
676                                                    const char *queryString,
677 #if PG_VERSION_NUM >= 90300
678                                                         ProcessUtilityContext context, ParamListInfo params,
679 #if PG_VERSION_NUM >= 100000
680                                                         QueryEnvironment *queryEnv,
681 #endif
682 #else
683                                                         ParamListInfo params, bool isTopLevel,
684 #endif
685                                                         DestReceiver *dest, char *completionTag) {
686         List            *tblnames = NIL;
687         CmdKind         op = CK_INSERT;
688 #if PG_VERSION_NUM >= 100000
689         Node            *parsetree = NULL;
690
691         if (pstmt->commandType == CMD_UTILITY)
692                 parsetree = pstmt->utilityStmt;
693 #endif
694
695         if (parsetree && online_analyze_enable)
696         {
697                 if (IsA(parsetree, CreateTableAsStmt) &&
698                         ((CreateTableAsStmt*)parsetree)->into)
699                 {
700                         tblnames =
701                                 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
702                         op = CK_CREATE;
703                 }
704                 else if (IsA(parsetree, TruncateStmt))
705                 {
706                         tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
707                         op = CK_TRUNCATE;
708                 }
709         }
710
711 #if PG_VERSION_NUM >= 100000
712 #define parsetree pstmt
713 #endif
714
715         if (oldProcessUtilityHook)
716                 oldProcessUtilityHook(parsetree, queryString,
717 #if PG_VERSION_NUM >= 90300
718                                                           context, params,
719 #if PG_VERSION_NUM >= 100000
720                                                           queryEnv,
721 #endif
722 #else
723                                                           params, isTopLevel,
724 #endif
725                                                           dest, completionTag);
726         else
727                 standard_ProcessUtility(parsetree, queryString,
728 #if PG_VERSION_NUM >= 90300
729                                                                 context, params,
730 #if PG_VERSION_NUM >= 100000
731                                                                 queryEnv,
732 #endif
733 #else
734                                                                 params, isTopLevel,
735 #endif
736                                                                 dest, completionTag);
737
738 #if PG_VERSION_NUM >= 100000
739 #undef parsetree
740 #endif
741
742         if (tblnames) {
743                 ListCell        *l;
744
745                 foreach(l, tblnames)
746                 {
747                         RangeVar        *tblname = (RangeVar*)lfirst(l);
748                         Oid     tblOid = RangeVarGetRelid(tblname, NoLock, true);
749
750                         makeAnalyze(tblOid, op, -1);
751                 }
752         }
753 }
754 #endif
755
756 static void
757 relstatsInit(void)
758 {
759         HASHCTL hash_ctl;
760         int             flags = 0;
761
762         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
763
764         hash_ctl.hash = oid_hash;
765         flags |= HASH_FUNCTION;
766
767         if (onlineAnalyzeMemoryContext)
768         {
769                 Assert(relstats != NULL);
770                 MemoryContextReset(onlineAnalyzeMemoryContext);
771         }
772         else
773         {
774                 Assert(relstats == NULL);
775                 onlineAnalyzeMemoryContext =
776                         AllocSetContextCreate(CacheMemoryContext,
777                                                                   "online_analyze storage context",
778 #if PG_VERSION_NUM < 90600
779                                                                   ALLOCSET_DEFAULT_MINSIZE,
780                                                                   ALLOCSET_DEFAULT_INITSIZE,
781                                                                   ALLOCSET_DEFAULT_MAXSIZE
782 #else
783                                                                   ALLOCSET_DEFAULT_SIZES
784 #endif
785                                                                  );
786         }
787
788         hash_ctl.hcxt = onlineAnalyzeMemoryContext;
789         flags |= HASH_CONTEXT;
790
791         hash_ctl.keysize = sizeof(Oid);
792
793         hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
794         flags |= HASH_ELEM;
795
796         relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
797 }
798
799 void _PG_init(void);
800 void
801 _PG_init(void)
802 {
803         relstatsInit();
804
805         oldExecutorEndHook = ExecutorEnd_hook;
806
807         ExecutorEnd_hook = onlineAnalyzeHooker;
808
809 #if PG_VERSION_NUM >= 90200
810         oldProcessUtilityHook = ProcessUtility_hook;
811
812         ProcessUtility_hook = onlineAnalyzeHookerUtility;
813 #endif
814
815
816         DefineCustomBoolVariable(
817                 "online_analyze.enable",
818                 "Enable on-line analyze",
819                 "Enables analyze of table directly after insert/update/delete/select into",
820                 &online_analyze_enable,
821 #if PG_VERSION_NUM >= 80400
822                 online_analyze_enable,
823 #endif
824                 PGC_USERSET,
825 #if PG_VERSION_NUM >= 80400
826                 GUC_NOT_IN_SAMPLE,
827 #if PG_VERSION_NUM >= 90100
828                 NULL,
829 #endif
830 #endif
831                 NULL,
832                 NULL
833         );
834
835         DefineCustomBoolVariable(
836                 "online_analyze.verbose",
837                 "Verbosity of on-line analyze",
838                 "Make ANALYZE VERBOSE after table's changes",
839                 &online_analyze_verbose,
840 #if PG_VERSION_NUM >= 80400
841                 online_analyze_verbose,
842 #endif
843                 PGC_USERSET,
844 #if PG_VERSION_NUM >= 80400
845                 GUC_NOT_IN_SAMPLE,
846 #if PG_VERSION_NUM >= 90100
847                 NULL,
848 #endif
849 #endif
850                 NULL,
851                 NULL
852         );
853
854         DefineCustomRealVariable(
855                 "online_analyze.scale_factor",
856                 "fraction of table size to start on-line analyze",
857                 "fraction of table size to start on-line analyze",
858                 &online_analyze_scale_factor,
859 #if PG_VERSION_NUM >= 80400
860                 online_analyze_scale_factor,
861 #endif
862                 0.0,
863                 1.0,
864                 PGC_USERSET,
865 #if PG_VERSION_NUM >= 80400
866                 GUC_NOT_IN_SAMPLE,
867 #if PG_VERSION_NUM >= 90100
868                 NULL,
869 #endif
870 #endif
871                 NULL,
872                 NULL
873         );
874
875         DefineCustomIntVariable(
876                 "online_analyze.threshold",
877                 "min number of row updates before on-line analyze",
878                 "min number of row updates before on-line analyze",
879                 &online_analyze_threshold,
880 #if PG_VERSION_NUM >= 80400
881                 online_analyze_threshold,
882 #endif
883                 0,
884                 0x7fffffff,
885                 PGC_USERSET,
886 #if PG_VERSION_NUM >= 80400
887                 GUC_NOT_IN_SAMPLE,
888 #if PG_VERSION_NUM >= 90100
889                 NULL,
890 #endif
891 #endif
892                 NULL,
893                 NULL
894         );
895
896         DefineCustomIntVariable(
897                 "online_analyze.capacity_threshold",
898                 "Max local cache table capacity",
899                 "Max local cache table capacity",
900                 &online_analyze_capacity_threshold,
901 #if PG_VERSION_NUM >= 80400
902                 online_analyze_capacity_threshold,
903 #endif
904                 0,
905                 0x7fffffff,
906                 PGC_USERSET,
907 #if PG_VERSION_NUM >= 80400
908                 GUC_NOT_IN_SAMPLE,
909 #if PG_VERSION_NUM >= 90100
910                 NULL,
911 #endif
912 #endif
913                 NULL,
914                 NULL
915         );
916
917         DefineCustomRealVariable(
918                 "online_analyze.min_interval",
919                 "minimum time interval between analyze call (in milliseconds)",
920                 "minimum time interval between analyze call (in milliseconds)",
921                 &online_analyze_min_interval,
922 #if PG_VERSION_NUM >= 80400
923                 online_analyze_min_interval,
924 #endif
925                 0.0,
926                 1e30,
927                 PGC_USERSET,
928 #if PG_VERSION_NUM >= 80400
929                 GUC_NOT_IN_SAMPLE,
930 #if PG_VERSION_NUM >= 90100
931                 NULL,
932 #endif
933 #endif
934                 NULL,
935                 NULL
936         );
937
938         DefineCustomEnumVariable(
939                 "online_analyze.table_type",
940                 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
941                 NULL,
942                 &online_analyze_table_type,
943 #if PG_VERSION_NUM >= 80400
944                 online_analyze_table_type,
945 #endif
946                 online_analyze_table_type_options,
947                 PGC_USERSET,
948 #if PG_VERSION_NUM >= 80400
949                 GUC_NOT_IN_SAMPLE,
950 #if PG_VERSION_NUM >= 90100
951                 NULL,
952 #endif
953 #endif
954                 NULL,
955                 NULL
956         );
957
958         DefineCustomStringVariable(
959                 "online_analyze.exclude_tables",
960                 "List of tables which will not online analyze",
961                 NULL,
962                 &excludeTables.tableStr,
963 #if PG_VERSION_NUM >= 80400
964                 "",
965 #endif
966                 PGC_USERSET,
967                 0,
968 #if PG_VERSION_NUM >= 90100
969                 excludeTablesCheck,
970                 excludeTablesAssign,
971 #else
972                 excludeTablesAssign,
973 #endif
974                 excludeTablesShow
975         );
976
977         DefineCustomStringVariable(
978                 "online_analyze.include_tables",
979                 "List of tables which will online analyze",
980                 NULL,
981                 &includeTables.tableStr,
982 #if PG_VERSION_NUM >= 80400
983                 "",
984 #endif
985                 PGC_USERSET,
986                 0,
987 #if PG_VERSION_NUM >= 90100
988                 includeTablesCheck,
989                 includeTablesAssign,
990 #else
991                 includeTablesAssign,
992 #endif
993                 includeTablesShow
994         );
995
996         DefineCustomIntVariable(
997                 "online_analyze.lower_limit",
998                 "min number of rows in table to analyze",
999                 "min number of rows in table to analyze",
1000                 &online_analyze_lower_limit,
1001 #if PG_VERSION_NUM >= 80400
1002                 online_analyze_lower_limit,
1003 #endif
1004                 0,
1005                 0x7fffffff,
1006                 PGC_USERSET,
1007 #if PG_VERSION_NUM >= 80400
1008                 GUC_NOT_IN_SAMPLE,
1009 #if PG_VERSION_NUM >= 90100
1010                 NULL,
1011 #endif
1012 #endif
1013                 NULL,
1014                 NULL
1015         );
1016
1017 }
1018
1019 void _PG_fini(void);
1020 void
1021 _PG_fini(void)
1022 {
1023         ExecutorEnd_hook = oldExecutorEndHook;
1024 #if PG_VERSION_NUM >= 90200
1025         ProcessUtility_hook = oldProcessUtilityHook;
1026 #endif
1027
1028         if (excludeTables.tables)
1029                 free(excludeTables.tables);
1030         if (includeTables.tables)
1031                 free(includeTables.tables);
1032
1033         excludeTables.tables = includeTables.tables = NULL;
1034         excludeTables.nTables = includeTables.nTables = 0;
1035 }