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/lsyscache.h"
41 #include "utils/guc.h"
42 #if PG_VERSION_NUM >= 90200
43 #include "catalog/pg_class.h"
44 #include "nodes/primnodes.h"
45 #include "tcop/utility.h"
46 #include "utils/rel.h"
47 #include "utils/relcache.h"
48 #include "utils/timestamp.h"
49 #if PG_VERSION_NUM >= 90500
50 #include "nodes/makefuncs.h"
54 #ifdef PG_MODULE_MAGIC
58 static bool online_analyze_enable = true;
59 static bool online_analyze_verbose = true;
60 static double online_analyze_scale_factor = 0.1;
61 static int online_analyze_threshold = 50;
62 static double online_analyze_min_interval = 10000;
64 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
65 #if PG_VERSION_NUM >= 90200
66 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
72 OATT_PERSISTENT = 0x01,
73 OATT_TEMPORARY = 0x02,
75 } OnlineAnalyzeTableType;
77 static const struct config_enum_entry online_analyze_table_type_options[] =
79 {"all", OATT_ALL, false},
80 {"persistent", OATT_PERSISTENT, false},
81 {"temporary", OATT_TEMPORARY, false},
82 {"none", OATT_NONE, false},
86 static int online_analyze_table_type = (int)OATT_ALL;
88 typedef struct TableList {
94 static TableList excludeTables = {0, NULL, NULL};
95 static TableList includeTables = {0, NULL, NULL};
98 oid_cmp(const void *a, const void *b)
100 if (*(Oid*)a == *(Oid*)b)
102 return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
106 tableListAssign(const char * newval, bool doit, TableList *tbl)
115 rawname = pstrdup(newval);
117 if (!SplitIdentifierString(rawname, ',', &namelist))
122 nOids = list_length(namelist);
123 newOids = malloc(sizeof(Oid) * (nOids+1));
125 elog(ERROR,"could not allocate %d bytes",
126 (int)(sizeof(Oid) * (nOids+1)));
131 char *curname = (char *) lfirst(l);
132 #if PG_VERSION_NUM >= 90200
133 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
134 stringToQualifiedNameList(curname)), NoLock, true);
136 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(
137 stringToQualifiedNameList(curname)), true);
140 if (relOid == InvalidOid)
142 #if PG_VERSION_NUM >= 90100
145 elog(WARNING,"'%s' does not exist", curname);
148 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
150 #if PG_VERSION_NUM >= 90100
153 elog(WARNING,"'%s' is not an table", curname);
158 newOids[i++] = relOid;
167 tbl->tables = newOids;
168 if (tbl->nTables > 1)
169 qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
185 #if PG_VERSION_NUM >= 90100
187 excludeTablesCheck(char **newval, void **extra, GucSource source)
191 val = (char*)tableListAssign(*newval, false, &excludeTables);
203 excludeTablesAssign(const char *newval, void *extra)
205 tableListAssign(newval, true, &excludeTables);
209 includeTablesCheck(char **newval, void **extra, GucSource source)
213 val = (char*)tableListAssign(*newval, false, &includeTables);
225 includeTablesAssign(const char *newval, void *extra)
227 tableListAssign(newval, true, &excludeTables);
230 #else /* PG_VERSION_NUM < 90100 */
233 excludeTablesAssign(const char * newval, bool doit, GucSource source)
235 return tableListAssign(newval, doit, &excludeTables);
239 includeTablesAssign(const char * newval, bool doit, GucSource source)
241 return tableListAssign(newval, doit, &includeTables);
247 tableListShow(TableList *tbl)
253 len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
254 ptr = val = palloc(len);
256 for(i=0; i<tbl->nTables; i++)
258 char *relname = get_rel_name(tbl->tables[i]);
259 Oid nspOid = get_rel_namespace(tbl->tables[i]);
260 char *nspname = get_namespace_name(nspOid);
262 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
265 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
274 excludeTablesShow(void)
276 return tableListShow(&excludeTables);
280 includeTablesShow(void)
282 return tableListShow(&includeTables);
286 matchOid(TableList *tbl, Oid oid)
288 Oid *StopLow = tbl->tables,
289 *StopHigh = tbl->tables + tbl->nTables,
292 /* Loop invariant: StopLow <= val < StopHigh */
293 while (StopLow < StopHigh)
295 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
297 if (*StopMiddle == oid)
299 else if (*StopMiddle < oid)
300 StopLow = StopMiddle + 1;
302 StopHigh = StopMiddle;
308 #if PG_VERSION_NUM >= 90500
310 makeRangeVarFromOid(Oid relOid)
313 get_namespace_name(get_rel_namespace(relOid)),
314 get_rel_name(relOid),
322 makeAnalyze(Oid relOid, CmdType operation, int32 naffected)
324 PgStat_StatTabEntry *tabentry;
325 TimestampTz now = GetCurrentTimestamp();
327 OnlineAnalyzeTableType reltype;
329 if (relOid == InvalidOid)
333 /* return if there is not changes */
335 else if (naffected < 0)
336 /* number if affected rows is unknown */
339 rel = RelationIdGetRelation(relOid);
340 if (rel->rd_rel->relkind != RELKIND_RELATION)
347 #if PG_VERSION_NUM >= 90100
348 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
350 (rel->rd_istemp || rel->rd_islocaltemp)
352 ? OATT_TEMPORARY : OATT_PERSISTENT;
357 * includeTables overwrites excludeTables
359 switch(online_analyze_table_type)
362 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
363 (matchOid(&excludeTables, relOid) == true &&
364 matchOid(&includeTables, relOid) == false))
368 if (get_rel_relkind(relOid) != RELKIND_RELATION ||
369 matchOid(&includeTables, relOid) == false)
373 case OATT_PERSISTENT:
376 * skip analyze if relation's type doesn't not match
377 * online_analyze_table_type
379 if ((online_analyze_table_type & reltype) == 0 ||
380 matchOid(&excludeTables, relOid) == true)
382 if (matchOid(&includeTables, relOid) == false)
388 tabentry = pgstat_fetch_stat_tabentry(relOid);
390 #if PG_VERSION_NUM >= 90000
391 #define changes_since_analyze(t) ((t)->changes_since_analyze)
393 #define changes_since_analyze(t) ((t)->n_live_tuples + (t)->n_dead_tuples - (t)->last_anl_tuples)
397 tabentry == NULL /* a new table */ ||
399 /* do not analyze too often, if both stamps are exceeded the go */
400 TimestampDifferenceExceeds(tabentry->analyze_timestamp, now, online_analyze_min_interval) &&
401 TimestampDifferenceExceeds(tabentry->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
402 /* be in sync with relation_needs_vacanalyze */
403 ((double)(changes_since_analyze(tabentry) + naffected)) >=
404 online_analyze_scale_factor * ((double)(tabentry->n_dead_tuples + tabentry->n_live_tuples)) +
405 (double)online_analyze_threshold
409 #if PG_VERSION_NUM < 90500
412 VacuumParams vacstmt;
414 TimestampTz startStamp, endStamp;
416 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
418 memset(&vacstmt, 0, sizeof(vacstmt));
420 vacstmt.freeze_min_age = -1;
421 vacstmt.freeze_table_age = -1; /* ??? */
423 #if PG_VERSION_NUM < 90500
424 vacstmt.type = T_VacuumStmt;
425 vacstmt.relation = NULL;
426 vacstmt.va_cols = NIL;
427 #if PG_VERSION_NUM >= 90000
428 vacstmt.options = VACOPT_ANALYZE;
429 if (online_analyze_verbose)
430 vacstmt.options |= VACOPT_VERBOSE;
432 vacstmt.vacuum = vacstmt.full = false;
433 vacstmt.analyze = true;
434 vacstmt.verbose = online_analyze_verbose;
437 vacstmt.multixact_freeze_min_age = -1;
438 vacstmt.multixact_freeze_table_age = -1;
439 vacstmt.log_min_duration = -1;
442 if (online_analyze_verbose)
443 startStamp = GetCurrentTimestamp();
446 #if PG_VERSION_NUM < 90500
448 #if PG_VERSION_NUM >= 90018
451 , GetAccessStrategy(BAS_VACUUM)
452 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
456 makeRangeVarFromOid(relOid),
457 VACOPT_ANALYZE | ((online_analyze_verbose) ? VACOPT_VERBOSE : 0),
458 &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
462 if (online_analyze_verbose)
467 endStamp = GetCurrentTimestamp();
468 TimestampDifference(startStamp, endStamp, &secs, µsecs);
469 elog(INFO, "analyze \"%s\" took %.02f seconds",
470 get_rel_name(relOid),
471 ((double)secs) + ((double)microsecs)/1.0e6);
474 if (tabentry == NULL)
477 pgstat_clear_snapshot();
481 /* update last analyze timestamp in local memory of backend */
482 tabentry->analyze_timestamp = now;
485 #if PG_VERSION_NUM >= 90000
486 else if (tabentry != NULL)
488 tabentry->changes_since_analyze += naffected;
493 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
495 onlineAnalyzeHooker(QueryDesc *queryDesc)
497 uint32 naffected = -1;
499 if (queryDesc->estate)
500 naffected = queryDesc->estate->es_processed;
502 if (online_analyze_enable && queryDesc->plannedstmt &&
503 (queryDesc->operation == CMD_INSERT ||
504 queryDesc->operation == CMD_UPDATE ||
505 queryDesc->operation == CMD_DELETE
506 #if PG_VERSION_NUM < 90200
507 || (queryDesc->operation == CMD_SELECT &&
508 queryDesc->plannedstmt->intoClause)
512 #if PG_VERSION_NUM < 90200
513 if (queryDesc->operation == CMD_SELECT)
515 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
517 makeAnalyze(relOid, queryDesc->operation, naffected);
521 if (queryDesc->plannedstmt->resultRelations &&
522 queryDesc->plannedstmt->rtable)
526 foreach(l, queryDesc->plannedstmt->resultRelations)
528 int n = lfirst_int(l);
529 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
531 if (rte->rtekind == RTE_RELATION)
532 makeAnalyze(rte->relid, queryDesc->operation, naffected);
537 if (oldExecutorEndHook)
538 oldExecutorEndHook(queryDesc);
540 standard_ExecutorEnd(queryDesc);
543 #if PG_VERSION_NUM >= 90200
545 onlineAnalyzeHookerUtility(Node *parsetree, const char *queryString,
546 #if PG_VERSION_NUM >= 90300
547 ProcessUtilityContext context, ParamListInfo params,
549 ParamListInfo params, bool isTopLevel,
551 DestReceiver *dest, char *completionTag) {
552 RangeVar *tblname = NULL;
554 if (IsA(parsetree, CreateTableAsStmt) && ((CreateTableAsStmt*)parsetree)->into)
555 tblname = (RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel);
557 if (oldProcessUtilityHook)
558 oldProcessUtilityHook(parsetree, queryString,
559 #if PG_VERSION_NUM >= 90300
564 dest, completionTag);
566 standard_ProcessUtility(parsetree, queryString,
567 #if PG_VERSION_NUM >= 90300
572 dest, completionTag);
575 Oid tblOid = RangeVarGetRelid(tblname, NoLock, true);
577 makeAnalyze(tblOid, CMD_INSERT, -1);
586 oldExecutorEndHook = ExecutorEnd_hook;
588 ExecutorEnd_hook = onlineAnalyzeHooker;
590 #if PG_VERSION_NUM >= 90200
591 oldProcessUtilityHook = ProcessUtility_hook;
593 ProcessUtility_hook = onlineAnalyzeHookerUtility;
597 DefineCustomBoolVariable(
598 "online_analyze.enable",
599 "Enable on-line analyze",
600 "Enables analyze of table directly after insert/update/delete/select into",
601 &online_analyze_enable,
602 #if PG_VERSION_NUM >= 80400
603 online_analyze_enable,
606 #if PG_VERSION_NUM >= 80400
608 #if PG_VERSION_NUM >= 90100
616 DefineCustomBoolVariable(
617 "online_analyze.verbose",
618 "Verbosity of on-line analyze",
619 "Make ANALYZE VERBOSE after table's changes",
620 &online_analyze_verbose,
621 #if PG_VERSION_NUM >= 80400
622 online_analyze_verbose,
625 #if PG_VERSION_NUM >= 80400
627 #if PG_VERSION_NUM >= 90100
635 DefineCustomRealVariable(
636 "online_analyze.scale_factor",
637 "fraction of table size to start on-line analyze",
638 "fraction of table size to start on-line analyze",
639 &online_analyze_scale_factor,
640 #if PG_VERSION_NUM >= 80400
641 online_analyze_scale_factor,
646 #if PG_VERSION_NUM >= 80400
648 #if PG_VERSION_NUM >= 90100
656 DefineCustomIntVariable(
657 "online_analyze.threshold",
658 "min number of row updates before on-line analyze",
659 "min number of row updates before on-line analyze",
660 &online_analyze_threshold,
661 #if PG_VERSION_NUM >= 80400
662 online_analyze_threshold,
667 #if PG_VERSION_NUM >= 80400
669 #if PG_VERSION_NUM >= 90100
677 DefineCustomRealVariable(
678 "online_analyze.min_interval",
679 "minimum time interval between analyze call (in milliseconds)",
680 "minimum time interval between analyze call (in milliseconds)",
681 &online_analyze_min_interval,
682 #if PG_VERSION_NUM >= 80400
683 online_analyze_min_interval,
688 #if PG_VERSION_NUM >= 80400
690 #if PG_VERSION_NUM >= 90100
698 DefineCustomEnumVariable(
699 "online_analyze.table_type",
700 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
702 &online_analyze_table_type,
703 #if PG_VERSION_NUM >= 80400
704 online_analyze_table_type,
706 online_analyze_table_type_options,
708 #if PG_VERSION_NUM >= 80400
710 #if PG_VERSION_NUM >= 90100
718 DefineCustomStringVariable(
719 "online_analyze.exclude_tables",
720 "List of tables which will not online analyze",
722 &excludeTables.tableStr,
723 #if PG_VERSION_NUM >= 80400
728 #if PG_VERSION_NUM >= 90100
737 DefineCustomStringVariable(
738 "online_analyze.include_tables",
739 "List of tables which will online analyze",
741 &includeTables.tableStr,
742 #if PG_VERSION_NUM >= 80400
747 #if PG_VERSION_NUM >= 90100
761 ExecutorEnd_hook = oldExecutorEndHook;
762 #if PG_VERSION_NUM >= 90200
763 ProcessUtility_hook = oldProcessUtilityHook;
766 if (excludeTables.tables)
767 free(excludeTables.tables);
768 if (includeTables.tables)
769 free(includeTables.tables);
771 excludeTables.tables = includeTables.tables = NULL;
772 excludeTables.nTables = includeTables.nTables = 0;