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