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