Remove _PG_fini Mikhail Litsarev
[online_analyze.git] / online_analyze.c
1 /*
2  * Copyright (c) 2011 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
28  */
29
30 #include "postgres.h"
31
32 #include "pgstat.h"
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 #endif
59 #endif
60 #endif
61
62 #ifdef PG_MODULE_MAGIC
63 PG_MODULE_MAGIC;
64 #endif
65
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;
74
75 static ExecutorEnd_hook_type oldExecutorEndHook = NULL;
76 #if PG_VERSION_NUM >= 90200
77 static ProcessUtility_hook_type oldProcessUtilityHook = NULL;
78 #endif
79
80 typedef enum CmdKind
81 {
82         CK_SELECT = CMD_SELECT,
83         CK_UPDATE = CMD_UPDATE,
84         CK_INSERT = CMD_INSERT,
85         CK_DELETE = CMD_DELETE,
86         CK_TRUNCATE,
87         CK_FASTTRUNCATE,
88         CK_CREATE,
89         CK_ANALYZE,
90         CK_VACUUM
91 } CmdKind;
92
93
94 typedef enum
95 {
96         OATT_ALL                = 0x03,
97         OATT_PERSISTENT = 0x01,
98         OATT_TEMPORARY  = 0x02,
99         OATT_NONE               = 0x00
100 } OnlineAnalyzeTableType;
101
102 static const struct config_enum_entry online_analyze_table_type_options[] =
103 {
104         {"all", OATT_ALL, false},
105         {"persistent", OATT_PERSISTENT, false},
106         {"temporary", OATT_TEMPORARY, false},
107         {"none", OATT_NONE, false},
108         {NULL, 0, false},
109 };
110
111 static int online_analyze_table_type = (int)OATT_ALL;
112
113 typedef struct TableList {
114         int             nTables;
115         Oid             *tables;
116         char    *tableStr;
117 } TableList;
118
119 static TableList excludeTables = {0, NULL, NULL};
120 static TableList includeTables = {0, NULL, NULL};
121
122 typedef struct OnlineAnalyzeTableStat {
123         Oid                             tableid;
124         bool                    rereadStat;
125         PgStat_Counter  n_tuples;
126         PgStat_Counter  changes_since_analyze;
127         TimestampTz             autovac_analyze_timestamp;
128         TimestampTz             analyze_timestamp;
129 } OnlineAnalyzeTableStat;
130
131 static  MemoryContext   onlineAnalyzeMemoryContext = NULL;
132 static  HTAB    *relstats = NULL;
133
134 static void relstatsInit(void);
135
136 #if PG_VERSION_NUM < 100000
137 static int
138 oid_cmp(const void *a, const void *b)
139 {
140         if (*(Oid*)a == *(Oid*)b)
141                 return 0;
142         return (*(Oid*)a > *(Oid*)b) ? 1 : -1;
143 }
144 #endif
145
146 static const char *
147 tableListAssign(const char * newval, bool doit, TableList *tbl)
148 {
149         char            *rawname;
150         List            *namelist;
151         ListCell        *l;
152         Oid                     *newOids = NULL;
153         int                     nOids = 0,
154                                 i = 0;
155
156         rawname = pstrdup(newval);
157
158         if (!SplitIdentifierString(rawname, ',', &namelist))
159                 goto cleanup;
160
161         if (doit)
162         {
163                 nOids = list_length(namelist);
164                 newOids = malloc(sizeof(Oid) * (nOids+1));
165                 if (!newOids)
166                         elog(ERROR,"could not allocate %d bytes",
167                                  (int)(sizeof(Oid) * (nOids+1)));
168         }
169
170         foreach(l, namelist)
171         {
172                 char    *curname = (char *) lfirst(l);
173 #if PG_VERSION_NUM >= 90200
174                 Oid             relOid = RangeVarGetRelid(makeRangeVarFromNameList(
175                                                         stringToQualifiedNameList(curname)), NoLock, true);
176 #else
177                 Oid             relOid = RangeVarGetRelid(makeRangeVarFromNameList(
178                                                         stringToQualifiedNameList(curname)), true);
179 #endif
180
181                 if (relOid == InvalidOid)
182                 {
183 #if PG_VERSION_NUM >= 90100
184                         if (doit == false)
185 #endif
186                         elog(WARNING,"'%s' does not exist", curname);
187                         continue;
188                 }
189                 else if ( get_rel_relkind(relOid) != RELKIND_RELATION )
190                 {
191 #if PG_VERSION_NUM >= 90100
192                         if (doit == false)
193 #endif
194                                 elog(WARNING,"'%s' is not an table", curname);
195                         continue;
196                 }
197                 else if (doit)
198                 {
199                         newOids[i++] = relOid;
200                 }
201         }
202
203         if (doit)
204         {
205                 tbl->nTables = i;
206                 if (tbl->tables)
207                         free(tbl->tables);
208                 tbl->tables = newOids;
209                 if (tbl->nTables > 1)
210                         qsort(tbl->tables, tbl->nTables, sizeof(tbl->tables[0]), oid_cmp);
211         }
212
213         pfree(rawname);
214         list_free(namelist);
215
216         return newval;
217
218 cleanup:
219         if (newOids)
220                 free(newOids);
221         pfree(rawname);
222         list_free(namelist);
223         return NULL;
224 }
225
226 #if PG_VERSION_NUM >= 90100
227 static bool
228 excludeTablesCheck(char **newval, void **extra, GucSource source)
229 {
230         char *val;
231
232         val = (char*)tableListAssign(*newval, false, &excludeTables);
233
234         if (val)
235         {
236                 *newval = val;
237                 return true;
238         }
239
240         return false;
241 }
242
243 static void
244 excludeTablesAssign(const char *newval, void *extra)
245 {
246         tableListAssign(newval, true, &excludeTables);
247 }
248
249 static bool
250 includeTablesCheck(char **newval, void **extra, GucSource source)
251 {
252         char *val;
253
254         val = (char*)tableListAssign(*newval, false, &includeTables);
255
256         if (val)
257         {
258                 *newval = val;
259                 return true;
260         }
261
262         return false;
263 }
264
265 static void
266 includeTablesAssign(const char *newval, void *extra)
267 {
268         tableListAssign(newval, true, &excludeTables);
269 }
270
271 #else /* PG_VERSION_NUM < 90100 */
272
273 static const char *
274 excludeTablesAssign(const char * newval, bool doit, GucSource source)
275 {
276         return tableListAssign(newval, doit, &excludeTables);
277 }
278
279 static const char *
280 includeTablesAssign(const char * newval, bool doit, GucSource source)
281 {
282         return tableListAssign(newval, doit, &includeTables);
283 }
284
285 #endif
286
287 static const char*
288 tableListShow(TableList *tbl)
289 {
290         char    *val, *ptr;
291         int             i,
292                         len;
293
294         len = 1 /* \0 */ + tbl->nTables * (2 * NAMEDATALEN + 2 /* ', ' */ + 1 /* . */);
295         ptr = val = palloc(len);
296         *ptr ='\0';
297         for(i=0; i<tbl->nTables; i++)
298         {
299                 char    *relname = get_rel_name(tbl->tables[i]);
300                 Oid             nspOid = get_rel_namespace(tbl->tables[i]);
301                 char    *nspname = get_namespace_name(nspOid);
302
303                 if ( relname == NULL || nspOid == InvalidOid || nspname == NULL )
304                         continue;
305
306                 ptr += snprintf(ptr, len - (ptr - val), "%s%s.%s",
307                                                                                                         (i==0) ? "" : ", ",
308                                                                                                         nspname, relname);
309         }
310
311         return val;
312 }
313
314 static const char*
315 excludeTablesShow(void)
316 {
317         return tableListShow(&excludeTables);
318 }
319
320 static const char*
321 includeTablesShow(void)
322 {
323         return tableListShow(&includeTables);
324 }
325
326 static bool
327 matchOid(TableList *tbl, Oid oid)
328 {
329         Oid     *StopLow = tbl->tables,
330                 *StopHigh = tbl->tables + tbl->nTables,
331                 *StopMiddle;
332
333         /* Loop invariant: StopLow <= val < StopHigh */
334         while (StopLow < StopHigh)
335         {
336                 StopMiddle = StopLow + ((StopHigh - StopLow) >> 1);
337
338                 if (*StopMiddle == oid)
339                         return true;
340                 else  if (*StopMiddle < oid)
341                         StopLow = StopMiddle + 1;
342                 else
343                         StopHigh = StopMiddle;
344         }
345
346         return false;
347 }
348
349 #if PG_VERSION_NUM >= 90500
350 static RangeVar*
351 makeRangeVarFromOid(Oid relOid)
352 {
353         return makeRangeVar(
354                                 get_namespace_name(get_rel_namespace(relOid)),
355                                 get_rel_name(relOid),
356                                 -1
357                         );
358
359 }
360 #endif
361
362 static void
363 makeAnalyze(Oid relOid, CmdKind operation, int64 naffected)
364 {
365         TimestampTz                             now = GetCurrentTimestamp();
366         Relation                                rel;
367         OnlineAnalyzeTableType  reltype;
368         bool                                    found = false,
369                                                         newTable = false;
370         OnlineAnalyzeTableStat  *rstat,
371                                                         dummyrstat;
372         PgStat_StatTabEntry             *tabentry = NULL;
373
374         if (relOid == InvalidOid)
375                 return;
376
377         if (naffected == 0)
378                 /* return if there is no changes */
379                 return;
380         else if (naffected < 0)
381                 /* number if affected rows is unknown */
382                 naffected = 0;
383
384         rel = RelationIdGetRelation(relOid);
385         if (rel->rd_rel->relkind != RELKIND_RELATION)
386         {
387                 RelationClose(rel);
388                 return;
389         }
390
391         reltype =
392 #if PG_VERSION_NUM >= 90100
393                 (rel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)
394 #else
395                 (rel->rd_istemp || rel->rd_islocaltemp)
396 #endif
397                         ? OATT_TEMPORARY : OATT_PERSISTENT;
398
399         RelationClose(rel);
400
401         /*
402          * includeTables overwrites excludeTables
403          */
404         switch(online_analyze_table_type)
405         {
406                 case OATT_ALL:
407                         if (get_rel_relkind(relOid) != RELKIND_RELATION ||
408                                 (matchOid(&excludeTables, relOid) == true &&
409                                 matchOid(&includeTables, relOid) == false))
410                                 return;
411                         break;
412                 case OATT_NONE:
413                         if (get_rel_relkind(relOid) != RELKIND_RELATION ||
414                                 matchOid(&includeTables, relOid) == false)
415                                 return;
416                         break;
417                 case OATT_TEMPORARY:
418                 case OATT_PERSISTENT:
419                 default:
420                         /*
421                          * skip analyze if relation's type doesn't not match
422                          * online_analyze_table_type
423                          */
424                         if ((online_analyze_table_type & reltype) == 0 ||
425                                 matchOid(&excludeTables, relOid) == true)
426                         {
427                                 if (matchOid(&includeTables, relOid) == false)
428                                         return;
429                         }
430                         break;
431         }
432
433         /*
434          * Do not store data about persistent table in local memory because we
435          * could not track changes of them: they could be changed by another
436          * backends. So always get a pgstat table entry.
437          */
438         if (reltype == OATT_TEMPORARY)
439                 rstat = hash_search(relstats, &relOid, HASH_ENTER, &found);
440         else
441                 rstat = &dummyrstat; /* found == false for following if */
442
443         if (!found)
444         {
445                 MemSet(rstat, 0, sizeof(*rstat));
446                 rstat->tableid = relOid;
447                 newTable = true;
448         }
449
450         if (operation == CK_VACUUM)
451         {
452                 /* force reread because vacuum could change n_tuples */
453                 rstat->rereadStat = true;
454                 return;
455         }
456         else if (operation == CK_ANALYZE)
457         {
458                 /* only analyze */
459                 rstat->changes_since_analyze = 0;
460                 rstat->analyze_timestamp = now;
461                 if (newTable)
462                         rstat->rereadStat = true;
463                 return;
464         }
465
466         Assert(rstat->tableid == relOid);
467
468         if (
469                 /* do not reread data if it was a truncation */
470                 operation != CK_TRUNCATE && operation != CK_FASTTRUNCATE &&
471                 /* read  for persistent table and for temp teble if it allowed */
472                 (reltype == OATT_PERSISTENT || online_analyze_local_tracking == false) &&
473                 /* read only for new table or we know that it's needed */
474                 (newTable == true || rstat->rereadStat == true)
475            )
476         {
477                 rstat->rereadStat = false;
478
479                 tabentry = pgstat_fetch_stat_tabentry(relOid);
480
481                 if (tabentry)
482                 {
483                         rstat->n_tuples = tabentry->n_dead_tuples + tabentry->n_live_tuples;
484                         rstat->changes_since_analyze =
485 #if PG_VERSION_NUM >= 90000
486                                 tabentry->changes_since_analyze;
487 #else
488                                 tabentry->n_live_tuples + tabentry->n_dead_tuples -
489                                         tabentry->last_anl_tuples;
490 #endif
491                         rstat->autovac_analyze_timestamp =
492                                 tabentry->autovac_analyze_timestamp;
493                         rstat->analyze_timestamp = tabentry->analyze_timestamp;
494                 }
495         }
496
497         if (newTable ||
498                 /* force analyze after truncate, fasttruncate already did analyze */
499                 operation == CK_TRUNCATE || (
500                 /* do not analyze too often, if both stamps are exceeded the go */
501                 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
502                 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
503                 /* do not analyze too small tables */
504                 rstat->n_tuples + rstat->changes_since_analyze + naffected > online_analyze_lower_limit &&
505                 /* be in sync with relation_needs_vacanalyze */
506                 ((double)(rstat->changes_since_analyze + naffected)) >=
507                          online_analyze_scale_factor * ((double)rstat->n_tuples) +
508                          (double)online_analyze_threshold))
509         {
510 #if PG_VERSION_NUM < 90500
511                 VacuumStmt                              vacstmt;
512 #else
513                 VacuumParams                    vacstmt;
514 #endif
515                 TimestampTz                             startStamp, endStamp;
516
517
518                 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
519
520                 memset(&vacstmt, 0, sizeof(vacstmt));
521
522                 vacstmt.freeze_min_age = -1;
523                 vacstmt.freeze_table_age = -1; /* ??? */
524
525 #if PG_VERSION_NUM < 90500
526                 vacstmt.type = T_VacuumStmt;
527                 vacstmt.relation = NULL;
528                 vacstmt.va_cols = NIL;
529 #if PG_VERSION_NUM >= 90000
530                 vacstmt.options = VACOPT_ANALYZE;
531                 if (online_analyze_verbose)
532                         vacstmt.options |= VACOPT_VERBOSE;
533 #else
534                 vacstmt.vacuum = vacstmt.full = false;
535                 vacstmt.analyze = true;
536                 vacstmt.verbose = online_analyze_verbose;
537 #endif
538 #else
539                 vacstmt.multixact_freeze_min_age = -1;
540                 vacstmt.multixact_freeze_table_age = -1;
541                 vacstmt.log_min_duration = -1;
542 #endif
543
544                 if (online_analyze_verbose)
545                         startStamp = GetCurrentTimestamp();
546
547                 analyze_rel(relOid,
548 #if PG_VERSION_NUM < 90500
549                         &vacstmt
550 #if PG_VERSION_NUM >= 90018
551                         , true
552 #endif
553                         , GetAccessStrategy(BAS_VACUUM)
554 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
555                         , true
556 #endif
557 #else
558                         makeRangeVarFromOid(relOid),
559                         VACOPT_ANALYZE | ((online_analyze_verbose) ? VACOPT_VERBOSE : 0),
560                         &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
561 #endif
562                 );
563
564                 /* Make changes visible to subsequent calls */
565                 CommandCounterIncrement();
566
567                 if (online_analyze_verbose)
568                 {
569                         long    secs;
570                         int             microsecs;
571
572                         endStamp = GetCurrentTimestamp();
573                         TimestampDifference(startStamp, endStamp, &secs, &microsecs);
574                         elog(INFO, "analyze \"%s\" took %.02f seconds",
575                                 get_rel_name(relOid),
576                                 ((double)secs) + ((double)microsecs)/1.0e6);
577                 }
578
579                 rstat->autovac_analyze_timestamp = now;
580                 rstat->changes_since_analyze = 0;
581
582                 switch(operation)
583                 {
584                         case CK_CREATE:
585                         case CK_INSERT:
586                         case CK_UPDATE:
587                                 rstat->n_tuples += naffected;
588                         case CK_DELETE:
589                                 rstat->rereadStat = (reltype == OATT_PERSISTENT);
590                                 break;
591                         case CK_TRUNCATE:
592                         case CK_FASTTRUNCATE:
593                                 rstat->rereadStat = false;
594                                 rstat->n_tuples = 0;
595                                 break;
596                         default:
597                                 break;
598                 }
599
600                 /* update last analyze timestamp in local memory of backend */
601                 if (tabentry)
602                 {
603                         tabentry->analyze_timestamp = now;
604                         tabentry->changes_since_analyze = 0;
605                 }
606 #if 0
607                 /* force reload stat for new table */
608                 if (newTable)
609                         pgstat_clear_snapshot();
610 #endif
611         }
612         else
613         {
614 #if PG_VERSION_NUM >= 90000
615                 if (tabentry)
616                         tabentry->changes_since_analyze += naffected;
617 #endif
618                 switch(operation)
619                 {
620                         case CK_CREATE:
621                         case CK_INSERT:
622                                 rstat->changes_since_analyze += naffected;
623                                 rstat->n_tuples += naffected;
624                                 break;
625                         case CK_UPDATE:
626                                 rstat->changes_since_analyze += 2 * naffected;
627                                 rstat->n_tuples += naffected;
628                         case CK_DELETE:
629                                 rstat->changes_since_analyze += naffected;
630                                 break;
631                         case CK_TRUNCATE:
632                         case CK_FASTTRUNCATE:
633                                 rstat->changes_since_analyze = 0;
634                                 rstat->n_tuples = 0;
635                                 break;
636                         default:
637                                 break;
638                 }
639         }
640
641         /* Reset local cache if we are over limit */
642         if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
643                 relstatsInit();
644 }
645
646 static Const*
647 isFastTruncateCall(QueryDesc *queryDesc)
648 {
649         TargetEntry     *te;
650         FuncExpr        *fe;
651         Const           *constval;
652
653         if (!(
654                   queryDesc->plannedstmt &&
655                   queryDesc->operation == CMD_SELECT &&
656                   queryDesc->plannedstmt->planTree &&
657                   queryDesc->plannedstmt->planTree->targetlist &&
658                   list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
659                  ))
660                 return NULL;
661
662         te = linitial(queryDesc->plannedstmt->planTree->targetlist);
663
664         if (!IsA(te, TargetEntry))
665                 return NULL;
666
667         fe = (FuncExpr*)te->expr;
668
669         if (!(
670                   fe && IsA(fe, FuncExpr) &&
671                   fe->funcid >= FirstNormalObjectId &&
672                   fe->funcretset == false &&
673                   fe->funcresulttype == VOIDOID &&
674                   fe->funcvariadic == false &&
675                   list_length(fe->args) == 1
676                  ))
677                 return NULL;
678
679         constval = linitial(fe->args);
680
681         if (!(
682                   IsA(constval,Const) &&
683                   constval->consttype == TEXTOID &&
684                   strcmp(get_func_name(fe->funcid), "fasttruncate") == 0
685                  ))
686                 return NULL;
687
688         return constval;
689 }
690
691
692 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
693 void
694 onlineAnalyzeHooker(QueryDesc *queryDesc)
695 {
696         int64   naffected = -1;
697         Const   *constval;
698
699         if (queryDesc->estate)
700                 naffected = queryDesc->estate->es_processed;
701
702 #if PG_VERSION_NUM >= 90200
703         if (online_analyze_enable &&
704                 (constval = isFastTruncateCall(queryDesc)) != NULL)
705         {
706                 Datum           tblnamed = constval->constvalue;
707                 char            *tblname = text_to_cstring(DatumGetTextP(tblnamed));
708                 RangeVar        *tblvar =
709                         makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
710
711                 makeAnalyze(RangeVarGetRelid(tblvar,
712                                                                          NoLock,
713                                                                          false),
714                                         CK_FASTTRUNCATE, -1);
715         }
716 #endif
717
718         if (online_analyze_enable && queryDesc->plannedstmt &&
719                         (queryDesc->operation == CMD_INSERT ||
720                          queryDesc->operation == CMD_UPDATE ||
721                          queryDesc->operation == CMD_DELETE
722 #if PG_VERSION_NUM < 90200
723                          || (queryDesc->operation == CMD_SELECT &&
724                                  queryDesc->plannedstmt->intoClause)
725 #endif
726                          ))
727         {
728 #if PG_VERSION_NUM < 90200
729                 if (queryDesc->operation == CMD_SELECT)
730                 {
731                         Oid     relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
732
733                         makeAnalyze(relOid, queryDesc->operation, naffected);
734                 }
735                 else
736 #endif
737                 if (queryDesc->plannedstmt->resultRelations &&
738                                  queryDesc->plannedstmt->rtable)
739                 {
740                         ListCell        *l;
741
742                         foreach(l, queryDesc->plannedstmt->resultRelations)
743                         {
744                                 int                             n = lfirst_int(l);
745                                 RangeTblEntry   *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
746
747                                 if (rte->rtekind == RTE_RELATION)
748                                         makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
749                         }
750                 }
751         }
752
753         if (oldExecutorEndHook)
754                 oldExecutorEndHook(queryDesc);
755         else
756                 standard_ExecutorEnd(queryDesc);
757 }
758
759 static List             *toremove = NIL;
760
761 /*
762  * removeTable called on transaction end, see call RegisterXactCallback() below
763  */
764 static void
765 removeTable(XactEvent event, void *arg)
766 {
767         ListCell        *cell;
768
769         switch(event)
770         {
771                 case XACT_EVENT_COMMIT:
772                         break;
773                 case XACT_EVENT_ABORT:
774                         toremove = NIL;
775                 default:
776                         return;
777         }
778
779         foreach(cell, toremove)
780         {
781                 Oid     relOid = lfirst_oid(cell);
782
783                 hash_search(relstats, &relOid, HASH_REMOVE, NULL);
784         }
785
786         toremove = NIL;
787 }
788
789
790 #if PG_VERSION_NUM >= 90200
791 static void
792 onlineAnalyzeHookerUtility(
793 #if PG_VERSION_NUM >= 100000
794                                                    PlannedStmt *pstmt,
795 #else
796                                                    Node *parsetree,
797 #endif
798                                                    const char *queryString,
799 #if PG_VERSION_NUM >= 90300
800                                                         ProcessUtilityContext context, ParamListInfo params,
801 #if PG_VERSION_NUM >= 100000
802                                                         QueryEnvironment *queryEnv,
803 #endif
804 #else
805                                                         ParamListInfo params, bool isTopLevel,
806 #endif
807                                                         DestReceiver *dest, char *completionTag) {
808         List            *tblnames = NIL;
809         CmdKind         op = CK_INSERT;
810 #if PG_VERSION_NUM >= 100000
811         Node            *parsetree = NULL;
812
813         if (pstmt->commandType == CMD_UTILITY)
814                 parsetree = pstmt->utilityStmt;
815 #endif
816
817         if (parsetree && online_analyze_enable)
818         {
819                 if (IsA(parsetree, CreateTableAsStmt) &&
820                         ((CreateTableAsStmt*)parsetree)->into)
821                 {
822                         tblnames =
823                                 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
824                         op = CK_CREATE;
825                 }
826                 else if (IsA(parsetree, TruncateStmt))
827                 {
828                         tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
829                         op = CK_TRUNCATE;
830                 }
831                 else if (IsA(parsetree, DropStmt) &&
832                                  ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
833                 {
834                         ListCell        *cell;
835
836                         foreach(cell, ((DropStmt*)parsetree)->objects)
837                         {
838                                 List            *relname = (List *) lfirst(cell);
839                                 RangeVar        *rel = makeRangeVarFromNameList(relname);
840                                 Oid                     relOid = RangeVarGetRelid(rel, NoLock, true);
841
842                                 if (OidIsValid(relOid))
843                                 {
844                                         MemoryContext   ctx;
845
846                                         ctx = MemoryContextSwitchTo(TopTransactionContext);
847                                         toremove = lappend_oid(toremove, relOid);
848                                         MemoryContextSwitchTo(ctx);
849                                 }
850                         }
851                 }
852                 else if (IsA(parsetree, VacuumStmt))
853                 {
854                         VacuumStmt      *vac = (VacuumStmt*)parsetree;
855
856 #if PG_VERSION_NUM >= 110000
857                         tblnames = vac->rels;
858 #else
859                         if (vac->relation)
860                                 tblnames = list_make1(vac->relation);
861 #endif
862
863                         if (vac->options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
864                         {
865                                 /* optionally with analyze */
866                                 op = CK_VACUUM;
867
868                                 /* drop all collected stat */
869                                 if (tblnames == NIL)
870                                         relstatsInit();
871                         }
872                         else if (vac->options & VACOPT_ANALYZE)
873                         {
874                                 op = CK_ANALYZE;
875
876                                 /* should reset all counters */
877                                 if (tblnames == NIL)
878                                 {
879                                         HASH_SEQ_STATUS                 hs;
880                                         OnlineAnalyzeTableStat  *rstat;
881                                         TimestampTz                             now = GetCurrentTimestamp();
882
883                                         hash_seq_init(&hs, relstats);
884
885                                         while((rstat = hash_seq_search(&hs)) != NULL)
886                                         {
887                                                 rstat->changes_since_analyze = 0;
888                                                 rstat->analyze_timestamp = now;
889                                         }
890                                 }
891                         }
892                         else
893                                 tblnames = NIL;
894                 }
895         }
896
897 #if PG_VERSION_NUM >= 100000
898 #define parsetree pstmt
899 #endif
900
901         if (oldProcessUtilityHook)
902                 oldProcessUtilityHook(parsetree, queryString,
903 #if PG_VERSION_NUM >= 90300
904                                                           context, params,
905 #if PG_VERSION_NUM >= 100000
906                                                           queryEnv,
907 #endif
908 #else
909                                                           params, isTopLevel,
910 #endif
911                                                           dest, completionTag);
912         else
913                 standard_ProcessUtility(parsetree, queryString,
914 #if PG_VERSION_NUM >= 90300
915                                                                 context, params,
916 #if PG_VERSION_NUM >= 100000
917                                                                 queryEnv,
918 #endif
919 #else
920                                                                 params, isTopLevel,
921 #endif
922                                                                 dest, completionTag);
923
924 #if PG_VERSION_NUM >= 100000
925 #undef parsetree
926 #endif
927
928         if (tblnames) {
929                 ListCell        *l;
930
931                 foreach(l, tblnames)
932                 {
933                         RangeVar        *tblname =
934 #if PG_VERSION_NUM >= 110000
935                                 (IsA(lfirst(l), VacuumRelation)) ?
936                                         ((VacuumRelation*)lfirst(l))->relation :
937 #endif
938                                         (RangeVar*)lfirst(l);
939                         Oid     tblOid;
940
941                         Assert(IsA(tblname, RangeVar));
942
943                         tblOid = RangeVarGetRelid(tblname, NoLock, true);
944                         makeAnalyze(tblOid, op, -1);
945                 }
946         }
947 }
948 #endif
949
950
951 static void
952 relstatsInit(void)
953 {
954         HASHCTL hash_ctl;
955         int             flags = 0;
956
957         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
958
959         hash_ctl.hash = oid_hash;
960         flags |= HASH_FUNCTION;
961
962         if (onlineAnalyzeMemoryContext)
963         {
964                 Assert(relstats != NULL);
965                 MemoryContextReset(onlineAnalyzeMemoryContext);
966         }
967         else
968         {
969                 Assert(relstats == NULL);
970
971 #if PG_VERSION_NUM < 90600
972                 onlineAnalyzeMemoryContext =
973                         AllocSetContextCreate(CacheMemoryContext,
974                         "online_analyze storage context",
975                         ALLOCSET_DEFAULT_MINSIZE,
976                         ALLOCSET_DEFAULT_INITSIZE,
977                         ALLOCSET_DEFAULT_MAXSIZE
978                         );
979 #else
980                 onlineAnalyzeMemoryContext =
981                         AllocSetContextCreate(CacheMemoryContext,
982                         "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
983 #endif
984         }
985
986         hash_ctl.hcxt = onlineAnalyzeMemoryContext;
987         flags |= HASH_CONTEXT;
988
989         hash_ctl.keysize = sizeof(Oid);
990
991         hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
992         flags |= HASH_ELEM;
993
994         relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
995 }
996
997 void _PG_init(void);
998 void
999 _PG_init(void)
1000 {
1001         relstatsInit();
1002
1003         oldExecutorEndHook = ExecutorEnd_hook;
1004
1005         ExecutorEnd_hook = onlineAnalyzeHooker;
1006
1007 #if PG_VERSION_NUM >= 90200
1008         oldProcessUtilityHook = ProcessUtility_hook;
1009
1010         ProcessUtility_hook = onlineAnalyzeHookerUtility;
1011 #endif
1012
1013
1014         DefineCustomBoolVariable(
1015                 "online_analyze.enable",
1016                 "Enable on-line analyze",
1017                 "Enables analyze of table directly after insert/update/delete/select into",
1018                 &online_analyze_enable,
1019 #if PG_VERSION_NUM >= 80400
1020                 online_analyze_enable,
1021 #endif
1022                 PGC_USERSET,
1023 #if PG_VERSION_NUM >= 80400
1024                 GUC_NOT_IN_SAMPLE,
1025 #if PG_VERSION_NUM >= 90100
1026                 NULL,
1027 #endif
1028 #endif
1029                 NULL,
1030                 NULL
1031         );
1032
1033         DefineCustomBoolVariable(
1034                 "online_analyze.local_tracking",
1035                 "Per backend tracking",
1036                 "Per backend tracking for temp tables (do not use system statistic)",
1037                 &online_analyze_local_tracking,
1038 #if PG_VERSION_NUM >= 80400
1039                 online_analyze_local_tracking,
1040 #endif
1041                 PGC_USERSET,
1042 #if PG_VERSION_NUM >= 80400
1043                 GUC_NOT_IN_SAMPLE,
1044 #if PG_VERSION_NUM >= 90100
1045                 NULL,
1046 #endif
1047 #endif
1048                 NULL,
1049                 NULL
1050         );
1051
1052         DefineCustomBoolVariable(
1053                 "online_analyze.verbose",
1054                 "Verbosity of on-line analyze",
1055                 "Make ANALYZE VERBOSE after table's changes",
1056                 &online_analyze_verbose,
1057 #if PG_VERSION_NUM >= 80400
1058                 online_analyze_verbose,
1059 #endif
1060                 PGC_USERSET,
1061 #if PG_VERSION_NUM >= 80400
1062                 GUC_NOT_IN_SAMPLE,
1063 #if PG_VERSION_NUM >= 90100
1064                 NULL,
1065 #endif
1066 #endif
1067                 NULL,
1068                 NULL
1069         );
1070
1071         DefineCustomRealVariable(
1072                 "online_analyze.scale_factor",
1073                 "fraction of table size to start on-line analyze",
1074                 "fraction of table size to start on-line analyze",
1075                 &online_analyze_scale_factor,
1076 #if PG_VERSION_NUM >= 80400
1077                 online_analyze_scale_factor,
1078 #endif
1079                 0.0,
1080                 1.0,
1081                 PGC_USERSET,
1082 #if PG_VERSION_NUM >= 80400
1083                 GUC_NOT_IN_SAMPLE,
1084 #if PG_VERSION_NUM >= 90100
1085                 NULL,
1086 #endif
1087 #endif
1088                 NULL,
1089                 NULL
1090         );
1091
1092         DefineCustomIntVariable(
1093                 "online_analyze.threshold",
1094                 "min number of row updates before on-line analyze",
1095                 "min number of row updates before on-line analyze",
1096                 &online_analyze_threshold,
1097 #if PG_VERSION_NUM >= 80400
1098                 online_analyze_threshold,
1099 #endif
1100                 0,
1101                 0x7fffffff,
1102                 PGC_USERSET,
1103 #if PG_VERSION_NUM >= 80400
1104                 GUC_NOT_IN_SAMPLE,
1105 #if PG_VERSION_NUM >= 90100
1106                 NULL,
1107 #endif
1108 #endif
1109                 NULL,
1110                 NULL
1111         );
1112
1113         DefineCustomIntVariable(
1114                 "online_analyze.capacity_threshold",
1115                 "Max local cache table capacity",
1116                 "Max local cache table capacity",
1117                 &online_analyze_capacity_threshold,
1118 #if PG_VERSION_NUM >= 80400
1119                 online_analyze_capacity_threshold,
1120 #endif
1121                 0,
1122                 0x7fffffff,
1123                 PGC_USERSET,
1124 #if PG_VERSION_NUM >= 80400
1125                 GUC_NOT_IN_SAMPLE,
1126 #if PG_VERSION_NUM >= 90100
1127                 NULL,
1128 #endif
1129 #endif
1130                 NULL,
1131                 NULL
1132         );
1133
1134         DefineCustomRealVariable(
1135                 "online_analyze.min_interval",
1136                 "minimum time interval between analyze call (in milliseconds)",
1137                 "minimum time interval between analyze call (in milliseconds)",
1138                 &online_analyze_min_interval,
1139 #if PG_VERSION_NUM >= 80400
1140                 online_analyze_min_interval,
1141 #endif
1142                 0.0,
1143                 1e30,
1144                 PGC_USERSET,
1145 #if PG_VERSION_NUM >= 80400
1146                 GUC_NOT_IN_SAMPLE,
1147 #if PG_VERSION_NUM >= 90100
1148                 NULL,
1149 #endif
1150 #endif
1151                 NULL,
1152                 NULL
1153         );
1154
1155         DefineCustomEnumVariable(
1156                 "online_analyze.table_type",
1157                 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1158                 NULL,
1159                 &online_analyze_table_type,
1160 #if PG_VERSION_NUM >= 80400
1161                 online_analyze_table_type,
1162 #endif
1163                 online_analyze_table_type_options,
1164                 PGC_USERSET,
1165 #if PG_VERSION_NUM >= 80400
1166                 GUC_NOT_IN_SAMPLE,
1167 #if PG_VERSION_NUM >= 90100
1168                 NULL,
1169 #endif
1170 #endif
1171                 NULL,
1172                 NULL
1173         );
1174
1175         DefineCustomStringVariable(
1176                 "online_analyze.exclude_tables",
1177                 "List of tables which will not online analyze",
1178                 NULL,
1179                 &excludeTables.tableStr,
1180 #if PG_VERSION_NUM >= 80400
1181                 "",
1182 #endif
1183                 PGC_USERSET,
1184                 0,
1185 #if PG_VERSION_NUM >= 90100
1186                 excludeTablesCheck,
1187                 excludeTablesAssign,
1188 #else
1189                 excludeTablesAssign,
1190 #endif
1191                 excludeTablesShow
1192         );
1193
1194         DefineCustomStringVariable(
1195                 "online_analyze.include_tables",
1196                 "List of tables which will online analyze",
1197                 NULL,
1198                 &includeTables.tableStr,
1199 #if PG_VERSION_NUM >= 80400
1200                 "",
1201 #endif
1202                 PGC_USERSET,
1203                 0,
1204 #if PG_VERSION_NUM >= 90100
1205                 includeTablesCheck,
1206                 includeTablesAssign,
1207 #else
1208                 includeTablesAssign,
1209 #endif
1210                 includeTablesShow
1211         );
1212
1213         DefineCustomIntVariable(
1214                 "online_analyze.lower_limit",
1215                 "min number of rows in table to analyze",
1216                 "min number of rows in table to analyze",
1217                 &online_analyze_lower_limit,
1218 #if PG_VERSION_NUM >= 80400
1219                 online_analyze_lower_limit,
1220 #endif
1221                 0,
1222                 0x7fffffff,
1223                 PGC_USERSET,
1224 #if PG_VERSION_NUM >= 80400
1225                 GUC_NOT_IN_SAMPLE,
1226 #if PG_VERSION_NUM >= 90100
1227                 NULL,
1228 #endif
1229 #endif
1230                 NULL,
1231                 NULL
1232         );
1233
1234         RegisterXactCallback(removeTable, NULL);
1235 }
1236
1237 void _PG_fini(void);
1238 void
1239 _PG_fini(void)
1240 {
1241         ExecutorEnd_hook = oldExecutorEndHook;
1242 #if PG_VERSION_NUM >= 90200
1243         ProcessUtility_hook = oldProcessUtilityHook;
1244 #endif
1245
1246         if (excludeTables.tables)
1247                 free(excludeTables.tables);
1248         if (includeTables.tables)
1249                 free(includeTables.tables);
1250
1251         excludeTables.tables = includeTables.tables = NULL;
1252         excludeTables.nTables = includeTables.nTables = 0;
1253 }