6ffcc30997ddc259a504b3fbfe6af5d3c59bc3cf
[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  changes_since_analyze;
136         TimestampTz             autovac_analyze_timestamp;
137         TimestampTz             analyze_timestamp;
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->changes_since_analyze = 0;
503                 rstat->analyze_timestamp = 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 = tabentry->n_dead_tuples + tabentry->n_live_tuples;
527                         rstat->changes_since_analyze =
528 #if PG_VERSION_NUM >= 90000
529                                 tabentry->changes_since_analyze;
530 #else
531                                 tabentry->n_live_tuples + tabentry->n_dead_tuples -
532                                         tabentry->last_anl_tuples;
533 #endif
534                         rstat->autovac_analyze_timestamp =
535                                 tabentry->autovac_analyze_timestamp;
536                         rstat->analyze_timestamp = tabentry->analyze_timestamp;
537                 }
538         }
539
540         if (newTable ||
541                 /* force analyze after truncate, fasttruncate already did analyze */
542                 operation == CK_TRUNCATE || (
543                 /* do not analyze too often, if both stamps are exceeded the go */
544                 TimestampDifferenceExceeds(rstat->analyze_timestamp, now, online_analyze_min_interval) &&
545                 TimestampDifferenceExceeds(rstat->autovac_analyze_timestamp, now, online_analyze_min_interval) &&
546                 /* do not analyze too small tables */
547                 rstat->n_tuples + rstat->changes_since_analyze + naffected > online_analyze_lower_limit &&
548                 /* be in sync with relation_needs_vacanalyze */
549                 ((double)(rstat->changes_since_analyze + naffected)) >=
550                          online_analyze_scale_factor * ((double)rstat->n_tuples) +
551                          (double)online_analyze_threshold))
552         {
553 #if PG_VERSION_NUM < 90500
554                 VacuumStmt                              vacstmt;
555 #else
556                 VacuumParams                    vacstmt;
557 #endif
558                 TimestampTz                             startStamp, endStamp;
559                 int                                             flags;
560
561 #ifdef PGPRO_EE
562                 /* ATX is not compatible with online_analyze */
563                 if (getNestLevelATX() != 0)
564                         return;
565 #endif
566
567                 memset(&startStamp, 0, sizeof(startStamp)); /* keep compiler quiet */
568
569                 memset(&vacstmt, 0, sizeof(vacstmt));
570
571                 vacstmt.freeze_min_age = -1;
572                 vacstmt.freeze_table_age = -1; /* ??? */
573
574 #if PG_VERSION_NUM < 90500
575                 vacstmt.type = T_VacuumStmt;
576                 vacstmt.relation = NULL;
577                 vacstmt.va_cols = NIL;
578 #if PG_VERSION_NUM >= 90000
579                 vacstmt.options = VACOPT_ANALYZE;
580                 if (online_analyze_verbose)
581                         vacstmt.options |= VACOPT_VERBOSE;
582 #else
583                 vacstmt.vacuum = vacstmt.full = false;
584                 vacstmt.analyze = true;
585                 vacstmt.verbose = online_analyze_verbose;
586 #endif
587 #else
588                 vacstmt.multixact_freeze_min_age = -1;
589                 vacstmt.multixact_freeze_table_age = -1;
590                 vacstmt.log_min_duration = -1;
591 #endif
592
593
594                 if (online_analyze_verbose)
595                         startStamp = GetCurrentTimestamp();
596
597                 flags = VACOPT_ANALYZE | VACOPT_NOWAIT |
598                                         ((online_analyze_verbose) ?  VACOPT_VERBOSE : 0);
599
600 #if PG_VERSION_NUM >= 120000
601                 vacstmt.options = flags;
602 #endif
603                 analyze_rel(relOid,
604 #if PG_VERSION_NUM < 90500
605                         &vacstmt
606 #if PG_VERSION_NUM >= 90018
607                         , true
608 #endif
609                         , GetAccessStrategy(BAS_VACUUM)
610 #if (PG_VERSION_NUM >= 90000) && (PG_VERSION_NUM < 90004)
611                         , true
612 #endif
613 #else
614                         makeRangeVarFromOid(relOid),
615 #if PG_VERSION_NUM < 120000
616                         flags,
617 #endif
618                         &vacstmt, NULL, true, GetAccessStrategy(BAS_VACUUM)
619 #endif
620                 );
621
622                 /* Make changes visible to subsequent calls */
623                 CommandCounterIncrement();
624
625                 if (online_analyze_verbose)
626                 {
627                         long    secs;
628                         int             microsecs;
629
630                         endStamp = GetCurrentTimestamp();
631                         TimestampDifference(startStamp, endStamp, &secs, &microsecs);
632                         elog(INFO, "analyze \"%s\" took %.02f seconds",
633                                 get_rel_name(relOid),
634                                 ((double)secs) + ((double)microsecs)/1.0e6);
635                 }
636
637                 rstat->autovac_analyze_timestamp = now;
638                 rstat->changes_since_analyze = 0;
639
640                 switch(operation)
641                 {
642                         case CK_CREATE:
643                         case CK_INSERT:
644                         case CK_UPDATE:
645                                 rstat->n_tuples += naffected;
646                                 /* FALLTHROUGH */
647                         case CK_DELETE:
648                                 rstat->rereadStat = (reltype == OATT_PERSISTENT);
649                                 break;
650                         case CK_TRUNCATE:
651                         case CK_FASTTRUNCATE:
652                                 rstat->rereadStat = false;
653                                 rstat->n_tuples = 0;
654                                 break;
655                         default:
656                                 break;
657                 }
658
659                 /* update last analyze timestamp in local memory of backend */
660                 if (tabentry)
661                 {
662                         tabentry->analyze_timestamp = now;
663                         tabentry->changes_since_analyze = 0;
664                 }
665 #if 0
666                 /* force reload stat for new table */
667                 if (newTable)
668                         pgstat_clear_snapshot();
669 #endif
670         }
671         else
672         {
673 #if PG_VERSION_NUM >= 90000
674                 if (tabentry)
675                         tabentry->changes_since_analyze += naffected;
676 #endif
677                 switch(operation)
678                 {
679                         case CK_CREATE:
680                         case CK_INSERT:
681                                 rstat->changes_since_analyze += naffected;
682                                 rstat->n_tuples += naffected;
683                                 break;
684                         case CK_UPDATE:
685                                 rstat->changes_since_analyze += 2 * naffected;
686                                 rstat->n_tuples += naffected;
687                                 break;
688                         case CK_DELETE:
689                                 rstat->changes_since_analyze += naffected;
690                                 break;
691                         case CK_TRUNCATE:
692                         case CK_FASTTRUNCATE:
693                                 rstat->changes_since_analyze = 0;
694                                 rstat->n_tuples = 0;
695                                 break;
696                         default:
697                                 break;
698                 }
699         }
700
701         /* Reset local cache if we are over limit */
702         if (hash_get_num_entries(relstats) > online_analyze_capacity_threshold)
703                 relstatsInit();
704 }
705
706 static Const*
707 isFastTruncateCall(QueryDesc *queryDesc)
708 {
709         TargetEntry     *te;
710         FuncExpr        *fe;
711         Const           *constval;
712
713         if (!(
714                   queryDesc->plannedstmt &&
715                   queryDesc->operation == CMD_SELECT &&
716                   queryDesc->plannedstmt->planTree &&
717                   queryDesc->plannedstmt->planTree->targetlist &&
718                   list_length(queryDesc->plannedstmt->planTree->targetlist) == 1
719                  ))
720                 return NULL;
721
722         te = linitial(queryDesc->plannedstmt->planTree->targetlist);
723
724         if (!IsA(te, TargetEntry))
725                 return NULL;
726
727         fe = (FuncExpr*)te->expr;
728
729         if (!(
730                   fe && IsA(fe, FuncExpr) &&
731                   fe->funcid >= FirstNormalObjectId &&
732                   fe->funcretset == false &&
733                   fe->funcresulttype == VOIDOID &&
734                   fe->funcvariadic == false &&
735                   list_length(fe->args) == 1
736                  ))
737                 return NULL;
738
739         constval = linitial(fe->args);
740
741         if (!(
742                   IsA(constval,Const) &&
743                   constval->consttype == TEXTOID &&
744                   strcmp(get_func_name(fe->funcid), "fasttruncate") == 0
745                  ))
746                 return NULL;
747
748         return constval;
749 }
750
751
752 extern PGDLLIMPORT void onlineAnalyzeHooker(QueryDesc *queryDesc);
753 void
754 onlineAnalyzeHooker(QueryDesc *queryDesc)
755 {
756         int64   naffected = -1;
757         Const   *constval;
758
759         if (queryDesc->estate)
760                 naffected = queryDesc->estate->es_processed;
761
762         lateInit();
763
764 #if PG_VERSION_NUM >= 90200
765         if (online_analyze_enable &&
766                 (constval = isFastTruncateCall(queryDesc)) != NULL)
767         {
768                 Datum           tblnamed = constval->constvalue;
769                 char            *tblname = text_to_cstring(DatumGetTextP(tblnamed));
770                 RangeVar        *tblvar =
771                         makeRangeVarFromNameList(stringToQualifiedNameList(tblname));
772
773                 makeAnalyze(RangeVarGetRelid(tblvar,
774                                                                          NoLock,
775                                                                          false),
776                                         CK_FASTTRUNCATE, -1);
777         }
778 #endif
779
780         if (online_analyze_enable && queryDesc->plannedstmt &&
781                         (queryDesc->operation == CMD_INSERT ||
782                          queryDesc->operation == CMD_UPDATE ||
783                          queryDesc->operation == CMD_DELETE
784 #if PG_VERSION_NUM < 90200
785                          || (queryDesc->operation == CMD_SELECT &&
786                                  queryDesc->plannedstmt->intoClause)
787 #endif
788                          ))
789         {
790 #if PG_VERSION_NUM < 90200
791                 if (queryDesc->operation == CMD_SELECT)
792                 {
793                         Oid     relOid = RangeVarGetRelid(queryDesc->plannedstmt->intoClause->rel, true);
794
795                         makeAnalyze(relOid, queryDesc->operation, naffected);
796                 }
797                 else
798 #endif
799                 if (queryDesc->plannedstmt->resultRelations &&
800                                  queryDesc->plannedstmt->rtable)
801                 {
802                         ListCell        *l;
803
804                         foreach(l, queryDesc->plannedstmt->resultRelations)
805                         {
806                                 int                             n = lfirst_int(l);
807                                 RangeTblEntry   *rte = list_nth(queryDesc->plannedstmt->rtable, n-1);
808
809                                 if (rte->rtekind == RTE_RELATION)
810                                         makeAnalyze(rte->relid, (CmdKind)queryDesc->operation, naffected);
811                         }
812                 }
813         }
814
815         if (oldExecutorEndHook)
816                 oldExecutorEndHook(queryDesc);
817         else
818                 standard_ExecutorEnd(queryDesc);
819 }
820
821 static List             *toremove = NIL;
822
823 /*
824  * removeTable called on transaction end, see call RegisterXactCallback() below
825  */
826 static void
827 removeTable(XactEvent event, void *arg)
828 {
829         ListCell        *cell;
830
831         switch(event)
832         {
833                 case XACT_EVENT_COMMIT:
834                         break;
835                 case XACT_EVENT_ABORT:
836                         toremove = NIL;
837                 default:
838                         return;
839         }
840
841         foreach(cell, toremove)
842         {
843                 Oid     relOid = lfirst_oid(cell);
844
845                 hash_search(relstats, &relOid, HASH_REMOVE, NULL);
846         }
847
848         toremove = NIL;
849 }
850
851 #if PG_VERSION_NUM >= 120000
852 static int
853 parse_vacuum_opt(VacuumStmt *vacstmt)
854 {
855         int                     options = vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE;
856         ListCell        *lc;
857
858         foreach(lc, vacstmt->options)
859         {
860                 DefElem *opt = (DefElem *) lfirst(lc);
861
862                 /* Parse common options for VACUUM and ANALYZE */
863                 if (strcmp(opt->defname, "verbose") == 0)
864                         options |= VACOPT_VERBOSE;
865                 else if (strcmp(opt->defname, "skip_locked") == 0)
866                         options |= VACOPT_SKIP_LOCKED;
867                 else if (strcmp(opt->defname, "analyze") == 0)
868                         options |= VACOPT_ANALYZE;
869                 else if (strcmp(opt->defname, "freeze") == 0)
870                         options |= VACOPT_FREEZE;
871                 else if (strcmp(opt->defname, "full") == 0)
872                         options |= VACOPT_FULL;
873                 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
874                         options |= VACOPT_DISABLE_PAGE_SKIPPING;
875         }
876
877         return options;
878 }
879 #endif
880
881
882 #if PG_VERSION_NUM >= 90200
883 static void
884 onlineAnalyzeHookerUtility(
885 #if PG_VERSION_NUM >= 100000
886                                                    PlannedStmt *pstmt,
887 #else
888                                                    Node *parsetree,
889 #endif
890                                                    const char *queryString,
891 #if PG_VERSION_NUM >= 140000
892                                                    bool readOnlyTree,
893 #endif
894 #if PG_VERSION_NUM >= 90300
895                                                         ProcessUtilityContext context, ParamListInfo params,
896 #if PG_VERSION_NUM >= 100000
897                                                         QueryEnvironment *queryEnv,
898 #endif
899 #else
900                                                         ParamListInfo params, bool isTopLevel,
901 #endif
902                                                         DestReceiver *dest,
903 #if  PG_VERSION_NUM >= 130000
904                                                         QueryCompletion *completionTag
905 #else
906                                                         char *completionTag
907 #endif
908 ) {
909         List            *tblnames = NIL;
910         CmdKind         op = CK_INSERT;
911 #if PG_VERSION_NUM >= 100000
912         Node            *parsetree = NULL;
913
914         if (pstmt->commandType == CMD_UTILITY)
915                 parsetree = pstmt->utilityStmt;
916 #endif
917
918         lateInit();
919
920         if (parsetree && online_analyze_enable)
921         {
922                 if (IsA(parsetree, CreateTableAsStmt) &&
923                         ((CreateTableAsStmt*)parsetree)->into)
924                 {
925                         tblnames =
926                                 list_make1((RangeVar*)copyObject(((CreateTableAsStmt*)parsetree)->into->rel));
927                         op = CK_CREATE;
928                 }
929                 else if (IsA(parsetree, TruncateStmt))
930                 {
931                         tblnames = list_copy(((TruncateStmt*)parsetree)->relations);
932                         op = CK_TRUNCATE;
933                 }
934                 else if (IsA(parsetree, DropStmt) &&
935                                  ((DropStmt*)parsetree)->removeType == OBJECT_TABLE)
936                 {
937                         ListCell        *cell;
938
939                         foreach(cell, ((DropStmt*)parsetree)->objects)
940                         {
941                                 List            *relname = (List *) lfirst(cell);
942                                 RangeVar        *rel = makeRangeVarFromNameList(relname);
943                                 Oid                     relOid = RangeVarGetRelid(rel, NoLock, true);
944
945                                 if (OidIsValid(relOid))
946                                 {
947                                         MemoryContext   ctx;
948
949                                         ctx = MemoryContextSwitchTo(TopTransactionContext);
950                                         toremove = lappend_oid(toremove, relOid);
951                                         MemoryContextSwitchTo(ctx);
952                                 }
953                         }
954                 }
955                 else if (IsA(parsetree, VacuumStmt))
956                 {
957                         VacuumStmt      *vac = (VacuumStmt*)parsetree;
958                         int                     options =
959 #if PG_VERSION_NUM >= 120000
960                                                         parse_vacuum_opt(vac)
961 #else
962                                                         vac->options
963 #endif
964                                                         ;
965
966
967 #if PG_VERSION_NUM >= 110000
968                         tblnames = vac->rels;
969 #else
970                         if (vac->relation)
971                                 tblnames = list_make1(vac->relation);
972 #endif
973
974                         if (options & (VACOPT_VACUUM | VACOPT_FULL | VACOPT_FREEZE))
975                         {
976                                 /* optionally with analyze */
977                                 op = CK_VACUUM;
978
979                                 /* drop all collected stat */
980                                 if (tblnames == NIL)
981                                         relstatsInit();
982                         }
983                         else if (options & VACOPT_ANALYZE)
984                         {
985                                 op = CK_ANALYZE;
986
987                                 /* should reset all counters */
988                                 if (tblnames == NIL)
989                                 {
990                                         HASH_SEQ_STATUS                 hs;
991                                         OnlineAnalyzeTableStat  *rstat;
992                                         TimestampTz                             now = GetCurrentTimestamp();
993
994                                         hash_seq_init(&hs, relstats);
995
996                                         while((rstat = hash_seq_search(&hs)) != NULL)
997                                         {
998                                                 rstat->changes_since_analyze = 0;
999                                                 rstat->analyze_timestamp = now;
1000                                         }
1001                                 }
1002                         }
1003                         else
1004                                 tblnames = NIL;
1005                 }
1006         }
1007
1008 #if PG_VERSION_NUM >= 100000
1009 #define parsetree pstmt
1010 #endif
1011
1012         if (oldProcessUtilityHook)
1013                 oldProcessUtilityHook(parsetree, queryString,
1014 #if PG_VERSION_NUM >= 140000
1015                                                           readOnlyTree,
1016 #endif
1017 #if PG_VERSION_NUM >= 90300
1018                                                           context, params,
1019 #if PG_VERSION_NUM >= 100000
1020                                                           queryEnv,
1021 #endif
1022 #else
1023                                                           params, isTopLevel,
1024 #endif
1025                                                           dest, completionTag);
1026         else
1027                 standard_ProcessUtility(parsetree, queryString,
1028 #if PG_VERSION_NUM >= 140000
1029                                                                 readOnlyTree,
1030 #endif
1031 #if PG_VERSION_NUM >= 90300
1032                                                                 context, params,
1033 #if PG_VERSION_NUM >= 100000
1034                                                                 queryEnv,
1035 #endif
1036 #else
1037                                                                 params, isTopLevel,
1038 #endif
1039                                                                 dest, completionTag);
1040
1041 #if PG_VERSION_NUM >= 100000
1042 #undef parsetree
1043 #endif
1044
1045         if (tblnames) {
1046                 ListCell        *l;
1047
1048                 foreach(l, tblnames)
1049                 {
1050                         RangeVar        *tblname =
1051 #if PG_VERSION_NUM >= 110000
1052                                 (IsA(lfirst(l), VacuumRelation)) ?
1053                                         ((VacuumRelation*)lfirst(l))->relation :
1054 #endif
1055                                         (RangeVar*)lfirst(l);
1056                         Oid     tblOid;
1057
1058                         Assert(IsA(tblname, RangeVar));
1059
1060                         tblOid = RangeVarGetRelid(tblname, NoLock, true);
1061                         makeAnalyze(tblOid, op, -1);
1062                 }
1063         }
1064 }
1065 #endif
1066
1067
1068 static void
1069 relstatsInit(void)
1070 {
1071         HASHCTL hash_ctl;
1072         int             flags = 0;
1073
1074         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1075
1076         hash_ctl.hash = oid_hash;
1077         flags |= HASH_FUNCTION;
1078
1079         if (onlineAnalyzeMemoryContext)
1080         {
1081                 Assert(relstats != NULL);
1082                 MemoryContextReset(onlineAnalyzeMemoryContext);
1083         }
1084         else
1085         {
1086                 Assert(relstats == NULL);
1087
1088 #if PG_VERSION_NUM < 90600
1089                 onlineAnalyzeMemoryContext =
1090                         AllocSetContextCreate(CacheMemoryContext,
1091                         "online_analyze storage context",
1092                         ALLOCSET_DEFAULT_MINSIZE,
1093                         ALLOCSET_DEFAULT_INITSIZE,
1094                         ALLOCSET_DEFAULT_MAXSIZE
1095                         );
1096 #else
1097                 onlineAnalyzeMemoryContext =
1098                         AllocSetContextCreate(CacheMemoryContext,
1099                         "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1100 #endif
1101         }
1102
1103         hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1104         flags |= HASH_CONTEXT;
1105
1106         hash_ctl.keysize = sizeof(Oid);
1107
1108         hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1109         flags |= HASH_ELEM;
1110
1111         relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1112 }
1113
1114 void _PG_init(void);
1115 void
1116 _PG_init(void)
1117 {
1118         relstatsInit();
1119
1120         oldExecutorEndHook = ExecutorEnd_hook;
1121
1122         ExecutorEnd_hook = onlineAnalyzeHooker;
1123
1124 #if PG_VERSION_NUM >= 90200
1125         oldProcessUtilityHook = ProcessUtility_hook;
1126
1127         ProcessUtility_hook = onlineAnalyzeHookerUtility;
1128 #endif
1129
1130
1131         DefineCustomBoolVariable(
1132                 "online_analyze.enable",
1133                 "Enable on-line analyze",
1134                 "Enables analyze of table directly after insert/update/delete/select into",
1135                 &online_analyze_enable,
1136 #if PG_VERSION_NUM >= 80400
1137                 online_analyze_enable,
1138 #endif
1139                 PGC_USERSET,
1140 #if PG_VERSION_NUM >= 80400
1141                 GUC_NOT_IN_SAMPLE,
1142 #if PG_VERSION_NUM >= 90100
1143                 NULL,
1144 #endif
1145 #endif
1146                 NULL,
1147                 NULL
1148         );
1149
1150         DefineCustomBoolVariable(
1151                 "online_analyze.local_tracking",
1152                 "Per backend tracking",
1153                 "Per backend tracking for temp tables (do not use system statistic)",
1154                 &online_analyze_local_tracking,
1155 #if PG_VERSION_NUM >= 80400
1156                 online_analyze_local_tracking,
1157 #endif
1158                 PGC_USERSET,
1159 #if PG_VERSION_NUM >= 80400
1160                 GUC_NOT_IN_SAMPLE,
1161 #if PG_VERSION_NUM >= 90100
1162                 NULL,
1163 #endif
1164 #endif
1165                 NULL,
1166                 NULL
1167         );
1168
1169         DefineCustomBoolVariable(
1170                 "online_analyze.verbose",
1171                 "Verbosity of on-line analyze",
1172                 "Make ANALYZE VERBOSE after table's changes",
1173                 &online_analyze_verbose,
1174 #if PG_VERSION_NUM >= 80400
1175                 online_analyze_verbose,
1176 #endif
1177                 PGC_USERSET,
1178 #if PG_VERSION_NUM >= 80400
1179                 GUC_NOT_IN_SAMPLE,
1180 #if PG_VERSION_NUM >= 90100
1181                 NULL,
1182 #endif
1183 #endif
1184                 NULL,
1185                 NULL
1186         );
1187
1188         DefineCustomRealVariable(
1189                 "online_analyze.scale_factor",
1190                 "fraction of table size to start on-line analyze",
1191                 "fraction of table size to start on-line analyze",
1192                 &online_analyze_scale_factor,
1193 #if PG_VERSION_NUM >= 80400
1194                 online_analyze_scale_factor,
1195 #endif
1196                 0.0,
1197                 1.0,
1198                 PGC_USERSET,
1199 #if PG_VERSION_NUM >= 80400
1200                 GUC_NOT_IN_SAMPLE,
1201 #if PG_VERSION_NUM >= 90100
1202                 NULL,
1203 #endif
1204 #endif
1205                 NULL,
1206                 NULL
1207         );
1208
1209         DefineCustomIntVariable(
1210                 "online_analyze.threshold",
1211                 "min number of row updates before on-line analyze",
1212                 "min number of row updates before on-line analyze",
1213                 &online_analyze_threshold,
1214 #if PG_VERSION_NUM >= 80400
1215                 online_analyze_threshold,
1216 #endif
1217                 0,
1218                 0x7fffffff,
1219                 PGC_USERSET,
1220 #if PG_VERSION_NUM >= 80400
1221                 GUC_NOT_IN_SAMPLE,
1222 #if PG_VERSION_NUM >= 90100
1223                 NULL,
1224 #endif
1225 #endif
1226                 NULL,
1227                 NULL
1228         );
1229
1230         DefineCustomIntVariable(
1231                 "online_analyze.capacity_threshold",
1232                 "Max local cache table capacity",
1233                 "Max local cache table capacity",
1234                 &online_analyze_capacity_threshold,
1235 #if PG_VERSION_NUM >= 80400
1236                 online_analyze_capacity_threshold,
1237 #endif
1238                 0,
1239                 0x7fffffff,
1240                 PGC_USERSET,
1241 #if PG_VERSION_NUM >= 80400
1242                 GUC_NOT_IN_SAMPLE,
1243 #if PG_VERSION_NUM >= 90100
1244                 NULL,
1245 #endif
1246 #endif
1247                 NULL,
1248                 NULL
1249         );
1250
1251         DefineCustomRealVariable(
1252                 "online_analyze.min_interval",
1253                 "minimum time interval between analyze call (in milliseconds)",
1254                 "minimum time interval between analyze call (in milliseconds)",
1255                 &online_analyze_min_interval,
1256 #if PG_VERSION_NUM >= 80400
1257                 online_analyze_min_interval,
1258 #endif
1259                 0.0,
1260                 1e30,
1261                 PGC_USERSET,
1262 #if PG_VERSION_NUM >= 80400
1263                 GUC_NOT_IN_SAMPLE,
1264 #if PG_VERSION_NUM >= 90100
1265                 NULL,
1266 #endif
1267 #endif
1268                 NULL,
1269                 NULL
1270         );
1271
1272         DefineCustomEnumVariable(
1273                 "online_analyze.table_type",
1274                 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1275                 NULL,
1276                 &online_analyze_table_type,
1277 #if PG_VERSION_NUM >= 80400
1278                 online_analyze_table_type,
1279 #endif
1280                 online_analyze_table_type_options,
1281                 PGC_USERSET,
1282 #if PG_VERSION_NUM >= 80400
1283                 GUC_NOT_IN_SAMPLE,
1284 #if PG_VERSION_NUM >= 90100
1285                 NULL,
1286 #endif
1287 #endif
1288                 NULL,
1289                 NULL
1290         );
1291
1292         DefineCustomStringVariable(
1293                 "online_analyze.exclude_tables",
1294                 "List of tables which will not online analyze",
1295                 NULL,
1296                 &excludeTables.tableStr,
1297 #if PG_VERSION_NUM >= 80400
1298                 "",
1299 #endif
1300                 PGC_USERSET,
1301                 0,
1302 #if PG_VERSION_NUM >= 90100
1303                 excludeTablesCheck,
1304                 excludeTablesAssign,
1305 #else
1306                 excludeTablesAssign,
1307 #endif
1308                 excludeTablesShow
1309         );
1310
1311         DefineCustomStringVariable(
1312                 "online_analyze.include_tables",
1313                 "List of tables which will online analyze",
1314                 NULL,
1315                 &includeTables.tableStr,
1316 #if PG_VERSION_NUM >= 80400
1317                 "",
1318 #endif
1319                 PGC_USERSET,
1320                 0,
1321 #if PG_VERSION_NUM >= 90100
1322                 includeTablesCheck,
1323                 includeTablesAssign,
1324 #else
1325                 includeTablesAssign,
1326 #endif
1327                 includeTablesShow
1328         );
1329
1330         DefineCustomIntVariable(
1331                 "online_analyze.lower_limit",
1332                 "min number of rows in table to analyze",
1333                 "min number of rows in table to analyze",
1334                 &online_analyze_lower_limit,
1335 #if PG_VERSION_NUM >= 80400
1336                 online_analyze_lower_limit,
1337 #endif
1338                 0,
1339                 0x7fffffff,
1340                 PGC_USERSET,
1341 #if PG_VERSION_NUM >= 80400
1342                 GUC_NOT_IN_SAMPLE,
1343 #if PG_VERSION_NUM >= 90100
1344                 NULL,
1345 #endif
1346 #endif
1347                 NULL,
1348                 NULL
1349         );
1350
1351         RegisterXactCallback(removeTable, NULL);
1352 }
1353
1354 void _PG_fini(void);
1355 void
1356 _PG_fini(void)
1357 {
1358         ExecutorEnd_hook = oldExecutorEndHook;
1359 #if PG_VERSION_NUM >= 90200
1360         ProcessUtility_hook = oldProcessUtilityHook;
1361 #endif
1362
1363         if (excludeTables.tables)
1364                 free(excludeTables.tables);
1365         if (includeTables.tables)
1366                 free(includeTables.tables);
1367
1368         excludeTables.tables = includeTables.tables = NULL;
1369         excludeTables.nTables = includeTables.nTables = 0;
1370 }