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;
606 rstat->rereadStat = (reltype == OATT_PERSISTENT);
609 case CK_FASTTRUNCATE:
610 rstat->rereadStat = false;
617 /* update last analyze timestamp in local memory of backend */
620 tabentry->analyze_timestamp = now;
621 tabentry->changes_since_analyze = 0;
624 /* force reload stat for new table */
626 pgstat_clear_snapshot();
631 #if PG_VERSION_NUM >= 90000
633 tabentry->changes_since_analyze += naffected;
639 rstat->changes_since_analyze += naffected;
640 rstat->n_tuples += naffected;
643 rstat->changes_since_analyze += 2 * naffected;
644 rstat->n_tuples += naffected;
646 rstat->changes_since_analyze += naffected;
649 case CK_FASTTRUNCATE:
650 rstat->changes_since_analyze = 0;
658 /* Reset local cache if we are over limit */
659 if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
664 isFastTruncateCall(QueryDesc *queryDesc)
671 queryDesc->plannedstmt &&
672 queryDesc->operation == CMD_SELECT &&
673 queryDesc->plannedstmt->planTree &&
674 queryDesc->plannedstmt->planTree->targetlist &&
675 list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
679 te = linitial(queryDesc->plannedstmt->planTree->targetlist);
681 if (!IsA(te, TargetEntry))
684 fe = (FuncExpr*)te->expr;
687 fe && IsA(fe, FuncExpr) &&
688 fe->funcid >= FirstNormalObjectId &&
689 fe->funcretset == false &&
690 fe->funcresulttype == VOIDOID &&
691 fe->funcvariadic == false &&
692 list_length(fe->args) == 1
696 constval = linitial(fe->args);
699 IsA(constval,Const) &&
700 constval->consttype == TEXTOID &&
701 strcmp(get_func_name(fe->funcid), "fasttruncate") == 0
709 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
711 onlineAnalyzeHooker(QueryDesc *queryDesc)
713 int64 naffected = -1;
716 if (queryDesc->estate)
717 naffected = queryDesc->estate->es_processed;
719 #if PG_VERSION_NUM >= 90200
720 if (online_analyze_enable &&
721 (constval = isFastTruncateCall(queryDesc)) != NULL)
723 Datum tblnamed = constval->constvalue;
724 char *tblname = text_to_cstring(DatumGetTextP(tblnamed));
726 makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
728 makeAnalyze(RangeVarGetRelid(tblvar,
731 CK_FASTTRUNCATE, -1);
735 if (online_analyze_enable && queryDesc->plannedstmt &&
736 (queryDesc->operation == CMD_INSERT ||
737 queryDesc->operation == CMD_UPDATE ||
738 queryDesc->operation == CMD_DELETE
739 #if PG_VERSION_NUM < 90200
740 || (queryDesc->operation == CMD_SELECT &&
741 queryDesc->plannedstmt->intoClause)
745 #if PG_VERSION_NUM < 90200
746 if (queryDesc->operation == CMD_SELECT)
748 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
750 makeAnalyze(relOid, queryDesc->operation, naffected);
754 if (queryDesc->plannedstmt->resultRelations &&
755 queryDesc->plannedstmt->rtable)
759 foreach(l, queryDesc->plannedstmt->resultRelations)
761 int n = lfirst_int(l);
762 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
764 if (rte->rtekind == RTE_RELATION)
765 makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
770 if (oldExecutorEndHook)
771 oldExecutorEndHook(queryDesc);
773 standard_ExecutorEnd(queryDesc);
776 static List *toremove = NIL;
779 * removeTable called on transaction end, see call RegisterXactCallback() below
782 removeTable(XactEvent event, void *arg)
788 case XACT_EVENT_COMMIT:
790 case XACT_EVENT_ABORT:
796 foreach(cell, toremove)
798 Oid relOid = lfirst_oid(cell);
800 hash_search(relstats, &relOid, HASH_REMOVE, NULL);
806 #if PG_VERSION_NUM >= 120000
808 parse_vacuum_opt(VacuumStmt *vacstmt)
810 int options = vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE;
813 foreach(lc, vacstmt->options)
815 DefElem *opt = (DefElem *) lfirst(lc);
817 /* Parse common options for VACUUM and ANALYZE */
818 if (strcmp(opt->defname, "verbose") == 0)
819 options |= VACOPT_VERBOSE;
820 else if (strcmp(opt->defname, "skip_locked") == 0)
821 options |= VACOPT_SKIP_LOCKED;
822 else if (strcmp(opt->defname, "analyze") == 0)
823 options |= VACOPT_ANALYZE;
824 else if (strcmp(opt->defname, "freeze") == 0)
825 options |= VACOPT_FREEZE;
826 else if (strcmp(opt->defname, "full") == 0)
827 options |= VACOPT_FULL;
828 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
829 options |= VACOPT_DISABLE_PAGE_SKIPPING;
837 #if PG_VERSION_NUM >= 90200
839 onlineAnalyzeHookerUtility(
840 #if PG_VERSION_NUM >= 100000
845 const char *queryString,
846 #if PG_VERSION_NUM >= 90300
847 ProcessUtilityContext context, ParamListInfo params,
848 #if PG_VERSION_NUM >= 100000
849 QueryEnvironment *queryEnv,
852 ParamListInfo params, bool isTopLevel,
855 #if PG_VERSION_NUM >= 130000
856 QueryCompletion *completionTag
861 List *tblnames = NIL;
862 CmdKind op = CK_INSERT;
863 #if PG_VERSION_NUM >= 100000
864 Node *parsetree = NULL;
866 if (pstmt->commandType == CMD_UTILITY)
867 parsetree = pstmt->utilityStmt;
870 if (parsetree && online_analyze_enable)
872 if (IsA(parsetree, CreateTableAsStmt) &&
873 ((CreateTableAsStmt*)parsetree)->into)
876 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
879 else if (IsA(parsetree, TruncateStmt))
881 tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
884 else if (IsA(parsetree, DropStmt) &&
885 ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
889 foreach(cell, ((DropStmt*)parsetree)->objects)
891 List *relname = (List *) lfirst(cell);
892 RangeVar *rel = makeRangeVarFromNameList(relname);
893 Oid relOid = RangeVarGetRelid(rel, NoLock, true);
895 if (OidIsValid(relOid))
899 ctx = MemoryContextSwitchTo(TopTransactionContext);
900 toremove = lappend_oid(toremove, relOid);
901 MemoryContextSwitchTo(ctx);
905 else if (IsA(parsetree, VacuumStmt))
907 VacuumStmt *vac = (VacuumStmt*)parsetree;
909 #if PG_VERSION_NUM >= 120000
910 parse_vacuum_opt(vac)
917 #if PG_VERSION_NUM >= 110000
918 tblnames = vac->rels;
921 tblnames = list_make1(vac->relation);
924 if (options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
926 /* optionally with analyze */
929 /* drop all collected stat */
933 else if (options & VACOPT_ANALYZE)
937 /* should reset all counters */
941 OnlineAnalyzeTableStat *rstat;
942 TimestampTz now = GetCurrentTimestamp();
944 hash_seq_init(&hs, relstats);
946 while((rstat = hash_seq_search(&hs)) != NULL)
948 rstat->changes_since_analyze = 0;
949 rstat->analyze_timestamp = now;
958 #if PG_VERSION_NUM >= 100000
959 #define parsetree pstmt
962 if (oldProcessUtilityHook)
963 oldProcessUtilityHook(parsetree, queryString,
964 #if PG_VERSION_NUM >= 90300
966 #if PG_VERSION_NUM >= 100000
972 dest, completionTag);
974 standard_ProcessUtility(parsetree, queryString,
975 #if PG_VERSION_NUM >= 90300
977 #if PG_VERSION_NUM >= 100000
983 dest, completionTag);
985 #if PG_VERSION_NUM >= 100000
995 #if PG_VERSION_NUM >= 110000
996 (IsA(lfirst(l), VacuumRelation)) ?
997 ((VacuumRelation*)lfirst(l))->relation :
999 (RangeVar*)lfirst(l);
1002 Assert(IsA(tblname, RangeVar));
1004 tblOid = RangeVarGetRelid(tblname, NoLock, true);
1005 makeAnalyze(tblOid, op, -1);
1018 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1020 hash_ctl.hash = oid_hash;
1021 flags |= HASH_FUNCTION;
1023 if (onlineAnalyzeMemoryContext)
1025 Assert(relstats != NULL);
1026 MemoryContextReset(onlineAnalyzeMemoryContext);
1030 Assert(relstats == NULL);
1032 #if PG_VERSION_NUM < 90600
1033 onlineAnalyzeMemoryContext =
1034 AllocSetContextCreate(CacheMemoryContext,
1035 "online_analyze storage context",
1036 ALLOCSET_DEFAULT_MINSIZE,
1037 ALLOCSET_DEFAULT_INITSIZE,
1038 ALLOCSET_DEFAULT_MAXSIZE
1041 onlineAnalyzeMemoryContext =
1042 AllocSetContextCreate(CacheMemoryContext,
1043 "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1047 hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1048 flags |= HASH_CONTEXT;
1050 hash_ctl.keysize = sizeof(Oid);
1052 hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1055 relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1058 void _PG_init(void);
1064 oldExecutorEndHook = ExecutorEnd_hook;
1066 ExecutorEnd_hook = onlineAnalyzeHooker;
1068 #if PG_VERSION_NUM >= 90200
1069 oldProcessUtilityHook = ProcessUtility_hook;
1071 ProcessUtility_hook = onlineAnalyzeHookerUtility;
1075 DefineCustomBoolVariable(
1076 "online_analyze.enable",
1077 "Enable on-line analyze",
1078 "Enables analyze of table directly after insert/update/delete/select into",
1079 &online_analyze_enable,
1080 #if PG_VERSION_NUM >= 80400
1081 online_analyze_enable,
1084 #if PG_VERSION_NUM >= 80400
1086 #if PG_VERSION_NUM >= 90100
1094 DefineCustomBoolVariable(
1095 "online_analyze.local_tracking",
1096 "Per backend tracking",
1097 "Per backend tracking for temp tables (do not use system statistic)",
1098 &online_analyze_local_tracking,
1099 #if PG_VERSION_NUM >= 80400
1100 online_analyze_local_tracking,
1103 #if PG_VERSION_NUM >= 80400
1105 #if PG_VERSION_NUM >= 90100
1113 DefineCustomBoolVariable(
1114 "online_analyze.verbose",
1115 "Verbosity of on-line analyze",
1116 "Make ANALYZE VERBOSE after table's changes",
1117 &online_analyze_verbose,
1118 #if PG_VERSION_NUM >= 80400
1119 online_analyze_verbose,
1122 #if PG_VERSION_NUM >= 80400
1124 #if PG_VERSION_NUM >= 90100
1132 DefineCustomRealVariable(
1133 "online_analyze.scale_factor",
1134 "fraction of table size to start on-line analyze",
1135 "fraction of table size to start on-line analyze",
1136 &online_analyze_scale_factor,
1137 #if PG_VERSION_NUM >= 80400
1138 online_analyze_scale_factor,
1143 #if PG_VERSION_NUM >= 80400
1145 #if PG_VERSION_NUM >= 90100
1153 DefineCustomIntVariable(
1154 "online_analyze.threshold",
1155 "min number of row updates before on-line analyze",
1156 "min number of row updates before on-line analyze",
1157 &online_analyze_threshold,
1158 #if PG_VERSION_NUM >= 80400
1159 online_analyze_threshold,
1164 #if PG_VERSION_NUM >= 80400
1166 #if PG_VERSION_NUM >= 90100
1174 DefineCustomIntVariable(
1175 "online_analyze.capacity_threshold",
1176 "Max local cache table capacity",
1177 "Max local cache table capacity",
1178 &online_analyze_capacity_threshold,
1179 #if PG_VERSION_NUM >= 80400
1180 online_analyze_capacity_threshold,
1185 #if PG_VERSION_NUM >= 80400
1187 #if PG_VERSION_NUM >= 90100
1195 DefineCustomRealVariable(
1196 "online_analyze.min_interval",
1197 "minimum time interval between analyze call (in milliseconds)",
1198 "minimum time interval between analyze call (in milliseconds)",
1199 &online_analyze_min_interval,
1200 #if PG_VERSION_NUM >= 80400
1201 online_analyze_min_interval,
1206 #if PG_VERSION_NUM >= 80400
1208 #if PG_VERSION_NUM >= 90100
1216 DefineCustomEnumVariable(
1217 "online_analyze.table_type",
1218 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1220 &online_analyze_table_type,
1221 #if PG_VERSION_NUM >= 80400
1222 online_analyze_table_type,
1224 online_analyze_table_type_options,
1226 #if PG_VERSION_NUM >= 80400
1228 #if PG_VERSION_NUM >= 90100
1236 DefineCustomStringVariable(
1237 "online_analyze.exclude_tables",
1238 "List of tables which will not online analyze",
1240 &excludeTables.tableStr,
1241 #if PG_VERSION_NUM >= 80400
1246 #if PG_VERSION_NUM >= 90100
1248 excludeTablesAssign,
1250 excludeTablesAssign,
1255 DefineCustomStringVariable(
1256 "online_analyze.include_tables",
1257 "List of tables which will online analyze",
1259 &includeTables.tableStr,
1260 #if PG_VERSION_NUM >= 80400
1265 #if PG_VERSION_NUM >= 90100
1267 includeTablesAssign,
1269 includeTablesAssign,
1274 DefineCustomIntVariable(
1275 "online_analyze.lower_limit",
1276 "min number of rows in table to analyze",
1277 "min number of rows in table to analyze",
1278 &online_analyze_lower_limit,
1279 #if PG_VERSION_NUM >= 80400
1280 online_analyze_lower_limit,
1285 #if PG_VERSION_NUM >= 80400
1287 #if PG_VERSION_NUM >= 90100
1295 RegisterXactCallback(removeTable, NULL);
1298 void _PG_fini(void);
1302 ExecutorEnd_hook = oldExecutorEndHook;
1303 #if PG_VERSION_NUM >= 90200
1304 ProcessUtility_hook = oldProcessUtilityHook;
1307 if (excludeTables.tables)
1308 free(excludeTables.tables);
1309 if (includeTables.tables)
1310 free(includeTables.tables);
1312 excludeTables.tables = includeTables.tables = NULL;
1313 excludeTables.nTables = includeTables.nTables = 0;