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 "catalog/namespace.h"
34 #include "commands/vacuum.h"
35 #include "executor/executor.h"
36 #include "nodes/nodes.h"
37 #include "nodes/parsenodes.h"
38 #include "storage/bufmgr.h"
39 #include "utils/builtins.h"
40 #include "utils/hsearch.h"
41 #include "utils/memutils.h"
42 #include "utils/lsyscache.h"
43 #include "utils/guc.h"
44 #if PG_VERSION_NUM >= 90200
45 #include "catalog/pg_class.h"
46 #include "nodes/primnodes.h"
47 #include "tcop/utility.h"
48 #include "utils/rel.h"
49 #include "utils/relcache.h"
50 #include "utils/timestamp.h"
51 #if PG_VERSION_NUM >= 90500
52 #include "nodes/makefuncs.h"
56 #ifdef PG_MODULE_MAGIC
60 static bool online_analyze_enable = true;
61 static bool online_analyze_verbose = true;
62 static double online_analyze_scale_factor = 0.1;
63 static int online_analyze_threshold = 50;
64 static int online_analyze_capacity_threshold = 100000;
65 static double online_analyze_min_interval = 10000;
67 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
68 #if PG_VERSION_NUM >= 90200
69 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
75 OATT_PERSISTENT = 0x01,
76 OATT_TEMPORARY = 0x02,
78 } OnlineAnalyzeTableType;
80 static const struct config_enum_entry online_analyze_table_type_options[] =
82 {"all", OATT_ALL, false},
83 {"persistent", OATT_PERSISTENT, false},
84 {"temporary", OATT_TEMPORARY, false},
85 {"none", OATT_NONE, false},
89 static int online_analyze_table_type = (int)OATT_ALL;
91 typedef struct TableList {
97 static TableList excludeTables = {0, NULL, NULL};
98 static TableList includeTables = {0, NULL, NULL};
100 typedef struct OnlineAnalyzeTableStat {
103 PgStat_Counter n_tuples;
104 PgStat_Counter changes_since_analyze;
105 TimestampTz autovac_analyze_timestamp;
106 TimestampTz analyze_timestamp;
107 } OnlineAnalyzeTableStat;
109 static MemoryContext onlineAnalyzeMemoryContext = NULL;
110 static HTAB *relstats = NULL;
112 static void relstatsInit(void);
115 oid_cmp(const void *a, const void *b)
117 if (*(Oid*)a == *(Oid*)b)
119 return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
123 tableListAssign(const char * newval, bool doit, TableList *tbl)
132 rawname = pstrdup(newval);
134 if (!SplitIdentifierString(rawname, ',', &namelist))
139 nOids = list_length(namelist);
140 newOids = malloc(sizeof(Oid) * (nOids+1));
142 elog(ERROR,"could not allocate %d bytes",
143 (int)(sizeof(Oid) * (nOids+1)));
148 char *curname = (char *) lfirst(l);
149 #if PG_VERSION_NUM >= 90200
150 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
151 stringToQualifiedNameList(curname)), NoLock, true);
153 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
154 stringToQualifiedNameList(curname)), true);
157 if (relOid == InvalidOid)
159 #if PG_VERSION_NUM >= 90100
162 elog(WARNING,"'%s' does not exist", curname);
165 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
167 #if PG_VERSION_NUM >= 90100
170 elog(WARNING,"'%s' is not an table", curname);
175 newOids[i++] = relOid;
184 tbl->tables = newOids;
185 if (tbl->nTables > 1)
186 qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
202 #if PG_VERSION_NUM >= 90100
204 excludeTablesCheck(char **newval, void **extra, GucSource source)
208 val = (char*)tableListAssign(*newval, false, &excludeTables);
220 excludeTablesAssign(const char *newval, void *extra)
222 tableListAssign(newval, true, &excludeTables);
226 includeTablesCheck(char **newval, void **extra, GucSource source)
230 val = (char*)tableListAssign(*newval, false, &includeTables);
242 includeTablesAssign(const char *newval, void *extra)
244 tableListAssign(newval, true, &excludeTables);
247 #else /* PG_VERSION_NUM < 90100 */
250 excludeTablesAssign(const char * newval, bool doit, GucSource source)
252 return tableListAssign(newval, doit, &excludeTables);
256 includeTablesAssign(const char * newval, bool doit, GucSource source)
258 return tableListAssign(newval, doit, &includeTables);
264 tableListShow(TableList *tbl)
270 len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
271 ptr = val = palloc(len);
273 for(i=0; i<tbl->nTables; i++)
275 char *relname = get_rel_name(tbl->tables[i]);
276 Oid nspOid = get_rel_namespace(tbl->tables[i]);
277 char *nspname = get_namespace_name(nspOid);
279 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
282 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
291 excludeTablesShow(void)
293 return tableListShow(&excludeTables);
297 includeTablesShow(void)
299 return tableListShow(&includeTables);
303 matchOid(TableList *tbl, Oid oid)
305 Oid *StopLow = tbl->tables,
306 *StopHigh = tbl->tables + tbl->nTables,
309 /* Loop invariant: StopLow <= val < StopHigh */
310 while (StopLow < StopHigh)
312 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
314 if (*StopMiddle == oid)
316 else if (*StopMiddle < oid)
317 StopLow = StopMiddle + 1;
319 StopHigh = StopMiddle;
325 #if PG_VERSION_NUM >= 90500
327 makeRangeVarFromOid(Oid relOid)
330 get_namespace_name(get_rel_namespace(relOid)),
331 get_rel_name(relOid),
339 makeAnalyze(Oid relOid, CmdType operation, int32 naffected)
341 TimestampTz now = GetCurrentTimestamp();
343 OnlineAnalyzeTableType reltype;
346 OnlineAnalyzeTableStat *rstat,
348 PgStat_StatTabEntry *tabentry = NULL;
350 if (relOid == InvalidOid)
354 /* return if there is no changes */
356 else if (naffected < 0)
357 /* number if affected rows is unknown */
360 rel = RelationIdGetRelation(relOid);
361 if (rel->rd_rel->relkind != RELKIND_RELATION)
368 #if PG_VERSION_NUM >= 90100
369 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
371 (rel->rd_istemp || rel->rd_islocaltemp)
373 ? OATT_TEMPORARY : OATT_PERSISTENT;
378 * includeTables overwrites excludeTables
380 switch(online_analyze_table_type)
383 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
384 (matchOid(&excludeTables, relOid) == true &&
385 matchOid(&includeTables, relOid) == false))
389 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
390 matchOid(&includeTables, relOid) == false)
394 case OATT_PERSISTENT:
397 * skip analyze if relation's type doesn't not match
398 * online_analyze_table_type
400 if ((online_analyze_table_type & reltype) == 0 ||
401 matchOid(&excludeTables, relOid) == true)
403 if (matchOid(&includeTables, relOid) == false)
410 * Do not store data about persistent table in local memory because we
411 * could not track changes of them: they could be changed by another
412 * backends. So always get a pgstat table entry.
414 if (reltype == OATT_TEMPORARY)
415 rstat = hash_search(relstats, &relOid, HASH_ENTER, &found);
417 rstat = &dummyrstat; /* found == false for following if */
419 if (found == false || rstat->rereadStat == true || naffected == 0)
424 MemSet(rstat, 0, sizeof(*rstat));
425 rstat->tableid = relOid;
427 Assert(rstat->tableid == relOid);
429 tabentry = pgstat_fetch_stat_tabentry(relOid);
433 rstat->n_tuples = tabentry->n_dead_tuples + tabentry->n_live_tuples;
434 rstat->changes_since_analyze =
435 #if PG_VERSION_NUM >= 90000
436 tabentry->changes_since_analyze;
438 tabentry->n_live_tuples + tabentry->n_dead_tuples -
439 tabentry->last_anl_tuples;
441 rstat->autovac_analyze_timestamp =
442 tabentry->autovac_analyze_timestamp;
443 rstat->analyze_timestamp = tabentry->analyze_timestamp;
444 rstat->rereadStat = false;
449 rstat->rereadStat = true;
454 /* do not analyze too often, if both stamps are exceeded the go */
455 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
456 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
457 /* be in sync with relation_needs_vacanalyze */
458 ((double)(rstat->changes_since_analyze + naffected)) >=
459 online_analyze_scale_factor * ((double)rstat->n_tuples) +
460 (double)online_analyze_threshold))
462 #if PG_VERSION_NUM < 90500
465 VacuumParams vacstmt;
467 TimestampTz startStamp, endStamp;
470 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
472 memset(&vacstmt, 0, sizeof(vacstmt));
474 vacstmt.freeze_min_age = -1;
475 vacstmt.freeze_table_age = -1; /* ??? */
477 #if PG_VERSION_NUM < 90500
478 vacstmt.type = T_VacuumStmt;
479 vacstmt.relation = NULL;
480 vacstmt.va_cols = NIL;
481 #if PG_VERSION_NUM >= 90000
482 vacstmt.options = VACOPT_ANALYZE;
483 if (online_analyze_verbose)
484 vacstmt.options |= VACOPT_VERBOSE;
486 vacstmt.vacuum = vacstmt.full = false;
487 vacstmt.analyze = true;
488 vacstmt.verbose = online_analyze_verbose;
491 vacstmt.multixact_freeze_min_age = -1;
492 vacstmt.multixact_freeze_table_age = -1;
493 vacstmt.log_min_duration = -1;
496 if (online_analyze_verbose)
497 startStamp = GetCurrentTimestamp();
500 #if PG_VERSION_NUM < 90500
502 #if PG_VERSION_NUM >= 90018
505 , GetAccessStrategy(BAS_VACUUM)
506 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
510 makeRangeVarFromOid(relOid),
511 VACOPT_ANALYZE | ((online_analyze_verbose) ? VACOPT_VERBOSE : 0),
512 &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
516 if (online_analyze_verbose)
521 endStamp = GetCurrentTimestamp();
522 TimestampDifference(startStamp, endStamp, &secs, µsecs);
523 elog(INFO, "analyze \"%s\" took %.02f seconds",
524 get_rel_name(relOid),
525 ((double)secs) + ((double)microsecs)/1.0e6);
528 rstat->autovac_analyze_timestamp = now;
529 rstat->changes_since_analyze = 0;
530 rstat->rereadStat = true;
532 /* update last analyze timestamp in local memory of backend */
535 tabentry->analyze_timestamp = now;
536 tabentry->changes_since_analyze = 0;
539 /* force reload stat for new table */
541 pgstat_clear_snapshot();
546 #if PG_VERSION_NUM >= 90000
548 tabentry->changes_since_analyze += naffected;
550 rstat->changes_since_analyze += naffected;
553 /* Reset local cache if we are over limit */
554 if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
558 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
560 onlineAnalyzeHooker(QueryDesc *queryDesc)
562 uint32 naffected = -1;
564 if (queryDesc->estate)
565 naffected = queryDesc->estate->es_processed;
567 if (online_analyze_enable && queryDesc->plannedstmt &&
568 (queryDesc->operation == CMD_INSERT ||
569 queryDesc->operation == CMD_UPDATE ||
570 queryDesc->operation == CMD_DELETE
571 #if PG_VERSION_NUM < 90200
572 || (queryDesc->operation == CMD_SELECT &&
573 queryDesc->plannedstmt->intoClause)
577 #if PG_VERSION_NUM < 90200
578 if (queryDesc->operation == CMD_SELECT)
580 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
582 makeAnalyze(relOid, queryDesc->operation, naffected);
586 if (queryDesc->plannedstmt->resultRelations &&
587 queryDesc->plannedstmt->rtable)
591 foreach(l, queryDesc->plannedstmt->resultRelations)
593 int n = lfirst_int(l);
594 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
596 if (rte->rtekind == RTE_RELATION)
597 makeAnalyze(rte->relid, queryDesc->operation, naffected);
602 if (oldExecutorEndHook)
603 oldExecutorEndHook(queryDesc);
605 standard_ExecutorEnd(queryDesc);
608 #if PG_VERSION_NUM >= 90200
610 onlineAnalyzeHookerUtility(Node *parsetree, const char *queryString,
611 #if PG_VERSION_NUM >= 90300
612 ProcessUtilityContext context, ParamListInfo params,
614 ParamListInfo params, bool isTopLevel,
616 DestReceiver *dest, char *completionTag) {
617 RangeVar *tblname = NULL;
619 if (IsA(parsetree, CreateTableAsStmt) && ((CreateTableAsStmt*)parsetree)->into)
620 tblname = (RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel);
622 if (oldProcessUtilityHook)
623 oldProcessUtilityHook(parsetree, queryString,
624 #if PG_VERSION_NUM >= 90300
629 dest, completionTag);
631 standard_ProcessUtility(parsetree, queryString,
632 #if PG_VERSION_NUM >= 90300
637 dest, completionTag);
640 Oid tblOid = RangeVarGetRelid(tblname, NoLock, true);
642 makeAnalyze(tblOid, CMD_INSERT, -1);
653 MemSet(&hash_ctl, 0, sizeof(hash_ctl));
655 hash_ctl.hash = oid_hash;
656 flags |= HASH_FUNCTION;
658 if (onlineAnalyzeMemoryContext)
660 Assert(relstats != NULL);
661 MemoryContextReset(onlineAnalyzeMemoryContext);
665 Assert(relstats == NULL);
666 onlineAnalyzeMemoryContext =
667 AllocSetContextCreate(CacheMemoryContext,
668 "online_analyze storage context",
669 #if PG_VERSION_NUM < 90600
670 ALLOCSET_DEFAULT_MINSIZE,
671 ALLOCSET_DEFAULT_INITSIZE,
672 ALLOCSET_DEFAULT_MAXSIZE
674 ALLOCSET_DEFAULT_SIZES
679 hash_ctl.hcxt = onlineAnalyzeMemoryContext;
680 flags |= HASH_CONTEXT;
682 hash_ctl.keysize = sizeof(Oid);
684 hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
687 relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
696 oldExecutorEndHook = ExecutorEnd_hook;
698 ExecutorEnd_hook = onlineAnalyzeHooker;
700 #if PG_VERSION_NUM >= 90200
701 oldProcessUtilityHook = ProcessUtility_hook;
703 ProcessUtility_hook = onlineAnalyzeHookerUtility;
707 DefineCustomBoolVariable(
708 "online_analyze.enable",
709 "Enable on-line analyze",
710 "Enables analyze of table directly after insert/update/delete/select into",
711 &online_analyze_enable,
712 #if PG_VERSION_NUM >= 80400
713 online_analyze_enable,
716 #if PG_VERSION_NUM >= 80400
718 #if PG_VERSION_NUM >= 90100
726 DefineCustomBoolVariable(
727 "online_analyze.verbose",
728 "Verbosity of on-line analyze",
729 "Make ANALYZE VERBOSE after table's changes",
730 &online_analyze_verbose,
731 #if PG_VERSION_NUM >= 80400
732 online_analyze_verbose,
735 #if PG_VERSION_NUM >= 80400
737 #if PG_VERSION_NUM >= 90100
745 DefineCustomRealVariable(
746 "online_analyze.scale_factor",
747 "fraction of table size to start on-line analyze",
748 "fraction of table size to start on-line analyze",
749 &online_analyze_scale_factor,
750 #if PG_VERSION_NUM >= 80400
751 online_analyze_scale_factor,
756 #if PG_VERSION_NUM >= 80400
758 #if PG_VERSION_NUM >= 90100
766 DefineCustomIntVariable(
767 "online_analyze.threshold",
768 "min number of row updates before on-line analyze",
769 "min number of row updates before on-line analyze",
770 &online_analyze_threshold,
771 #if PG_VERSION_NUM >= 80400
772 online_analyze_threshold,
777 #if PG_VERSION_NUM >= 80400
779 #if PG_VERSION_NUM >= 90100
787 DefineCustomIntVariable(
788 "online_analyze.capacity_threshold",
789 "Max local cache table capacity",
790 "Max local cache table capacity",
791 &online_analyze_capacity_threshold,
792 #if PG_VERSION_NUM >= 80400
793 online_analyze_capacity_threshold,
798 #if PG_VERSION_NUM >= 80400
800 #if PG_VERSION_NUM >= 90100
808 DefineCustomRealVariable(
809 "online_analyze.min_interval",
810 "minimum time interval between analyze call (in milliseconds)",
811 "minimum time interval between analyze call (in milliseconds)",
812 &online_analyze_min_interval,
813 #if PG_VERSION_NUM >= 80400
814 online_analyze_min_interval,
819 #if PG_VERSION_NUM >= 80400
821 #if PG_VERSION_NUM >= 90100
829 DefineCustomEnumVariable(
830 "online_analyze.table_type",
831 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
833 &online_analyze_table_type,
834 #if PG_VERSION_NUM >= 80400
835 online_analyze_table_type,
837 online_analyze_table_type_options,
839 #if PG_VERSION_NUM >= 80400
841 #if PG_VERSION_NUM >= 90100
849 DefineCustomStringVariable(
850 "online_analyze.exclude_tables",
851 "List of tables which will not online analyze",
853 &excludeTables.tableStr,
854 #if PG_VERSION_NUM >= 80400
859 #if PG_VERSION_NUM >= 90100
868 DefineCustomStringVariable(
869 "online_analyze.include_tables",
870 "List of tables which will online analyze",
872 &includeTables.tableStr,
873 #if PG_VERSION_NUM >= 80400
878 #if PG_VERSION_NUM >= 90100
892 ExecutorEnd_hook = oldExecutorEndHook;
893 #if PG_VERSION_NUM >= 90200
894 ProcessUtility_hook = oldProcessUtilityHook;
897 if (excludeTables.tables)
898 free(excludeTables.tables);
899 if (includeTables.tables)
900 free(includeTables.tables);
902 excludeTables.tables = includeTables.tables = NULL;
903 excludeTables.nTables = includeTables.nTables = 0;