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