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 >= 90300
849 ProcessUtilityContext context, ParamListInfo params,
850 #if PG_VERSION_NUM >= 100000
851 QueryEnvironment *queryEnv,
854 ParamListInfo params, bool isTopLevel,
857 #if PG_VERSION_NUM >= 130000
858 QueryCompletion *completionTag
863 List *tblnames = NIL;
864 CmdKind op = CK_INSERT;
865 #if PG_VERSION_NUM >= 100000
866 Node *parsetree = NULL;
868 if (pstmt->commandType == CMD_UTILITY)
869 parsetree = pstmt->utilityStmt;
872 if (parsetree && online_analyze_enable)
874 if (IsA(parsetree, CreateTableAsStmt) &&
875 ((CreateTableAsStmt*)parsetree)->into)
878 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
881 else if (IsA(parsetree, TruncateStmt))
883 tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
886 else if (IsA(parsetree, DropStmt) &&
887 ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
891 foreach(cell, ((DropStmt*)parsetree)->objects)
893 List *relname = (List *) lfirst(cell);
894 RangeVar *rel = makeRangeVarFromNameList(relname);
895 Oid relOid = RangeVarGetRelid(rel, NoLock, true);
897 if (OidIsValid(relOid))
901 ctx = MemoryContextSwitchTo(TopTransactionContext);
902 toremove = lappend_oid(toremove, relOid);
903 MemoryContextSwitchTo(ctx);
907 else if (IsA(parsetree, VacuumStmt))
909 VacuumStmt *vac = (VacuumStmt*)parsetree;
911 #if PG_VERSION_NUM >= 120000
912 parse_vacuum_opt(vac)
919 #if PG_VERSION_NUM >= 110000
920 tblnames = vac->rels;
923 tblnames = list_make1(vac->relation);
926 if (options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
928 /* optionally with analyze */
931 /* drop all collected stat */
935 else if (options & VACOPT_ANALYZE)
939 /* should reset all counters */
943 OnlineAnalyzeTableStat *rstat;
944 TimestampTz now = GetCurrentTimestamp();
946 hash_seq_init(&hs, relstats);
948 while((rstat = hash_seq_search(&hs)) != NULL)
950 rstat->changes_since_analyze = 0;
951 rstat->analyze_timestamp = now;
960 #if PG_VERSION_NUM >= 100000
961 #define parsetree pstmt
964 if (oldProcessUtilityHook)
965 oldProcessUtilityHook(parsetree, queryString,
966 #if PG_VERSION_NUM >= 90300
968 #if PG_VERSION_NUM >= 100000
974 dest, completionTag);
976 standard_ProcessUtility(parsetree, queryString,
977 #if PG_VERSION_NUM >= 90300
979 #if PG_VERSION_NUM >= 100000
985 dest, completionTag);
987 #if PG_VERSION_NUM >= 100000
997 #if PG_VERSION_NUM >= 110000
998 (IsA(lfirst(l), VacuumRelation)) ?
999 ((VacuumRelation*)lfirst(l))->relation :
1001 (RangeVar*)lfirst(l);
1004 Assert(IsA(tblname, RangeVar));
1006 tblOid = RangeVarGetRelid(tblname, NoLock, true);
1007 makeAnalyze(tblOid, op, -1);
1020 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1022 hash_ctl.hash = oid_hash;
1023 flags |= HASH_FUNCTION;
1025 if (onlineAnalyzeMemoryContext)
1027 Assert(relstats != NULL);
1028 MemoryContextReset(onlineAnalyzeMemoryContext);
1032 Assert(relstats == NULL);
1034 #if PG_VERSION_NUM < 90600
1035 onlineAnalyzeMemoryContext =
1036 AllocSetContextCreate(CacheMemoryContext,
1037 "online_analyze storage context",
1038 ALLOCSET_DEFAULT_MINSIZE,
1039 ALLOCSET_DEFAULT_INITSIZE,
1040 ALLOCSET_DEFAULT_MAXSIZE
1043 onlineAnalyzeMemoryContext =
1044 AllocSetContextCreate(CacheMemoryContext,
1045 "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1049 hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1050 flags |= HASH_CONTEXT;
1052 hash_ctl.keysize = sizeof(Oid);
1054 hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1057 relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1060 void _PG_init(void);
1066 oldExecutorEndHook = ExecutorEnd_hook;
1068 ExecutorEnd_hook = onlineAnalyzeHooker;
1070 #if PG_VERSION_NUM >= 90200
1071 oldProcessUtilityHook = ProcessUtility_hook;
1073 ProcessUtility_hook = onlineAnalyzeHookerUtility;
1077 DefineCustomBoolVariable(
1078 "online_analyze.enable",
1079 "Enable on-line analyze",
1080 "Enables analyze of table directly after insert/update/delete/select into",
1081 &online_analyze_enable,
1082 #if PG_VERSION_NUM >= 80400
1083 online_analyze_enable,
1086 #if PG_VERSION_NUM >= 80400
1088 #if PG_VERSION_NUM >= 90100
1096 DefineCustomBoolVariable(
1097 "online_analyze.local_tracking",
1098 "Per backend tracking",
1099 "Per backend tracking for temp tables (do not use system statistic)",
1100 &online_analyze_local_tracking,
1101 #if PG_VERSION_NUM >= 80400
1102 online_analyze_local_tracking,
1105 #if PG_VERSION_NUM >= 80400
1107 #if PG_VERSION_NUM >= 90100
1115 DefineCustomBoolVariable(
1116 "online_analyze.verbose",
1117 "Verbosity of on-line analyze",
1118 "Make ANALYZE VERBOSE after table's changes",
1119 &online_analyze_verbose,
1120 #if PG_VERSION_NUM >= 80400
1121 online_analyze_verbose,
1124 #if PG_VERSION_NUM >= 80400
1126 #if PG_VERSION_NUM >= 90100
1134 DefineCustomRealVariable(
1135 "online_analyze.scale_factor",
1136 "fraction of table size to start on-line analyze",
1137 "fraction of table size to start on-line analyze",
1138 &online_analyze_scale_factor,
1139 #if PG_VERSION_NUM >= 80400
1140 online_analyze_scale_factor,
1145 #if PG_VERSION_NUM >= 80400
1147 #if PG_VERSION_NUM >= 90100
1155 DefineCustomIntVariable(
1156 "online_analyze.threshold",
1157 "min number of row updates before on-line analyze",
1158 "min number of row updates before on-line analyze",
1159 &online_analyze_threshold,
1160 #if PG_VERSION_NUM >= 80400
1161 online_analyze_threshold,
1166 #if PG_VERSION_NUM >= 80400
1168 #if PG_VERSION_NUM >= 90100
1176 DefineCustomIntVariable(
1177 "online_analyze.capacity_threshold",
1178 "Max local cache table capacity",
1179 "Max local cache table capacity",
1180 &online_analyze_capacity_threshold,
1181 #if PG_VERSION_NUM >= 80400
1182 online_analyze_capacity_threshold,
1187 #if PG_VERSION_NUM >= 80400
1189 #if PG_VERSION_NUM >= 90100
1197 DefineCustomRealVariable(
1198 "online_analyze.min_interval",
1199 "minimum time interval between analyze call (in milliseconds)",
1200 "minimum time interval between analyze call (in milliseconds)",
1201 &online_analyze_min_interval,
1202 #if PG_VERSION_NUM >= 80400
1203 online_analyze_min_interval,
1208 #if PG_VERSION_NUM >= 80400
1210 #if PG_VERSION_NUM >= 90100
1218 DefineCustomEnumVariable(
1219 "online_analyze.table_type",
1220 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1222 &online_analyze_table_type,
1223 #if PG_VERSION_NUM >= 80400
1224 online_analyze_table_type,
1226 online_analyze_table_type_options,
1228 #if PG_VERSION_NUM >= 80400
1230 #if PG_VERSION_NUM >= 90100
1238 DefineCustomStringVariable(
1239 "online_analyze.exclude_tables",
1240 "List of tables which will not online analyze",
1242 &excludeTables.tableStr,
1243 #if PG_VERSION_NUM >= 80400
1248 #if PG_VERSION_NUM >= 90100
1250 excludeTablesAssign,
1252 excludeTablesAssign,
1257 DefineCustomStringVariable(
1258 "online_analyze.include_tables",
1259 "List of tables which will online analyze",
1261 &includeTables.tableStr,
1262 #if PG_VERSION_NUM >= 80400
1267 #if PG_VERSION_NUM >= 90100
1269 includeTablesAssign,
1271 includeTablesAssign,
1276 DefineCustomIntVariable(
1277 "online_analyze.lower_limit",
1278 "min number of rows in table to analyze",
1279 "min number of rows in table to analyze",
1280 &online_analyze_lower_limit,
1281 #if PG_VERSION_NUM >= 80400
1282 online_analyze_lower_limit,
1287 #if PG_VERSION_NUM >= 80400
1289 #if PG_VERSION_NUM >= 90100
1297 RegisterXactCallback(removeTable, NULL);
1300 void _PG_fini(void);
1304 ExecutorEnd_hook = oldExecutorEndHook;
1305 #if PG_VERSION_NUM >= 90200
1306 ProcessUtility_hook = oldProcessUtilityHook;
1309 if (excludeTables.tables)
1310 free(excludeTables.tables);
1311 if (includeTables.tables)
1312 free(includeTables.tables);
1314 excludeTables.tables = includeTables.tables = NULL;
1315 excludeTables.nTables = includeTables.nTables = 0;