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