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