50d8aca9af8819a4a68ce5e99c1bf16a535d3adb
[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                 online_analyze_enable)
643                 tblname = (RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel);
644
645 #if PG_VERSION_NUM >= 100000
646 #define parsetree pstmt
647 #endif
648
649         if (oldProcessUtilityHook)
650                 oldProcessUtilityHook(parsetree, queryString,
651 #if PG_VERSION_NUM >= 90300
652                                                           context, params,
653 #if PG_VERSION_NUM >= 100000
654                                                           queryEnv,
655 #endif
656 #else
657                                                           params, isTopLevel,
658 #endif
659                                                           dest, completionTag);
660         else
661                 standard_ProcessUtility(parsetree, queryString,
662 #if PG_VERSION_NUM >= 90300
663                                                                 context, params,
664 #if PG_VERSION_NUM >= 100000
665                                                                 queryEnv,
666 #endif
667 #else
668                                                                 params, isTopLevel,
669 #endif
670                                                                 dest, completionTag);
671
672 #if PG_VERSION_NUM >= 100000
673 #undef parsetree
674 #endif
675
676         if (tblname) {
677                 Oid     tblOid = RangeVarGetRelid(tblname, NoLock, true);
678
679                 makeAnalyze(tblOid, CMD_INSERT, -1);
680         }
681 }
682 #endif
683
684 static void
685 relstatsInit(void)
686 {
687         HASHCTL hash_ctl;
688         int             flags = 0;
689
690         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
691
692         hash_ctl.hash = oid_hash;
693         flags |= HASH_FUNCTION;
694
695         if (onlineAnalyzeMemoryContext)
696         {
697                 Assert(relstats != NULL);
698                 MemoryContextReset(onlineAnalyzeMemoryContext);
699         }
700         else
701         {
702                 Assert(relstats == NULL);
703                 onlineAnalyzeMemoryContext =
704                         AllocSetContextCreate(CacheMemoryContext,
705                                                                   "online_analyze storage context",
706 #if PG_VERSION_NUM < 90600
707                                                                   ALLOCSET_DEFAULT_MINSIZE,
708                                                                   ALLOCSET_DEFAULT_INITSIZE,
709                                                                   ALLOCSET_DEFAULT_MAXSIZE
710 #else
711                                                                   ALLOCSET_DEFAULT_SIZES
712 #endif
713                                                                  );
714         }
715
716         hash_ctl.hcxt = onlineAnalyzeMemoryContext;
717         flags |= HASH_CONTEXT;
718
719         hash_ctl.keysize = sizeof(Oid);
720
721         hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
722         flags |= HASH_ELEM;
723
724         relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
725 }
726
727 void _PG_init(void);
728 void
729 _PG_init(void)
730 {
731         relstatsInit();
732
733         oldExecutorEndHook = ExecutorEnd_hook;
734
735         ExecutorEnd_hook = onlineAnalyzeHooker;
736
737 #if PG_VERSION_NUM >= 90200
738         oldProcessUtilityHook = ProcessUtility_hook;
739
740         ProcessUtility_hook = onlineAnalyzeHookerUtility;
741 #endif
742
743
744         DefineCustomBoolVariable(
745                 "online_analyze.enable",
746                 "Enable on-line analyze",
747                 "Enables analyze of table directly after insert/update/delete/select into",
748                 &online_analyze_enable,
749 #if PG_VERSION_NUM >= 80400
750                 online_analyze_enable,
751 #endif
752                 PGC_USERSET,
753 #if PG_VERSION_NUM >= 80400
754                 GUC_NOT_IN_SAMPLE,
755 #if PG_VERSION_NUM >= 90100
756                 NULL,
757 #endif
758 #endif
759                 NULL,
760                 NULL
761         );
762
763         DefineCustomBoolVariable(
764                 "online_analyze.verbose",
765                 "Verbosity of on-line analyze",
766                 "Make ANALYZE VERBOSE after table's changes",
767                 &online_analyze_verbose,
768 #if PG_VERSION_NUM >= 80400
769                 online_analyze_verbose,
770 #endif
771                 PGC_USERSET,
772 #if PG_VERSION_NUM >= 80400
773                 GUC_NOT_IN_SAMPLE,
774 #if PG_VERSION_NUM >= 90100
775                 NULL,
776 #endif
777 #endif
778                 NULL,
779                 NULL
780         );
781
782         DefineCustomRealVariable(
783                 "online_analyze.scale_factor",
784                 "fraction of table size to start on-line analyze",
785                 "fraction of table size to start on-line analyze",
786                 &online_analyze_scale_factor,
787 #if PG_VERSION_NUM >= 80400
788                 online_analyze_scale_factor,
789 #endif
790                 0.0,
791                 1.0,
792                 PGC_USERSET,
793 #if PG_VERSION_NUM >= 80400
794                 GUC_NOT_IN_SAMPLE,
795 #if PG_VERSION_NUM >= 90100
796                 NULL,
797 #endif
798 #endif
799                 NULL,
800                 NULL
801         );
802
803         DefineCustomIntVariable(
804                 "online_analyze.threshold",
805                 "min number of row updates before on-line analyze",
806                 "min number of row updates before on-line analyze",
807                 &online_analyze_threshold,
808 #if PG_VERSION_NUM >= 80400
809                 online_analyze_threshold,
810 #endif
811                 0,
812                 0x7fffffff,
813                 PGC_USERSET,
814 #if PG_VERSION_NUM >= 80400
815                 GUC_NOT_IN_SAMPLE,
816 #if PG_VERSION_NUM >= 90100
817                 NULL,
818 #endif
819 #endif
820                 NULL,
821                 NULL
822         );
823
824         DefineCustomIntVariable(
825                 "online_analyze.capacity_threshold",
826                 "Max local cache table capacity",
827                 "Max local cache table capacity",
828                 &online_analyze_capacity_threshold,
829 #if PG_VERSION_NUM >= 80400
830                 online_analyze_capacity_threshold,
831 #endif
832                 0,
833                 0x7fffffff,
834                 PGC_USERSET,
835 #if PG_VERSION_NUM >= 80400
836                 GUC_NOT_IN_SAMPLE,
837 #if PG_VERSION_NUM >= 90100
838                 NULL,
839 #endif
840 #endif
841                 NULL,
842                 NULL
843         );
844
845         DefineCustomRealVariable(
846                 "online_analyze.min_interval",
847                 "minimum time interval between analyze call (in milliseconds)",
848                 "minimum time interval between analyze call (in milliseconds)",
849                 &online_analyze_min_interval,
850 #if PG_VERSION_NUM >= 80400
851                 online_analyze_min_interval,
852 #endif
853                 0.0,
854                 1e30,
855                 PGC_USERSET,
856 #if PG_VERSION_NUM >= 80400
857                 GUC_NOT_IN_SAMPLE,
858 #if PG_VERSION_NUM >= 90100
859                 NULL,
860 #endif
861 #endif
862                 NULL,
863                 NULL
864         );
865
866         DefineCustomEnumVariable(
867                 "online_analyze.table_type",
868                 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
869                 NULL,
870                 &online_analyze_table_type,
871 #if PG_VERSION_NUM >= 80400
872                 online_analyze_table_type,
873 #endif
874                 online_analyze_table_type_options,
875                 PGC_USERSET,
876 #if PG_VERSION_NUM >= 80400
877                 GUC_NOT_IN_SAMPLE,
878 #if PG_VERSION_NUM >= 90100
879                 NULL,
880 #endif
881 #endif
882                 NULL,
883                 NULL
884         );
885
886         DefineCustomStringVariable(
887                 "online_analyze.exclude_tables",
888                 "List of tables which will not online analyze",
889                 NULL,
890                 &excludeTables.tableStr,
891 #if PG_VERSION_NUM >= 80400
892                 "",
893 #endif
894                 PGC_USERSET,
895                 0,
896 #if PG_VERSION_NUM >= 90100
897                 excludeTablesCheck,
898                 excludeTablesAssign,
899 #else
900                 excludeTablesAssign,
901 #endif
902                 excludeTablesShow
903         );
904
905         DefineCustomStringVariable(
906                 "online_analyze.include_tables",
907                 "List of tables which will online analyze",
908                 NULL,
909                 &includeTables.tableStr,
910 #if PG_VERSION_NUM >= 80400
911                 "",
912 #endif
913                 PGC_USERSET,
914                 0,
915 #if PG_VERSION_NUM >= 90100
916                 includeTablesCheck,
917                 includeTablesAssign,
918 #else
919                 includeTablesAssign,
920 #endif
921                 includeTablesShow
922         );
923 }
924
925 void _PG_fini(void);
926 void
927 _PG_fini(void)
928 {
929         ExecutorEnd_hook = oldExecutorEndHook;
930 #if PG_VERSION_NUM >= 90200
931         ProcessUtility_hook = oldProcessUtilityHook;
932 #endif
933
934         if (excludeTables.tables)
935                 free(excludeTables.tables);
936         if (includeTables.tables)
937                 free(includeTables.tables);
938
939         excludeTables.tables = includeTables.tables = NULL;
940         excludeTables.nTables = includeTables.nTables = 0;
941 }