2 * Copyright (c) 2011 Teodor Sigaev <teodor@sigaev.ru>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include "access/transam.h"
34 #include "access/xact.h"
35 #include "catalog/namespace.h"
36 #include "commands/vacuum.h"
37 #include "executor/executor.h"
38 #include "nodes/nodes.h"
39 #include "nodes/parsenodes.h"
40 #include "storage/bufmgr.h"
41 #include "utils/builtins.h"
42 #include "utils/hsearch.h"
43 #include "utils/memutils.h"
44 #include "utils/lsyscache.h"
45 #include "utils/guc.h"
46 #if PG_VERSION_NUM >= 90200
47 #include "catalog/pg_class.h"
48 #include "nodes/primnodes.h"
49 #include "tcop/utility.h"
50 #include "utils/rel.h"
51 #include "utils/relcache.h"
52 #include "utils/timestamp.h"
53 #if PG_VERSION_NUM >= 90500
54 #include "nodes/makefuncs.h"
55 #if PG_VERSION_NUM >= 100000
56 #include "utils/varlena.h"
57 #include "utils/regproc.h"
58 #if PG_VERSION_NUM >= 130000
59 #include "common/hashfn.h"
65 #ifdef PG_MODULE_MAGIC
69 static bool online_analyze_enable = true;
70 static bool online_analyze_local_tracking = false;
71 static bool online_analyze_verbose = true;
72 static double online_analyze_scale_factor = 0.1;
73 static int online_analyze_threshold = 50;
74 static int online_analyze_capacity_threshold = 100000;
75 static double online_analyze_min_interval = 10000;
76 static int online_analyze_lower_limit = 0;
78 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
79 #if PG_VERSION_NUM >= 90200
80 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
83 #if PG_VERSION_NUM >= 120000
84 #define VACOPT_NOWAIT VACOPT_SKIP_LOCKED
89 CK_SELECT = CMD_SELECT,
90 CK_UPDATE = CMD_UPDATE,
91 CK_INSERT = CMD_INSERT,
92 CK_DELETE = CMD_DELETE,
104 OATT_PERSISTENT = 0x01,
105 OATT_TEMPORARY = 0x02,
107 } OnlineAnalyzeTableType;
109 static const struct config_enum_entry online_analyze_table_type_options[] =
111 {"all", OATT_ALL, false},
112 {"persistent", OATT_PERSISTENT, false},
113 {"temporary", OATT_TEMPORARY, false},
114 {"none", OATT_NONE, false},
118 static int online_analyze_table_type = (int)OATT_ALL;
120 typedef struct TableList {
126 static TableList excludeTables = {0, NULL, NULL};
127 static TableList includeTables = {0, NULL, NULL};
129 typedef struct OnlineAnalyzeTableStat {
132 PgStat_Counter n_tuples;
133 PgStat_Counter changes_since_analyze;
134 TimestampTz autovac_analyze_timestamp;
135 TimestampTz analyze_timestamp;
136 } OnlineAnalyzeTableStat;
138 static MemoryContext onlineAnalyzeMemoryContext = NULL;
139 static HTAB *relstats = NULL;
141 static void relstatsInit(void);
143 #if PG_VERSION_NUM < 100000
145 oid_cmp(const void *a, const void *b)
147 if (*(Oid*)a == *(Oid*)b)
149 return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
154 tableListAssign(const char * newval, bool doit, TableList *tbl)
163 rawname = pstrdup(newval);
165 if (!SplitIdentifierString(rawname, ',', &namelist))
170 nOids = list_length(namelist);
171 newOids = malloc(sizeof(Oid) * (nOids+1));
173 elog(ERROR,"could not allocate %d bytes",
174 (int)(sizeof(Oid) * (nOids+1)));
179 char *curname = (char *) lfirst(l);
180 #if PG_VERSION_NUM >= 90200
181 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
182 stringToQualifiedNameList(curname)), NoLock, true);
184 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
185 stringToQualifiedNameList(curname)), true);
188 if (relOid == InvalidOid)
190 #if PG_VERSION_NUM >= 90100
193 elog(WARNING,"'%s' does not exist", curname);
196 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
198 #if PG_VERSION_NUM >= 90100
201 elog(WARNING,"'%s' is not an table", curname);
206 newOids[i++] = relOid;
215 tbl->tables = newOids;
216 if (tbl->nTables > 1)
217 qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
233 #if PG_VERSION_NUM >= 90100
235 excludeTablesCheck(char **newval, void **extra, GucSource source)
239 val = (char*)tableListAssign(*newval, false, &excludeTables);
251 excludeTablesAssign(const char *newval, void *extra)
253 tableListAssign(newval, true, &excludeTables);
257 includeTablesCheck(char **newval, void **extra, GucSource source)
261 val = (char*)tableListAssign(*newval, false, &includeTables);
273 includeTablesAssign(const char *newval, void *extra)
275 tableListAssign(newval, true, &excludeTables);
278 #else /* PG_VERSION_NUM < 90100 */
281 excludeTablesAssign(const char * newval, bool doit, GucSource source)
283 return tableListAssign(newval, doit, &excludeTables);
287 includeTablesAssign(const char * newval, bool doit, GucSource source)
289 return tableListAssign(newval, doit, &includeTables);
295 tableListShow(TableList *tbl)
301 len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
302 ptr = val = palloc(len);
304 for(i=0; i<tbl->nTables; i++)
306 char *relname = get_rel_name(tbl->tables[i]);
307 Oid nspOid = get_rel_namespace(tbl->tables[i]);
308 char *nspname = get_namespace_name(nspOid);
310 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
313 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
322 excludeTablesShow(void)
324 return tableListShow(&excludeTables);
328 includeTablesShow(void)
330 return tableListShow(&includeTables);
334 matchOid(TableList *tbl, Oid oid)
336 Oid *StopLow = tbl->tables,
337 *StopHigh = tbl->tables + tbl->nTables,
340 /* Loop invariant: StopLow <= val < StopHigh */
341 while (StopLow < StopHigh)
343 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
345 if (*StopMiddle == oid)
347 else if (*StopMiddle < oid)
348 StopLow = StopMiddle + 1;
350 StopHigh = StopMiddle;
356 #if PG_VERSION_NUM >= 90500
358 makeRangeVarFromOid(Oid relOid)
361 get_namespace_name(get_rel_namespace(relOid)),
362 get_rel_name(relOid),
370 makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
372 TimestampTz now = GetCurrentTimestamp();
374 OnlineAnalyzeTableType reltype;
377 OnlineAnalyzeTableStat *rstat,
379 PgStat_StatTabEntry *tabentry = NULL;
381 if (relOid == InvalidOid)
385 /* return if there is no changes */
387 else if (naffected < 0)
388 /* number if affected rows is unknown */
391 rel = RelationIdGetRelation(relOid);
392 if (rel->rd_rel->relkind != RELKIND_RELATION)
399 #if PG_VERSION_NUM >= 90100
400 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
402 (rel->rd_istemp || rel->rd_islocaltemp)
404 ? OATT_TEMPORARY : OATT_PERSISTENT;
409 * includeTables overwrites excludeTables
411 switch(online_analyze_table_type)
414 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
415 (matchOid(&excludeTables, relOid) == true &&
416 matchOid(&includeTables, relOid) == false))
420 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
421 matchOid(&includeTables, relOid) == false)
425 case OATT_PERSISTENT:
428 * skip analyze if relation's type doesn't not match
429 * online_analyze_table_type
431 if ((online_analyze_table_type & reltype) == 0 ||
432 matchOid(&excludeTables, relOid) == true)
434 if (matchOid(&includeTables, relOid) == false)
441 * Do not store data about persistent table in local memory because we
442 * could not track changes of them: they could be changed by another
443 * backends. So always get a pgstat table entry.
445 if (reltype == OATT_TEMPORARY)
446 rstat = hash_search(relstats, &relOid, HASH_ENTER, &found);
448 rstat = &dummyrstat; /* found == false for following if */
452 MemSet(rstat, 0, sizeof(*rstat));
453 rstat->tableid = relOid;
457 if (operation == CK_VACUUM)
459 /* force reread because vacuum could change n_tuples */
460 rstat->rereadStat = true;
463 else if (operation == CK_ANALYZE)
466 rstat->changes_since_analyze = 0;
467 rstat->analyze_timestamp = now;
469 rstat->rereadStat = true;
473 Assert(rstat->tableid == relOid);
476 /* do not reread data if it was a truncation */
477 operation != CK_TRUNCATE && operation != CK_FASTTRUNCATE &&
478 /* read for persistent table and for temp teble if it allowed */
479 (reltype == OATT_PERSISTENT || online_analyze_local_tracking == false) &&
480 /* read only for new table or we know that it's needed */
481 (newTable == true || rstat->rereadStat == true)
484 rstat->rereadStat = false;
486 tabentry = pgstat_fetch_stat_tabentry(relOid);
490 rstat->n_tuples = tabentry->n_dead_tuples + tabentry->n_live_tuples;
491 rstat->changes_since_analyze =
492 #if PG_VERSION_NUM >= 90000
493 tabentry->changes_since_analyze;
495 tabentry->n_live_tuples + tabentry->n_dead_tuples -
496 tabentry->last_anl_tuples;
498 rstat->autovac_analyze_timestamp =
499 tabentry->autovac_analyze_timestamp;
500 rstat->analyze_timestamp = tabentry->analyze_timestamp;
505 /* force analyze after truncate, fasttruncate already did analyze */
506 operation == CK_TRUNCATE || (
507 /* do not analyze too often, if both stamps are exceeded the go */
508 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
509 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
510 /* do not analyze too small tables */
511 rstat->n_tuples + rstat->changes_since_analyze + naffected > online_analyze_lower_limit &&
512 /* be in sync with relation_needs_vacanalyze */
513 ((double)(rstat->changes_since_analyze + naffected)) >=
514 online_analyze_scale_factor * ((double)rstat->n_tuples) +
515 (double)online_analyze_threshold))
517 #if PG_VERSION_NUM < 90500
520 VacuumParams vacstmt;
522 TimestampTz startStamp, endStamp;
526 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
528 memset(&vacstmt, 0, sizeof(vacstmt));
530 vacstmt.freeze_min_age = -1;
531 vacstmt.freeze_table_age = -1; /* ??? */
533 #if PG_VERSION_NUM < 90500
534 vacstmt.type = T_VacuumStmt;
535 vacstmt.relation = NULL;
536 vacstmt.va_cols = NIL;
537 #if PG_VERSION_NUM >= 90000
538 vacstmt.options = VACOPT_ANALYZE;
539 if (online_analyze_verbose)
540 vacstmt.options |= VACOPT_VERBOSE;
542 vacstmt.vacuum = vacstmt.full = false;
543 vacstmt.analyze = true;
544 vacstmt.verbose = online_analyze_verbose;
547 vacstmt.multixact_freeze_min_age = -1;
548 vacstmt.multixact_freeze_table_age = -1;
549 vacstmt.log_min_duration = -1;
553 if (online_analyze_verbose)
554 startStamp = GetCurrentTimestamp();
556 flags = VACOPT_ANALYZE | VACOPT_NOWAIT |
557 ((online_analyze_verbose) ? VACOPT_VERBOSE : 0);
559 #if PG_VERSION_NUM >= 120000
560 vacstmt.options = flags;
563 #if PG_VERSION_NUM < 90500
565 #if PG_VERSION_NUM >= 90018
568 , GetAccessStrategy(BAS_VACUUM)
569 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
573 makeRangeVarFromOid(relOid),
574 #if PG_VERSION_NUM < 120000
577 &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
581 /* Make changes visible to subsequent calls */
582 CommandCounterIncrement();
584 if (online_analyze_verbose)
589 endStamp = GetCurrentTimestamp();
590 TimestampDifference(startStamp, endStamp, &secs, µsecs);
591 elog(INFO, "analyze \"%s\" took %.02f seconds",
592 get_rel_name(relOid),
593 ((double)secs) + ((double)microsecs)/1.0e6);
596 rstat->autovac_analyze_timestamp = now;
597 rstat->changes_since_analyze = 0;
604 rstat->n_tuples += naffected;
607 rstat->rereadStat = (reltype == OATT_PERSISTENT);
610 case CK_FASTTRUNCATE:
611 rstat->rereadStat = false;
618 /* update last analyze timestamp in local memory of backend */
621 tabentry->analyze_timestamp = now;
622 tabentry->changes_since_analyze = 0;
625 /* force reload stat for new table */
627 pgstat_clear_snapshot();
632 #if PG_VERSION_NUM >= 90000
634 tabentry->changes_since_analyze += naffected;
640 rstat->changes_since_analyze += naffected;
641 rstat->n_tuples += naffected;
644 rstat->changes_since_analyze += 2 * naffected;
645 rstat->n_tuples += naffected;
648 rstat->changes_since_analyze += naffected;
651 case CK_FASTTRUNCATE:
652 rstat->changes_since_analyze = 0;
660 /* Reset local cache if we are over limit */
661 if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
666 isFastTruncateCall(QueryDesc *queryDesc)
673 queryDesc->plannedstmt &&
674 queryDesc->operation == CMD_SELECT &&
675 queryDesc->plannedstmt->planTree &&
676 queryDesc->plannedstmt->planTree->targetlist &&
677 list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
681 te = linitial(queryDesc->plannedstmt->planTree->targetlist);
683 if (!IsA(te, TargetEntry))
686 fe = (FuncExpr*)te->expr;
689 fe && IsA(fe, FuncExpr) &&
690 fe->funcid >= FirstNormalObjectId &&
691 fe->funcretset == false &&
692 fe->funcresulttype == VOIDOID &&
693 fe->funcvariadic == false &&
694 list_length(fe->args) == 1
698 constval = linitial(fe->args);
701 IsA(constval,Const) &&
702 constval->consttype == TEXTOID &&
703 strcmp(get_func_name(fe->funcid), "fasttruncate") == 0
711 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
713 onlineAnalyzeHooker(QueryDesc *queryDesc)
715 int64 naffected = -1;
718 if (queryDesc->estate)
719 naffected = queryDesc->estate->es_processed;
721 #if PG_VERSION_NUM >= 90200
722 if (online_analyze_enable &&
723 (constval = isFastTruncateCall(queryDesc)) != NULL)
725 Datum tblnamed = constval->constvalue;
726 char *tblname = text_to_cstring(DatumGetTextP(tblnamed));
728 makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
730 makeAnalyze(RangeVarGetRelid(tblvar,
733 CK_FASTTRUNCATE, -1);
737 if (online_analyze_enable && queryDesc->plannedstmt &&
738 (queryDesc->operation == CMD_INSERT ||
739 queryDesc->operation == CMD_UPDATE ||
740 queryDesc->operation == CMD_DELETE
741 #if PG_VERSION_NUM < 90200
742 || (queryDesc->operation == CMD_SELECT &&
743 queryDesc->plannedstmt->intoClause)
747 #if PG_VERSION_NUM < 90200
748 if (queryDesc->operation == CMD_SELECT)
750 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
752 makeAnalyze(relOid, queryDesc->operation, naffected);
756 if (queryDesc->plannedstmt->resultRelations &&
757 queryDesc->plannedstmt->rtable)
761 foreach(l, queryDesc->plannedstmt->resultRelations)
763 int n = lfirst_int(l);
764 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
766 if (rte->rtekind == RTE_RELATION)
767 makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
772 if (oldExecutorEndHook)
773 oldExecutorEndHook(queryDesc);
775 standard_ExecutorEnd(queryDesc);
778 static List *toremove = NIL;
781 * removeTable called on transaction end, see call RegisterXactCallback() below
784 removeTable(XactEvent event, void *arg)
790 case XACT_EVENT_COMMIT:
792 case XACT_EVENT_ABORT:
798 foreach(cell, toremove)
800 Oid relOid = lfirst_oid(cell);
802 hash_search(relstats, &relOid, HASH_REMOVE, NULL);
808 #if PG_VERSION_NUM >= 120000
810 parse_vacuum_opt(VacuumStmt *vacstmt)
812 int options = vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE;
815 foreach(lc, vacstmt->options)
817 DefElem *opt = (DefElem *) lfirst(lc);
819 /* Parse common options for VACUUM and ANALYZE */
820 if (strcmp(opt->defname, "verbose") == 0)
821 options |= VACOPT_VERBOSE;
822 else if (strcmp(opt->defname, "skip_locked") == 0)
823 options |= VACOPT_SKIP_LOCKED;
824 else if (strcmp(opt->defname, "analyze") == 0)
825 options |= VACOPT_ANALYZE;
826 else if (strcmp(opt->defname, "freeze") == 0)
827 options |= VACOPT_FREEZE;
828 else if (strcmp(opt->defname, "full") == 0)
829 options |= VACOPT_FULL;
830 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
831 options |= VACOPT_DISABLE_PAGE_SKIPPING;
839 #if PG_VERSION_NUM >= 90200
841 onlineAnalyzeHookerUtility(
842 #if PG_VERSION_NUM >= 100000
847 const char *queryString,
848 #if PG_VERSION_NUM >= 140000
851 #if PG_VERSION_NUM >= 90300
852 ProcessUtilityContext context, ParamListInfo params,
853 #if PG_VERSION_NUM >= 100000
854 QueryEnvironment *queryEnv,
857 ParamListInfo params, bool isTopLevel,
860 #if PG_VERSION_NUM >= 130000
861 QueryCompletion *completionTag
866 List *tblnames = NIL;
867 CmdKind op = CK_INSERT;
868 #if PG_VERSION_NUM >= 100000
869 Node *parsetree = NULL;
871 if (pstmt->commandType == CMD_UTILITY)
872 parsetree = pstmt->utilityStmt;
875 if (parsetree && online_analyze_enable)
877 if (IsA(parsetree, CreateTableAsStmt) &&
878 ((CreateTableAsStmt*)parsetree)->into)
881 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
884 else if (IsA(parsetree, TruncateStmt))
886 tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
889 else if (IsA(parsetree, DropStmt) &&
890 ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
894 foreach(cell, ((DropStmt*)parsetree)->objects)
896 List *relname = (List *) lfirst(cell);
897 RangeVar *rel = makeRangeVarFromNameList(relname);
898 Oid relOid = RangeVarGetRelid(rel, NoLock, true);
900 if (OidIsValid(relOid))
904 ctx = MemoryContextSwitchTo(TopTransactionContext);
905 toremove = lappend_oid(toremove, relOid);
906 MemoryContextSwitchTo(ctx);
910 else if (IsA(parsetree, VacuumStmt))
912 VacuumStmt *vac = (VacuumStmt*)parsetree;
914 #if PG_VERSION_NUM >= 120000
915 parse_vacuum_opt(vac)
922 #if PG_VERSION_NUM >= 110000
923 tblnames = vac->rels;
926 tblnames = list_make1(vac->relation);
929 if (options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
931 /* optionally with analyze */
934 /* drop all collected stat */
938 else if (options & VACOPT_ANALYZE)
942 /* should reset all counters */
946 OnlineAnalyzeTableStat *rstat;
947 TimestampTz now = GetCurrentTimestamp();
949 hash_seq_init(&hs, relstats);
951 while((rstat = hash_seq_search(&hs)) != NULL)
953 rstat->changes_since_analyze = 0;
954 rstat->analyze_timestamp = now;
963 #if PG_VERSION_NUM >= 100000
964 #define parsetree pstmt
967 if (oldProcessUtilityHook)
968 oldProcessUtilityHook(parsetree, queryString,
969 #if PG_VERSION_NUM >= 140000
972 #if PG_VERSION_NUM >= 90300
974 #if PG_VERSION_NUM >= 100000
980 dest, completionTag);
982 standard_ProcessUtility(parsetree, queryString,
983 #if PG_VERSION_NUM >= 140000
986 #if PG_VERSION_NUM >= 90300
988 #if PG_VERSION_NUM >= 100000
994 dest, completionTag);
996 #if PG_VERSION_NUM >= 100000
1003 foreach(l, tblnames)
1006 #if PG_VERSION_NUM >= 110000
1007 (IsA(lfirst(l), VacuumRelation)) ?
1008 ((VacuumRelation*)lfirst(l))->relation :
1010 (RangeVar*)lfirst(l);
1013 Assert(IsA(tblname, RangeVar));
1015 tblOid = RangeVarGetRelid(tblname, NoLock, true);
1016 makeAnalyze(tblOid, op, -1);
1029 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1031 hash_ctl.hash = oid_hash;
1032 flags |= HASH_FUNCTION;
1034 if (onlineAnalyzeMemoryContext)
1036 Assert(relstats != NULL);
1037 MemoryContextReset(onlineAnalyzeMemoryContext);
1041 Assert(relstats == NULL);
1043 #if PG_VERSION_NUM < 90600
1044 onlineAnalyzeMemoryContext =
1045 AllocSetContextCreate(CacheMemoryContext,
1046 "online_analyze storage context",
1047 ALLOCSET_DEFAULT_MINSIZE,
1048 ALLOCSET_DEFAULT_INITSIZE,
1049 ALLOCSET_DEFAULT_MAXSIZE
1052 onlineAnalyzeMemoryContext =
1053 AllocSetContextCreate(CacheMemoryContext,
1054 "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1058 hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1059 flags |= HASH_CONTEXT;
1061 hash_ctl.keysize = sizeof(Oid);
1063 hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1066 relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1069 void _PG_init(void);
1075 oldExecutorEndHook = ExecutorEnd_hook;
1077 ExecutorEnd_hook = onlineAnalyzeHooker;
1079 #if PG_VERSION_NUM >= 90200
1080 oldProcessUtilityHook = ProcessUtility_hook;
1082 ProcessUtility_hook = onlineAnalyzeHookerUtility;
1086 DefineCustomBoolVariable(
1087 "online_analyze.enable",
1088 "Enable on-line analyze",
1089 "Enables analyze of table directly after insert/update/delete/select into",
1090 &online_analyze_enable,
1091 #if PG_VERSION_NUM >= 80400
1092 online_analyze_enable,
1095 #if PG_VERSION_NUM >= 80400
1097 #if PG_VERSION_NUM >= 90100
1105 DefineCustomBoolVariable(
1106 "online_analyze.local_tracking",
1107 "Per backend tracking",
1108 "Per backend tracking for temp tables (do not use system statistic)",
1109 &online_analyze_local_tracking,
1110 #if PG_VERSION_NUM >= 80400
1111 online_analyze_local_tracking,
1114 #if PG_VERSION_NUM >= 80400
1116 #if PG_VERSION_NUM >= 90100
1124 DefineCustomBoolVariable(
1125 "online_analyze.verbose",
1126 "Verbosity of on-line analyze",
1127 "Make ANALYZE VERBOSE after table's changes",
1128 &online_analyze_verbose,
1129 #if PG_VERSION_NUM >= 80400
1130 online_analyze_verbose,
1133 #if PG_VERSION_NUM >= 80400
1135 #if PG_VERSION_NUM >= 90100
1143 DefineCustomRealVariable(
1144 "online_analyze.scale_factor",
1145 "fraction of table size to start on-line analyze",
1146 "fraction of table size to start on-line analyze",
1147 &online_analyze_scale_factor,
1148 #if PG_VERSION_NUM >= 80400
1149 online_analyze_scale_factor,
1154 #if PG_VERSION_NUM >= 80400
1156 #if PG_VERSION_NUM >= 90100
1164 DefineCustomIntVariable(
1165 "online_analyze.threshold",
1166 "min number of row updates before on-line analyze",
1167 "min number of row updates before on-line analyze",
1168 &online_analyze_threshold,
1169 #if PG_VERSION_NUM >= 80400
1170 online_analyze_threshold,
1175 #if PG_VERSION_NUM >= 80400
1177 #if PG_VERSION_NUM >= 90100
1185 DefineCustomIntVariable(
1186 "online_analyze.capacity_threshold",
1187 "Max local cache table capacity",
1188 "Max local cache table capacity",
1189 &online_analyze_capacity_threshold,
1190 #if PG_VERSION_NUM >= 80400
1191 online_analyze_capacity_threshold,
1196 #if PG_VERSION_NUM >= 80400
1198 #if PG_VERSION_NUM >= 90100
1206 DefineCustomRealVariable(
1207 "online_analyze.min_interval",
1208 "minimum time interval between analyze call (in milliseconds)",
1209 "minimum time interval between analyze call (in milliseconds)",
1210 &online_analyze_min_interval,
1211 #if PG_VERSION_NUM >= 80400
1212 online_analyze_min_interval,
1217 #if PG_VERSION_NUM >= 80400
1219 #if PG_VERSION_NUM >= 90100
1227 DefineCustomEnumVariable(
1228 "online_analyze.table_type",
1229 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1231 &online_analyze_table_type,
1232 #if PG_VERSION_NUM >= 80400
1233 online_analyze_table_type,
1235 online_analyze_table_type_options,
1237 #if PG_VERSION_NUM >= 80400
1239 #if PG_VERSION_NUM >= 90100
1247 DefineCustomStringVariable(
1248 "online_analyze.exclude_tables",
1249 "List of tables which will not online analyze",
1251 &excludeTables.tableStr,
1252 #if PG_VERSION_NUM >= 80400
1257 #if PG_VERSION_NUM >= 90100
1259 excludeTablesAssign,
1261 excludeTablesAssign,
1266 DefineCustomStringVariable(
1267 "online_analyze.include_tables",
1268 "List of tables which will online analyze",
1270 &includeTables.tableStr,
1271 #if PG_VERSION_NUM >= 80400
1276 #if PG_VERSION_NUM >= 90100
1278 includeTablesAssign,
1280 includeTablesAssign,
1285 DefineCustomIntVariable(
1286 "online_analyze.lower_limit",
1287 "min number of rows in table to analyze",
1288 "min number of rows in table to analyze",
1289 &online_analyze_lower_limit,
1290 #if PG_VERSION_NUM >= 80400
1291 online_analyze_lower_limit,
1296 #if PG_VERSION_NUM >= 80400
1298 #if PG_VERSION_NUM >= 90100
1306 RegisterXactCallback(removeTable, NULL);
1309 void _PG_fini(void);
1313 ExecutorEnd_hook = oldExecutorEndHook;
1314 #if PG_VERSION_NUM >= 90200
1315 ProcessUtility_hook = oldProcessUtilityHook;
1318 if (excludeTables.tables)
1319 free(excludeTables.tables);
1320 if (includeTables.tables)
1321 free(includeTables.tables);
1323 excludeTables.tables = includeTables.tables = NULL;
1324 excludeTables.nTables = includeTables.nTables = 0;