Remove _PG_fini Mikhail Litsarev
[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                 }
1052         }
1053
1054 #if PG_VERSION_NUM >= 100000
1055 #define parsetree pstmt
1056 #endif
1057
1058         if (oldProcessUtilityHook)
1059                 oldProcessUtilityHook(parsetree, queryString,
1060 #if PG_VERSION_NUM >= 140000
1061                                                           readOnlyTree,
1062 #endif
1063 #if PG_VERSION_NUM >= 90300
1064                                                           context, params,
1065 #if PG_VERSION_NUM >= 100000
1066                                                           queryEnv,
1067 #endif
1068 #else
1069                                                           params, isTopLevel,
1070 #endif
1071                                                           dest, completionTag);
1072         else
1073                 standard_ProcessUtility(parsetree, queryString,
1074 #if PG_VERSION_NUM >= 140000
1075                                                                 readOnlyTree,
1076 #endif
1077 #if PG_VERSION_NUM >= 90300
1078                                                                 context, params,
1079 #if PG_VERSION_NUM >= 100000
1080                                                                 queryEnv,
1081 #endif
1082 #else
1083                                                                 params, isTopLevel,
1084 #endif
1085                                                                 dest, completionTag);
1086
1087 #if PG_VERSION_NUM >= 100000
1088 #undef parsetree
1089 #endif
1090
1091         if (tblnames) {
1092                 ListCell        *l;
1093
1094                 foreach(l, tblnames)
1095                 {
1096                         RangeVar        *tblname =
1097 #if PG_VERSION_NUM >= 110000
1098                                 (IsA(lfirst(l), VacuumRelation)) ?
1099                                         ((VacuumRelation*)lfirst(l))->relation :
1100 #endif
1101                                         (RangeVar*)lfirst(l);
1102                         Oid     tblOid;
1103
1104                         Assert(IsA(tblname, RangeVar));
1105
1106                         tblOid = RangeVarGetRelid(tblname, NoLock, true);
1107                         makeAnalyze(tblOid, op, -1);
1108                 }
1109         }
1110 }
1111 #endif
1112
1113
1114 static void
1115 relstatsInit(void)
1116 {
1117         HASHCTL hash_ctl;
1118         int             flags = 0;
1119
1120         MemSet(&hash_ctl, 0, sizeof(hash_ctl));
1121
1122         hash_ctl.hash = oid_hash;
1123         flags |= HASH_FUNCTION;
1124
1125         if (onlineAnalyzeMemoryContext)
1126         {
1127                 Assert(relstats != NULL);
1128                 MemoryContextReset(onlineAnalyzeMemoryContext);
1129         }
1130         else
1131         {
1132                 Assert(relstats == NULL);
1133
1134 #if PG_VERSION_NUM < 90600
1135                 onlineAnalyzeMemoryContext =
1136                         AllocSetContextCreate(CacheMemoryContext,
1137                         "online_analyze storage context",
1138                         ALLOCSET_DEFAULT_MINSIZE,
1139                         ALLOCSET_DEFAULT_INITSIZE,
1140                         ALLOCSET_DEFAULT_MAXSIZE
1141                         );
1142 #else
1143                 onlineAnalyzeMemoryContext =
1144                         AllocSetContextCreate(CacheMemoryContext,
1145                         "online_analyze storage context", ALLOCSET_DEFAULT_SIZES);
1146 #endif
1147         }
1148
1149         hash_ctl.hcxt = onlineAnalyzeMemoryContext;
1150         flags |= HASH_CONTEXT;
1151
1152         hash_ctl.keysize = sizeof(Oid);
1153
1154         hash_ctl.entrysize = sizeof(OnlineAnalyzeTableStat);
1155         flags |= HASH_ELEM;
1156
1157         relstats = hash_create("online_analyze storage", 1024, &hash_ctl, flags);
1158 }
1159
1160 void _PG_init(void);
1161 void
1162 _PG_init(void)
1163 {
1164         relstatsInit();
1165
1166         oldExecutorEndHook = ExecutorEnd_hook;
1167
1168         ExecutorEnd_hook = onlineAnalyzeHooker;
1169
1170 #if PG_VERSION_NUM >= 90200
1171         oldProcessUtilityHook = ProcessUtility_hook;
1172
1173         ProcessUtility_hook = onlineAnalyzeHookerUtility;
1174 #endif
1175
1176
1177         DefineCustomBoolVariable(
1178                 "online_analyze.enable",
1179                 "Enable on-line analyze",
1180                 "Enables analyze of table directly after insert/update/delete/select into",
1181                 &online_analyze_enable,
1182 #if PG_VERSION_NUM >= 80400
1183                 online_analyze_enable,
1184 #endif
1185                 PGC_USERSET,
1186 #if PG_VERSION_NUM >= 80400
1187                 GUC_NOT_IN_SAMPLE,
1188 #if PG_VERSION_NUM >= 90100
1189                 NULL,
1190 #endif
1191 #endif
1192                 NULL,
1193                 NULL
1194         );
1195
1196         DefineCustomBoolVariable(
1197                 "online_analyze.local_tracking",
1198                 "Per backend tracking",
1199                 "Per backend tracking for temp tables (do not use system statistic)",
1200                 &online_analyze_local_tracking,
1201 #if PG_VERSION_NUM >= 80400
1202                 online_analyze_local_tracking,
1203 #endif
1204                 PGC_USERSET,
1205 #if PG_VERSION_NUM >= 80400
1206                 GUC_NOT_IN_SAMPLE,
1207 #if PG_VERSION_NUM >= 90100
1208                 NULL,
1209 #endif
1210 #endif
1211                 NULL,
1212                 NULL
1213         );
1214
1215         DefineCustomBoolVariable(
1216                 "online_analyze.verbose",
1217                 "Verbosity of on-line analyze",
1218                 "Make ANALYZE VERBOSE after table's changes",
1219                 &online_analyze_verbose,
1220 #if PG_VERSION_NUM >= 80400
1221                 online_analyze_verbose,
1222 #endif
1223                 PGC_USERSET,
1224 #if PG_VERSION_NUM >= 80400
1225                 GUC_NOT_IN_SAMPLE,
1226 #if PG_VERSION_NUM >= 90100
1227                 NULL,
1228 #endif
1229 #endif
1230                 NULL,
1231                 NULL
1232         );
1233
1234         DefineCustomRealVariable(
1235                 "online_analyze.scale_factor",
1236                 "fraction of table size to start on-line analyze",
1237                 "fraction of table size to start on-line analyze",
1238                 &online_analyze_scale_factor,
1239 #if PG_VERSION_NUM >= 80400
1240                 online_analyze_scale_factor,
1241 #endif
1242                 0.0,
1243                 1.0,
1244                 PGC_USERSET,
1245 #if PG_VERSION_NUM >= 80400
1246                 GUC_NOT_IN_SAMPLE,
1247 #if PG_VERSION_NUM >= 90100
1248                 NULL,
1249 #endif
1250 #endif
1251                 NULL,
1252                 NULL
1253         );
1254
1255         DefineCustomIntVariable(
1256                 "online_analyze.threshold",
1257                 "min number of row updates before on-line analyze",
1258                 "min number of row updates before on-line analyze",
1259                 &online_analyze_threshold,
1260 #if PG_VERSION_NUM >= 80400
1261                 online_analyze_threshold,
1262 #endif
1263                 0,
1264                 0x7fffffff,
1265                 PGC_USERSET,
1266 #if PG_VERSION_NUM >= 80400
1267                 GUC_NOT_IN_SAMPLE,
1268 #if PG_VERSION_NUM >= 90100
1269                 NULL,
1270 #endif
1271 #endif
1272                 NULL,
1273                 NULL
1274         );
1275
1276         DefineCustomIntVariable(
1277                 "online_analyze.capacity_threshold",
1278                 "Max local cache table capacity",
1279                 "Max local cache table capacity",
1280                 &online_analyze_capacity_threshold,
1281 #if PG_VERSION_NUM >= 80400
1282                 online_analyze_capacity_threshold,
1283 #endif
1284                 0,
1285                 0x7fffffff,
1286                 PGC_USERSET,
1287 #if PG_VERSION_NUM >= 80400
1288                 GUC_NOT_IN_SAMPLE,
1289 #if PG_VERSION_NUM >= 90100
1290                 NULL,
1291 #endif
1292 #endif
1293                 NULL,
1294                 NULL
1295         );
1296
1297         DefineCustomRealVariable(
1298                 "online_analyze.min_interval",
1299                 "minimum time interval between analyze call (in milliseconds)",
1300                 "minimum time interval between analyze call (in milliseconds)",
1301                 &online_analyze_min_interval,
1302 #if PG_VERSION_NUM >= 80400
1303                 online_analyze_min_interval,
1304 #endif
1305                 0.0,
1306                 1e30,
1307                 PGC_USERSET,
1308 #if PG_VERSION_NUM >= 80400
1309                 GUC_NOT_IN_SAMPLE,
1310 #if PG_VERSION_NUM >= 90100
1311                 NULL,
1312 #endif
1313 #endif
1314                 NULL,
1315                 NULL
1316         );
1317
1318         DefineCustomEnumVariable(
1319                 "online_analyze.table_type",
1320                 "Type(s) of table for online analyze: all(default), persistent, temporary, none",
1321                 NULL,
1322                 &online_analyze_table_type,
1323 #if PG_VERSION_NUM >= 80400
1324                 online_analyze_table_type,
1325 #endif
1326                 online_analyze_table_type_options,
1327                 PGC_USERSET,
1328 #if PG_VERSION_NUM >= 80400
1329                 GUC_NOT_IN_SAMPLE,
1330 #if PG_VERSION_NUM >= 90100
1331                 NULL,
1332 #endif
1333 #endif
1334                 NULL,
1335                 NULL
1336         );
1337
1338         DefineCustomStringVariable(
1339                 "online_analyze.exclude_tables",
1340                 "List of tables which will not online analyze",
1341                 NULL,
1342                 &excludeTables.tableStr,
1343 #if PG_VERSION_NUM >= 80400
1344                 "",
1345 #endif
1346                 PGC_USERSET,
1347                 0,
1348 #if PG_VERSION_NUM >= 90100
1349                 excludeTablesCheck,
1350                 excludeTablesAssign,
1351 #else
1352                 excludeTablesAssign,
1353 #endif
1354                 excludeTablesShow
1355         );
1356
1357         DefineCustomStringVariable(
1358                 "online_analyze.include_tables",
1359                 "List of tables which will online analyze",
1360                 NULL,
1361                 &includeTables.tableStr,
1362 #if PG_VERSION_NUM >= 80400
1363                 "",
1364 #endif
1365                 PGC_USERSET,
1366                 0,
1367 #if PG_VERSION_NUM >= 90100
1368                 includeTablesCheck,
1369                 includeTablesAssign,
1370 #else
1371                 includeTablesAssign,
1372 #endif
1373                 includeTablesShow
1374         );
1375
1376         DefineCustomIntVariable(
1377                 "online_analyze.lower_limit",
1378                 "min number of rows in table to analyze",
1379                 "min number of rows in table to analyze",
1380                 &online_analyze_lower_limit,
1381 #if PG_VERSION_NUM >= 80400
1382                 online_analyze_lower_limit,
1383 #endif
1384                 0,
1385                 0x7fffffff,
1386                 PGC_USERSET,
1387 #if PG_VERSION_NUM >= 80400
1388                 GUC_NOT_IN_SAMPLE,
1389 #if PG_VERSION_NUM >= 90100
1390                 NULL,
1391 #endif
1392 #endif
1393                 NULL,
1394                 NULL
1395         );
1396
1397         RegisterXactCallback(removeTable, NULL);
1398 }