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