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