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"
51 #ifdef PG_MODULE_MAGIC
55 static bool online_analyze_enable = true;
56 static bool online_analyze_verbose = true;
57 static double online_analyze_scale_factor = 0.1;
58 static int online_analyze_threshold = 50;
59 static double online_analyze_min_interval = 10000;
61 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
62 #if PG_VERSION_NUM >= 90200
63 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
69 OATT_PERSISTENT = 0x01,
70 OATT_TEMPORARY = 0x02,
72 } OnlineAnalyzeTableType;
74 static const struct config_enum_entry online_analyze_table_type_options[] =
76 {"all", OATT_ALL, false},
77 {"persistent", OATT_PERSISTENT, false},
78 {"temporary", OATT_TEMPORARY, false},
79 {"none", OATT_NONE, false},
83 static int online_analyze_table_type = (int)OATT_ALL;
85 typedef struct TableList {
91 static TableList excludeTables = {0, NULL, NULL};
92 static TableList includeTables = {0, NULL, NULL};
95 oid_cmp(const void *a, const void *b)
97 if (*(Oid*)a == *(Oid*)b)
99 return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
103 tableListAssign(const char * newval, bool doit, TableList *tbl)
112 rawname = pstrdup(newval);
114 if (!SplitIdentifierString(rawname, ',', &namelist))
119 nOids = list_length(namelist);
120 newOids = malloc(sizeof(Oid) * (nOids+1));
122 elog(ERROR,"could not allocate %d bytes", (int)(sizeof(Oid) * (nOids+1)));
127 char *curname = (char *) lfirst(l);
128 #if PG_VERSION_NUM >= 90200
129 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
132 Oid relOid = RangeVarGetRelid(makeRangeVarFromNameList(stringToQualifiedNameList(curname)),
136 if (relOid == InvalidOid)
138 #if PG_VERSION_NUM >= 90100
141 elog(WARNING,"'%s' does not exist", curname);
144 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
146 #if PG_VERSION_NUM >= 90100
149 elog(WARNING,"'%s' is not an table", curname);
154 newOids[i++] = relOid;
163 tbl->tables = newOids;
164 if (tbl->nTables > 1)
165 qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
181 #if PG_VERSION_NUM >= 90100
183 excludeTablesCheck(char **newval, void **extra, GucSource source)
187 val = (char*)tableListAssign(*newval, false, &excludeTables);
199 excludeTablesAssign(const char *newval, void *extra)
201 tableListAssign(newval, true, &excludeTables);
205 includeTablesCheck(char **newval, void **extra, GucSource source)
209 val = (char*)tableListAssign(*newval, false, &includeTables);
221 includeTablesAssign(const char *newval, void *extra)
223 tableListAssign(newval, true, &excludeTables);
226 #else /* PG_VERSION_NUM < 90100 */
229 excludeTablesAssign(const char * newval, bool doit, GucSource source)
231 return tableListAssign(newval, doit, &excludeTables);
235 includeTablesAssign(const char * newval, bool doit, GucSource source)
237 return tableListAssign(newval, doit, &includeTables);
243 tableListShow(TableList *tbl)
249 len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
250 ptr = val = palloc(len);
252 for(i=0; i<tbl->nTables; i++)
254 char *relname = get_rel_name(tbl->tables[i]);
255 Oid nspOid = get_rel_namespace(tbl->tables[i]);
256 char *nspname = get_namespace_name(nspOid);
258 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
261 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
270 excludeTablesShow(void)
272 return tableListShow(&excludeTables);
276 includeTablesShow(void)
278 return tableListShow(&includeTables);
282 matchOid(TableList *tbl, Oid oid)
284 Oid *StopLow = tbl->tables,
285 *StopHigh = tbl->tables + tbl->nTables,
288 /* Loop invariant: StopLow <= val < StopHigh */
289 while (StopLow < StopHigh)
291 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
293 if (*StopMiddle == oid)
295 else if (*StopMiddle < oid)
296 StopLow = StopMiddle + 1;
298 StopHigh = StopMiddle;
305 makeAnalyze(Oid relOid, CmdType operation, uint32 naffected)
307 PgStat_StatTabEntry *tabentry;
308 TimestampTz now = GetCurrentTimestamp();
310 if (relOid == InvalidOid)
313 if (get_rel_relkind(relOid) != RELKIND_RELATION)
316 tabentry = pgstat_fetch_stat_tabentry(relOid);
318 #if PG_VERSION_NUM >= 90000
319 #define changes_since_analyze(t) ((t)->changes_since_analyze)
321 #define changes_since_analyze(t) ((t)->n_live_tuples + (t)->n_dead_tuples - (t)->last_anl_tuples)
325 tabentry == NULL /* a new table */ ||
327 /* do not analyze too often, if both stamps are exceeded the go */
328 TimestampDifferenceExceeds(tabentry->analyze_timestamp, now, online_analyze_min_interval) &&
329 TimestampDifferenceExceeds(tabentry->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
330 /* be in sync with relation_needs_vacanalyze */
331 ((double)(changes_since_analyze(tabentry) + naffected)) >=
332 online_analyze_scale_factor * ((double)(tabentry->n_dead_tuples + tabentry->n_live_tuples)) +
333 (double)online_analyze_threshold
338 TimestampTz startStamp, endStamp;
340 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
343 * includeTables overwrites excludeTables
345 switch(online_analyze_table_type)
348 if (matchOid(&excludeTables, relOid) == true && matchOid(&includeTables, relOid) == false)
352 if (matchOid(&includeTables, relOid) == false)
356 case OATT_PERSISTENT:
360 OnlineAnalyzeTableType reltype;
362 rel = RelationIdGetRelation(relOid);
364 #if PG_VERSION_NUM >= 90100
365 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
367 (rel->rd_istemp || rel->rd_islocaltemp)
369 ? OATT_TEMPORARY : OATT_PERSISTENT;
373 * skip analyze if relation's type doesn't not match online_analyze_table_type
375 if ((online_analyze_table_type & reltype) == 0 || matchOid(&excludeTables, relOid) == true)
377 if (matchOid(&includeTables, relOid) == false)
384 vacstmt.type = T_VacuumStmt;
385 vacstmt.freeze_min_age = -1;
386 vacstmt.freeze_table_age = -1; /* ??? */
387 vacstmt.relation = NULL;
388 vacstmt.va_cols = NIL;
390 #if PG_VERSION_NUM >= 90000
391 vacstmt.options = VACOPT_ANALYZE;
392 if (online_analyze_verbose)
393 vacstmt.options |= VACOPT_VERBOSE;
395 vacstmt.vacuum = vacstmt.full = false;
396 vacstmt.analyze = true;
397 vacstmt.verbose = online_analyze_verbose;
400 if (online_analyze_verbose)
401 startStamp = GetCurrentTimestamp();
403 analyze_rel(relOid, &vacstmt, GetAccessStrategy(BAS_VACUUM)
404 #if (PG_VERSION_NUM < 90004) && (PG_VERSION_NUM >= 90000)
409 if (online_analyze_verbose)
414 endStamp = GetCurrentTimestamp();
415 TimestampDifference(startStamp, endStamp, &secs, µsecs);
416 elog(INFO, "analyze \"%s\" took %.02f seconds",
417 get_rel_name(relOid), ((double)secs) + ((double)microsecs)/1.0e6);
421 if (tabentry == NULL)
424 pgstat_clear_snapshot();
428 /* update last analyze timestamp in local memory of backend */
429 tabentry->analyze_timestamp = now;
432 #if PG_VERSION_NUM >= 90000
433 else if (tabentry != NULL)
435 tabentry->changes_since_analyze += naffected;
440 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
442 onlineAnalyzeHooker(QueryDesc *queryDesc)
444 uint32 naffected = 0;
446 if (queryDesc->estate)
447 naffected = queryDesc->estate->es_processed;
449 if (online_analyze_enable && queryDesc->plannedstmt &&
450 (queryDesc->operation == CMD_INSERT ||
451 queryDesc->operation == CMD_UPDATE ||
452 queryDesc->operation == CMD_DELETE
453 #if PG_VERSION_NUM < 90200
454 || (queryDesc->operation == CMD_SELECT && queryDesc->plannedstmt->intoClause)
458 #if PG_VERSION_NUM < 90200
459 if (queryDesc->operation == CMD_SELECT)
461 Oid relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
463 makeAnalyze(relOid, queryDesc->operation, naffected);
467 if (queryDesc->plannedstmt->resultRelations &&
468 queryDesc->plannedstmt->rtable)
472 foreach(l, queryDesc->plannedstmt->resultRelations)
474 int n = lfirst_int(l);
475 RangeTblEntry *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
477 if (rte->rtekind == RTE_RELATION)
478 makeAnalyze(rte->relid, queryDesc->operation, naffected);
483 if (oldExecutorEndHook)
484 oldExecutorEndHook(queryDesc);
486 standard_ExecutorEnd(queryDesc);
489 #if PG_VERSION_NUM >= 90200
491 onlineAnalyzeHookerUtility(Node *parsetree, const char *queryString,
492 #if PG_VERSION_NUM >= 90300
493 ProcessUtilityContext context, ParamListInfo params,
495 ParamListInfo params, bool isTopLevel,
497 DestReceiver *dest, char *completionTag) {
498 RangeVar *tblname = NULL;
500 if (IsA(parsetree, CreateTableAsStmt) && ((CreateTableAsStmt*)parsetree)->into)
501 tblname = (RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel);
503 if (oldProcessUtilityHook)
504 oldProcessUtilityHook(parsetree, queryString,
505 #if PG_VERSION_NUM >= 90300
510 dest, completionTag);
512 standard_ProcessUtility(parsetree, queryString,
513 #if PG_VERSION_NUM >= 90300
518 dest, completionTag);
521 Oid tblOid = RangeVarGetRelid(tblname, NoLock, true);
523 makeAnalyze(tblOid, CMD_INSERT, 0);
532 oldExecutorEndHook = ExecutorEnd_hook;
534 ExecutorEnd_hook = onlineAnalyzeHooker;
536 #if PG_VERSION_NUM >= 90200
537 oldProcessUtilityHook = ProcessUtility_hook;
539 ProcessUtility_hook = onlineAnalyzeHookerUtility;
543 DefineCustomBoolVariable(
544 "online_analyze.enable",
545 "Enable on-line analyze",
546 "Enables analyze of table directly after insert/update/delete/select into",
547 &online_analyze_enable,
548 #if PG_VERSION_NUM >= 80400
549 online_analyze_enable,
552 #if PG_VERSION_NUM >= 80400
554 #if PG_VERSION_NUM >= 90100
562 DefineCustomBoolVariable(
563 "online_analyze.verbose",
564 "Verbosity of on-line analyze",
565 "Make ANALYZE VERBOSE after table's changes",
566 &online_analyze_verbose,
567 #if PG_VERSION_NUM >= 80400
568 online_analyze_verbose,
571 #if PG_VERSION_NUM >= 80400
573 #if PG_VERSION_NUM >= 90100
581 DefineCustomRealVariable(
582 "online_analyze.scale_factor",
583 "fraction of table size to start on-line analyze",
584 "fraction of table size to start on-line analyze",
585 &online_analyze_scale_factor,
586 #if PG_VERSION_NUM >= 80400
587 online_analyze_scale_factor,
592 #if PG_VERSION_NUM >= 80400
594 #if PG_VERSION_NUM >= 90100
602 DefineCustomIntVariable(
603 "online_analyze.threshold",
604 "min number of row updates before on-line analyze",
605 "min number of row updates before on-line analyze",
606 &online_analyze_threshold,
607 #if PG_VERSION_NUM >= 80400
608 online_analyze_threshold,
613 #if PG_VERSION_NUM >= 80400
615 #if PG_VERSION_NUM >= 90100
623 DefineCustomRealVariable(
624 "online_analyze.min_interval",
625 "minimum time interval between analyze call (in milliseconds)",
626 "minimum time interval between analyze call (in milliseconds)",
627 &online_analyze_min_interval,
628 #if PG_VERSION_NUM >= 80400
629 online_analyze_min_interval,
634 #if PG_VERSION_NUM >= 80400
636 #if PG_VERSION_NUM >= 90100
644 DefineCustomEnumVariable(
645 "online_analyze.table_type",
646 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
648 &online_analyze_table_type,
649 #if PG_VERSION_NUM >= 80400
650 online_analyze_table_type,
652 online_analyze_table_type_options,
654 #if PG_VERSION_NUM >= 80400
656 #if PG_VERSION_NUM >= 90100
664 DefineCustomStringVariable(
665 "online_analyze.exclude_tables",
666 "List of tables which will not online analyze",
668 &excludeTables.tableStr,
669 #if PG_VERSION_NUM >= 80400
674 #if PG_VERSION_NUM >= 90100
683 DefineCustomStringVariable(
684 "online_analyze.include_tables",
685 "List of tables which will online analyze",
687 &includeTables.tableStr,
688 #if PG_VERSION_NUM >= 80400
693 #if PG_VERSION_NUM >= 90100
707 ExecutorEnd_hook = oldExecutorEndHook;
708 #if PG_VERSION_NUM >= 90200
709 ProcessUtility_hook = oldProcessUtilityHook;
712 if (excludeTables.tables)
713 free(excludeTables.tables);
714 if (includeTables.tables)
715 free(includeTables.tables);
717 excludeTables.tables = includeTables.tables = NULL;
718 excludeTables.nTables = includeTables.nTables = 0;