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"
62 #ifdef PG_MODULE_MAGIC
66 static bool online_analyze_enable = true;
67 static bool online_analyze_local_tracking = false;
68 static bool online_analyze_verbose = true;
69 static double online_analyze_scale_factor = 0.1;
70 static int online_analyze_threshold = 50;
71 static int online_analyze_capacity_threshold = 100000;
72 static double online_analyze_min_interval = 10000;
73 static int online_analyze_lower_limit = 0;
75 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
76 #if PG_VERSION_NUM >= 90200
77 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
80 #if PG_VERSION_NUM >= 120000
81 #define VACOPT_NOWAIT VACOPT_SKIP_LOCKED
86 CK_SELECT = CMD_SELECT,
87 CK_UPDATE = CMD_UPDATE,
88 CK_INSERT = CMD_INSERT,
89 CK_DELETE = CMD_DELETE,
101 OATT_PERSISTENT = 0x01,
102 OATT_TEMPORARY = 0x02,
104 } OnlineAnalyzeTableType;
106 static const struct config_enum_entry online_analyze_table_type_options[] =
108 {"all", OATT_ALL, false},
109 {"persistent", OATT_PERSISTENT, false},
110 {"temporary", OATT_TEMPORARY, false},
111 {"none", OATT_NONE, false},
115 static int online_analyze_table_type = (int)OATT_ALL;
117 typedef struct TableList {
123 static TableList excludeTables = {0, NULL, NULL};
124 static TableList includeTables = {0, NULL, NULL};
126 typedef struct OnlineAnalyzeTableStat {
129 PgStat_Counter n_tuples;
130 PgStat_Counter changes_since_analyze;
131 TimestampTz autovac_analyze_timestamp;
132 TimestampTz analyze_timestamp;
133 } OnlineAnalyzeTableStat;
135 static MemoryContext onlineAnalyzeMemoryContext = NULL;
136 static HTAB *relstats = NULL;
138 static void relstatsInit(void);
140 #if PG_VERSION_NUM < 100000
142 oid_cmp(const void *a, const void *b)
144 if (*(Oid*)a == *(Oid*)b)
146 return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
151 tableListAssign(const char * newval, bool doit, TableList *tbl)
160 rawname = pstrdup(newval);
162 if (!SplitIdentifierString(rawname, ',', &namelist))
167 nOids = list_length(namelist);
168 newOids = malloc(sizeof(Oid) * (nOids+1));
170 elog(ERROR,"could not allocate %d bytes",
171 (int)(sizeof(Oid) * (nOids+1)));
176 char *curname = (char *) lfirst(l);
177 #if PG_VERSION_NUM >= 90200
178 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
179 stringToQualifiedNameList(curname)), NoLock, true);
181 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
182 stringToQualifiedNameList(curname)), true);
185 if (relOid == InvalidOid)
187 #if PG_VERSION_NUM >= 90100
190 elog(WARNING,"'%s' does not exist", curname);
193 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
195 #if PG_VERSION_NUM >= 90100
198 elog(WARNING,"'%s' is not an table", curname);
203 newOids[i++] = relOid;
212 tbl->tables = newOids;
213 if (tbl->nTables > 1)
214 qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
230 #if PG_VERSION_NUM >= 90100
232 excludeTablesCheck(char **newval, void **extra, GucSource source)
236 val = (char*)tableListAssign(*newval, false, &excludeTables);
248 excludeTablesAssign(const char *newval, void *extra)
250 tableListAssign(newval, true, &excludeTables);
254 includeTablesCheck(char **newval, void **extra, GucSource source)
258 val = (char*)tableListAssign(*newval, false, &includeTables);
270 includeTablesAssign(const char *newval, void *extra)
272 tableListAssign(newval, true, &excludeTables);
275 #else /* PG_VERSION_NUM < 90100 */
278 excludeTablesAssign(const char * newval, bool doit, GucSource source)
280 return tableListAssign(newval, doit, &excludeTables);
284 includeTablesAssign(const char * newval, bool doit, GucSource source)
286 return tableListAssign(newval, doit, &includeTables);
292 tableListShow(TableList *tbl)
298 len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
299 ptr = val = palloc(len);
301 for(i=0; i<tbl->nTables; i++)
303 char *relname = get_rel_name(tbl->tables[i]);
304 Oid nspOid = get_rel_namespace(tbl->tables[i]);
305 char *nspname = get_namespace_name(nspOid);
307 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
310 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
319 excludeTablesShow(void)
321 return tableListShow(&excludeTables);
325 includeTablesShow(void)
327 return tableListShow(&includeTables);
331 matchOid(TableList *tbl, Oid oid)
333 Oid *StopLow = tbl->tables,
334 *StopHigh = tbl->tables + tbl->nTables,
337 /* Loop invariant: StopLow <= val < StopHigh */
338 while (StopLow < StopHigh)
340 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
342 if (*StopMiddle == oid)
344 else if (*StopMiddle < oid)
345 StopLow = StopMiddle + 1;
347 StopHigh = StopMiddle;
353 #if PG_VERSION_NUM >= 90500
355 makeRangeVarFromOid(Oid relOid)
358 get_namespace_name(get_rel_namespace(relOid)),
359 get_rel_name(relOid),
367 makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
369 TimestampTz now = GetCurrentTimestamp();
371 OnlineAnalyzeTableType reltype;
374 OnlineAnalyzeTableStat *rstat,
376 PgStat_StatTabEntry *tabentry = NULL;
378 if (relOid == InvalidOid)
382 /* return if there is no changes */
384 else if (naffected < 0)
385 /* number if affected rows is unknown */
388 rel = RelationIdGetRelation(relOid);
389 if (rel->rd_rel->relkind != RELKIND_RELATION)
396 #if PG_VERSION_NUM >= 90100
397 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
399 (rel->rd_istemp || rel->rd_islocaltemp)
401 ? OATT_TEMPORARY : OATT_PERSISTENT;
406 * includeTables overwrites excludeTables
408 switch(online_analyze_table_type)
411 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
412 (matchOid(&excludeTables, relOid) == true &&
413 matchOid(&includeTables, relOid) == false))
417 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
418 matchOid(&includeTables, relOid) == false)
422 case OATT_PERSISTENT:
425 * skip analyze if relation's type doesn't not match
426 * online_analyze_table_type
428 if ((online_analyze_table_type & reltype) == 0 ||
429 matchOid(&excludeTables, relOid) == true)
431 if (matchOid(&includeTables, relOid) == false)
438 * Do not store data about persistent table in local memory because we
439 * could not track changes of them: they could be changed by another
440 * backends. So always get a pgstat table entry.
442 if (reltype == OATT_TEMPORARY)
443 rstat = hash_search(relstats, &relOid, HASH_ENTER, &found);
445 rstat = &dummyrstat; /* found == false for following if */
449 MemSet(rstat, 0, sizeof(*rstat));
450 rstat->tableid = relOid;
454 if (operation == CK_VACUUM)
456 /* force reread because vacuum could change n_tuples */
457 rstat->rereadStat = true;
460 else if (operation == CK_ANALYZE)
463 rstat->changes_since_analyze = 0;
464 rstat->analyze_timestamp = now;
466 rstat->rereadStat = true;
470 Assert(rstat->tableid == relOid);
473 /* do not reread data if it was a truncation */
474 operation != CK_TRUNCATE && operation != CK_FASTTRUNCATE &&
475 /* read for persistent table and for temp teble if it allowed */
476 (reltype == OATT_PERSISTENT || online_analyze_local_tracking == false) &&
477 /* read only for new table or we know that it's needed */
478 (newTable == true || rstat->rereadStat == true)
481 rstat->rereadStat = false;
483 tabentry = pgstat_fetch_stat_tabentry(relOid);
487 rstat->n_tuples = tabentry->n_dead_tuples + tabentry->n_live_tuples;
488 rstat->changes_since_analyze =
489 #if PG_VERSION_NUM >= 90000
490 tabentry->changes_since_analyze;
492 tabentry->n_live_tuples + tabentry->n_dead_tuples -
493 tabentry->last_anl_tuples;
495 rstat->autovac_analyze_timestamp =
496 tabentry->autovac_analyze_timestamp;
497 rstat->analyze_timestamp = tabentry->analyze_timestamp;
502 /* force analyze after truncate, fasttruncate already did analyze */
503 operation == CK_TRUNCATE || (
504 /* do not analyze too often, if both stamps are exceeded the go */
505 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
506 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
507 /* do not analyze too small tables */
508 rstat->n_tuples + rstat->changes_since_analyze + naffected > online_analyze_lower_limit &&
509 /* be in sync with relation_needs_vacanalyze */
510 ((double)(rstat->changes_since_analyze + naffected)) >=
511 online_analyze_scale_factor * ((double)rstat->n_tuples) +
512 (double)online_analyze_threshold))
514 #if PG_VERSION_NUM < 90500
517 VacuumParams vacstmt;
519 TimestampTz startStamp, endStamp;
523 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
525 memset(&vacstmt, 0, sizeof(vacstmt));
527 vacstmt.freeze_min_age = -1;
528 vacstmt.freeze_table_age = -1; /* ??? */
530 #if PG_VERSION_NUM < 90500
531 vacstmt.type = T_VacuumStmt;
532 vacstmt.relation = NULL;
533 vacstmt.va_cols = NIL;
534 #if PG_VERSION_NUM >= 90000
535 vacstmt.options = VACOPT_ANALYZE;
536 if (online_analyze_verbose)
537 vacstmt.options |= VACOPT_VERBOSE;
539 vacstmt.vacuum = vacstmt.full = false;
540 vacstmt.analyze = true;
541 vacstmt.verbose = online_analyze_verbose;
544 vacstmt.multixact_freeze_min_age = -1;
545 vacstmt.multixact_freeze_table_age = -1;
546 vacstmt.log_min_duration = -1;
550 if (online_analyze_verbose)
551 startStamp = GetCurrentTimestamp();
553 flags = VACOPT_ANALYZE | VACOPT_NOWAIT |
554 ((online_analyze_verbose) ? VACOPT_VERBOSE : 0);
556 #if PG_VERSION_NUM >= 120000
557 vacstmt.options = flags;
560 #if PG_VERSION_NUM < 90500
562 #if PG_VERSION_NUM >= 90018
565 , GetAccessStrategy(BAS_VACUUM)
566 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
570 makeRangeVarFromOid(relOid),
571 #if PG_VERSION_NUM < 120000
574 &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
578 /* Make changes visible to subsequent calls */
579 CommandCounterIncrement();
581 if (online_analyze_verbose)
586 endStamp = GetCurrentTimestamp();
587 TimestampDifference(startStamp, endStamp, &secs, µsecs);
588 elog(INFO, "analyze \"%s\" took %.02f seconds",
589 get_rel_name(relOid),
590 ((double)secs) + ((double)microsecs)/1.0e6);
593 rstat->autovac_analyze_timestamp = now;
594 rstat->changes_since_analyze = 0;
601 rstat->n_tuples += naffected;
603 rstat->rereadStat = (reltype == OATT_PERSISTENT);
606 case CK_FASTTRUNCATE:
607 rstat->rereadStat = false;
614 /* update last analyze timestamp in local memory of backend */
617 tabentry->analyze_timestamp = now;
618 tabentry->changes_since_analyze = 0;
621 /* force reload stat for new table */
623 pgstat_clear_snapshot();
628 #if PG_VERSION_NUM >= 90000
630 tabentry->changes_since_analyze += naffected;
636 rstat->changes_since_analyze += naffected;
637 rstat->n_tuples += naffected;
640 rstat->changes_since_analyze += 2 * naffected;
641 rstat->n_tuples += naffected;
643 rstat->changes_since_analyze += naffected;
646 case CK_FASTTRUNCATE:
647 rstat->changes_since_analyze = 0;
655 /* Reset local cache if we are over limit */
656 if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
661 isFastTruncateCall(QueryDesc *queryDesc)
668 queryDesc->plannedstmt &&
669 queryDesc->operation == CMD_SELECT &&
670 queryDesc->plannedstmt->planTree &&
671 queryDesc->plannedstmt->planTree->targetlist &&
672 list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
676 te = linitial(queryDesc->plannedstmt->planTree->targetlist);
678 if (!IsA(te, TargetEntry))
681 fe = (FuncExpr*)te->expr;
684 fe && IsA(fe, FuncExpr) &&
685 fe->funcid >= FirstNormalObjectId &&
686 fe->funcretset == false &&
687 fe->funcresulttype == VOIDOID &&
688 fe->funcvariadic == false &&
689 list_length(fe->args) == 1
693 constval = linitial(fe->args);
696 IsA(constval,Const) &&
697 constval->consttype == TEXTOID &&
698 strcmp(get_func_name(fe->funcid), "fasttruncate") == 0
706 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
708 onlineAnalyzeHooker(QueryDesc *queryDesc)
710 int64 naffected = -1;
713 if (queryDesc->estate)
714 naffected = queryDesc->estate->es_processed;
716 #if PG_VERSION_NUM >= 90200
717 if (online_analyze_enable &&
718 (constval = isFastTruncateCall(queryDesc)) != NULL)
720 Datum tblnamed = constval->constvalue;
721 char *tblname = text_to_cstring(DatumGetTextP(tblnamed));
723 makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
725 makeAnalyze(RangeVarGetRelid(tblvar,
728 CK_FASTTRUNCATE, -1);
732 if (online_analyze_enable && queryDesc->plannedstmt &&
733 (queryDesc->operation == CMD_INSERT ||
734 queryDesc->operation == CMD_UPDATE ||
735 queryDesc->operation == CMD_DELETE
736 #if PG_VERSION_NUM < 90200
737 || (queryDesc->operation == CMD_SELECT &&
738 queryDesc->plannedstmt->intoClause)
742 #if PG_VERSION_NUM < 90200
743 if (queryDesc->operation == CMD_SELECT)
745 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
747 makeAnalyze(relOid, queryDesc->operation, naffected);
751 if (queryDesc->plannedstmt->resultRelations &&
752 queryDesc->plannedstmt->rtable)
756 foreach(l, queryDesc->plannedstmt->resultRelations)
758 int n = lfirst_int(l);
759 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
761 if (rte->rtekind == RTE_RELATION)
762 makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
767 if (oldExecutorEndHook)
768 oldExecutorEndHook(queryDesc);
770 standard_ExecutorEnd(queryDesc);
773 static List *toremove = NIL;
776 * removeTable called on transaction end, see call RegisterXactCallback() below
779 removeTable(XactEvent event, void *arg)
785 case XACT_EVENT_COMMIT:
787 case XACT_EVENT_ABORT:
793 foreach(cell, toremove)
795 Oid relOid = lfirst_oid(cell);
797 hash_search(relstats, &relOid, HASH_REMOVE, NULL);
803 #if PG_VERSION_NUM >= 120000
805 parse_vacuum_opt(VacuumStmt *vacstmt)
807 int options = vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE;
810 foreach(lc, vacstmt->options)
812 DefElem *opt = (DefElem *) lfirst(lc);
814 /* Parse common options for VACUUM and ANALYZE */
815 if (strcmp(opt->defname, "verbose") == 0)
816 options |= VACOPT_VERBOSE;
817 else if (strcmp(opt->defname, "skip_locked") == 0)
818 options |= VACOPT_SKIP_LOCKED;
819 else if (strcmp(opt->defname, "analyze") == 0)
820 options |= VACOPT_ANALYZE;
821 else if (strcmp(opt->defname, "freeze") == 0)
822 options |= VACOPT_FREEZE;
823 else if (strcmp(opt->defname, "full") == 0)
824 options |= VACOPT_FULL;
825 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
826 options |= VACOPT_DISABLE_PAGE_SKIPPING;
834 #if PG_VERSION_NUM >= 90200
836 onlineAnalyzeHookerUtility(
837 #if PG_VERSION_NUM >= 100000
842 const char *queryString,
843 #if PG_VERSION_NUM >= 90300
844 ProcessUtilityContext context, ParamListInfo params,
845 #if PG_VERSION_NUM >= 100000
846 QueryEnvironment *queryEnv,
849 ParamListInfo params, bool isTopLevel,
851 DestReceiver *dest, char *completionTag) {
852 List *tblnames = NIL;
853 CmdKind op = CK_INSERT;
854 #if PG_VERSION_NUM >= 100000
855 Node *parsetree = NULL;
857 if (pstmt->commandType == CMD_UTILITY)
858 parsetree = pstmt->utilityStmt;
861 if (parsetree && online_analyze_enable)
863 if (IsA(parsetree, CreateTableAsStmt) &&
864 ((CreateTableAsStmt*)parsetree)->into)
867 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
870 else if (IsA(parsetree, TruncateStmt))
872 tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
875 else if (IsA(parsetree, DropStmt) &&
876 ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
880 foreach(cell, ((DropStmt*)parsetree)->objects)
882 List *relname = (List *) lfirst(cell);
883 RangeVar *rel = makeRangeVarFromNameList(relname);
884 Oid relOid = RangeVarGetRelid(rel, NoLock, true);
886 if (OidIsValid(relOid))
890 ctx = MemoryContextSwitchTo(TopTransactionContext);
891 toremove = lappend_oid(toremove, relOid);
892 MemoryContextSwitchTo(ctx);
896 else if (IsA(parsetree, VacuumStmt))
898 VacuumStmt *vac = (VacuumStmt*)parsetree;
900 #if PG_VERSION_NUM >= 120000
901 parse_vacuum_opt(vac)
908 #if PG_VERSION_NUM >= 110000
909 tblnames = vac->rels;
912 tblnames = list_make1(vac->relation);
915 if (options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
917 /* optionally with analyze */
920 /* drop all collected stat */
924 else if (options & VACOPT_ANALYZE)
928 /* should reset all counters */
932 OnlineAnalyzeTableStat *rstat;
933 TimestampTz now = GetCurrentTimestamp();
935 hash_seq_init(&hs, relstats);
937 while((rstat = hash_seq_search(&hs)) != NULL)
939 rstat->changes_since_analyze = 0;
940 rstat->analyze_timestamp = now;
949 #if PG_VERSION_NUM >= 100000
950 #define parsetree pstmt
953 if (oldProcessUtilityHook)
954 oldProcessUtilityHook(parsetree, queryString,
955 #if PG_VERSION_NUM >= 90300
957 #if PG_VERSION_NUM >= 100000
963 dest, completionTag);
965 standard_ProcessUtility(parsetree, queryString,
966 #if PG_VERSION_NUM >= 90300
968 #if PG_VERSION_NUM >= 100000
974 dest, completionTag);
976 #if PG_VERSION_NUM >= 100000
986 #if PG_VERSION_NUM >= 110000
987 (IsA(lfirst(l), VacuumRelation)) ?
988 ((VacuumRelation*)lfirst(l))->relation :
990 (RangeVar*)lfirst(l);
993 Assert(IsA(tblname, RangeVar));
995 tblOid = RangeVarGetRelid(tblname, NoLock, true);
996 makeAnalyze(tblOid, op, -1);
1009 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1011 hash_ctl.hash = oid_hash;
1012 flags |= HASH_FUNCTION;
1014 if (onlineAnalyzeMemoryContext)
1016 Assert(relstats != NULL);
1017 MemoryContextReset(onlineAnalyzeMemoryContext);
1021 Assert(relstats == NULL);
1023 #if PG_VERSION_NUM < 90600
1024 onlineAnalyzeMemoryContext =
1025 AllocSetContextCreate(CacheMemoryContext,
1026 "online_analyze storage context",
1027 ALLOCSET_DEFAULT_MINSIZE,
1028 ALLOCSET_DEFAULT_INITSIZE,
1029 ALLOCSET_DEFAULT_MAXSIZE
1032 onlineAnalyzeMemoryContext =
1033 AllocSetContextCreate(CacheMemoryContext,
1034 "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1038 hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1039 flags |= HASH_CONTEXT;
1041 hash_ctl.keysize = sizeof(Oid);
1043 hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1046 relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1049 void _PG_init(void);
1055 oldExecutorEndHook = ExecutorEnd_hook;
1057 ExecutorEnd_hook = onlineAnalyzeHooker;
1059 #if PG_VERSION_NUM >= 90200
1060 oldProcessUtilityHook = ProcessUtility_hook;
1062 ProcessUtility_hook = onlineAnalyzeHookerUtility;
1066 DefineCustomBoolVariable(
1067 "online_analyze.enable",
1068 "Enable on-line analyze",
1069 "Enables analyze of table directly after insert/update/delete/select into",
1070 &online_analyze_enable,
1071 #if PG_VERSION_NUM >= 80400
1072 online_analyze_enable,
1075 #if PG_VERSION_NUM >= 80400
1077 #if PG_VERSION_NUM >= 90100
1085 DefineCustomBoolVariable(
1086 "online_analyze.local_tracking",
1087 "Per backend tracking",
1088 "Per backend tracking for temp tables (do not use system statistic)",
1089 &online_analyze_local_tracking,
1090 #if PG_VERSION_NUM >= 80400
1091 online_analyze_local_tracking,
1094 #if PG_VERSION_NUM >= 80400
1096 #if PG_VERSION_NUM >= 90100
1104 DefineCustomBoolVariable(
1105 "online_analyze.verbose",
1106 "Verbosity of on-line analyze",
1107 "Make ANALYZE VERBOSE after table's changes",
1108 &online_analyze_verbose,
1109 #if PG_VERSION_NUM >= 80400
1110 online_analyze_verbose,
1113 #if PG_VERSION_NUM >= 80400
1115 #if PG_VERSION_NUM >= 90100
1123 DefineCustomRealVariable(
1124 "online_analyze.scale_factor",
1125 "fraction of table size to start on-line analyze",
1126 "fraction of table size to start on-line analyze",
1127 &online_analyze_scale_factor,
1128 #if PG_VERSION_NUM >= 80400
1129 online_analyze_scale_factor,
1134 #if PG_VERSION_NUM >= 80400
1136 #if PG_VERSION_NUM >= 90100
1144 DefineCustomIntVariable(
1145 "online_analyze.threshold",
1146 "min number of row updates before on-line analyze",
1147 "min number of row updates before on-line analyze",
1148 &online_analyze_threshold,
1149 #if PG_VERSION_NUM >= 80400
1150 online_analyze_threshold,
1155 #if PG_VERSION_NUM >= 80400
1157 #if PG_VERSION_NUM >= 90100
1165 DefineCustomIntVariable(
1166 "online_analyze.capacity_threshold",
1167 "Max local cache table capacity",
1168 "Max local cache table capacity",
1169 &online_analyze_capacity_threshold,
1170 #if PG_VERSION_NUM >= 80400
1171 online_analyze_capacity_threshold,
1176 #if PG_VERSION_NUM >= 80400
1178 #if PG_VERSION_NUM >= 90100
1186 DefineCustomRealVariable(
1187 "online_analyze.min_interval",
1188 "minimum time interval between analyze call (in milliseconds)",
1189 "minimum time interval between analyze call (in milliseconds)",
1190 &online_analyze_min_interval,
1191 #if PG_VERSION_NUM >= 80400
1192 online_analyze_min_interval,
1197 #if PG_VERSION_NUM >= 80400
1199 #if PG_VERSION_NUM >= 90100
1207 DefineCustomEnumVariable(
1208 "online_analyze.table_type",
1209 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1211 &online_analyze_table_type,
1212 #if PG_VERSION_NUM >= 80400
1213 online_analyze_table_type,
1215 online_analyze_table_type_options,
1217 #if PG_VERSION_NUM >= 80400
1219 #if PG_VERSION_NUM >= 90100
1227 DefineCustomStringVariable(
1228 "online_analyze.exclude_tables",
1229 "List of tables which will not online analyze",
1231 &excludeTables.tableStr,
1232 #if PG_VERSION_NUM >= 80400
1237 #if PG_VERSION_NUM >= 90100
1239 excludeTablesAssign,
1241 excludeTablesAssign,
1246 DefineCustomStringVariable(
1247 "online_analyze.include_tables",
1248 "List of tables which will online analyze",
1250 &includeTables.tableStr,
1251 #if PG_VERSION_NUM >= 80400
1256 #if PG_VERSION_NUM >= 90100
1258 includeTablesAssign,
1260 includeTablesAssign,
1265 DefineCustomIntVariable(
1266 "online_analyze.lower_limit",
1267 "min number of rows in table to analyze",
1268 "min number of rows in table to analyze",
1269 &online_analyze_lower_limit,
1270 #if PG_VERSION_NUM >= 80400
1271 online_analyze_lower_limit,
1276 #if PG_VERSION_NUM >= 80400
1278 #if PG_VERSION_NUM >= 90100
1286 RegisterXactCallback(removeTable, NULL);
1289 void _PG_fini(void);
1293 ExecutorEnd_hook = oldExecutorEndHook;
1294 #if PG_VERSION_NUM >= 90200
1295 ProcessUtility_hook = oldProcessUtilityHook;
1298 if (excludeTables.tables)
1299 free(excludeTables.tables);
1300 if (includeTables.tables)
1301 free(includeTables.tables);
1303 excludeTables.tables = includeTables.tables = NULL;
1304 excludeTables.nTables = includeTables.nTables = 0;