LCOV - code coverage report
Current view: top level - contrib/pg_stat_statements - pg_stat_statements.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 717 956 75.0 %
Date: 2025-01-18 05:15:39 Functions: 44 51 86.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * pg_stat_statements.c
       4             :  *      Track statement planning and execution times as well as resource
       5             :  *      usage across a whole database cluster.
       6             :  *
       7             :  * Execution costs are totaled for each distinct source query, and kept in
       8             :  * a shared hashtable.  (We track only as many distinct queries as will fit
       9             :  * in the designated amount of shared memory.)
      10             :  *
      11             :  * Starting in Postgres 9.2, this module normalized query entries.  As of
      12             :  * Postgres 14, the normalization is done by the core if compute_query_id is
      13             :  * enabled, or optionally by third-party modules.
      14             :  *
      15             :  * To facilitate presenting entries to users, we create "representative" query
      16             :  * strings in which constants are replaced with parameter symbols ($n), to
      17             :  * make it clearer what a normalized entry can represent.  To save on shared
      18             :  * memory, and to avoid having to truncate oversized query strings, we store
      19             :  * these strings in a temporary external query-texts file.  Offsets into this
      20             :  * file are kept in shared memory.
      21             :  *
      22             :  * Note about locking issues: to create or delete an entry in the shared
      23             :  * hashtable, one must hold pgss->lock exclusively.  Modifying any field
      24             :  * in an entry except the counters requires the same.  To look up an entry,
      25             :  * one must hold the lock shared.  To read or update the counters within
      26             :  * an entry, one must hold the lock shared or exclusive (so the entry doesn't
      27             :  * disappear!) and also take the entry's mutex spinlock.
      28             :  * The shared state variable pgss->extent (the next free spot in the external
      29             :  * query-text file) should be accessed only while holding either the
      30             :  * pgss->mutex spinlock, or exclusive lock on pgss->lock.  We use the mutex to
      31             :  * allow reserving file space while holding only shared lock on pgss->lock.
      32             :  * Rewriting the entire external query-text file, eg for garbage collection,
      33             :  * requires holding pgss->lock exclusively; this allows individual entries
      34             :  * in the file to be read or written while holding only shared lock.
      35             :  *
      36             :  *
      37             :  * Copyright (c) 2008-2025, PostgreSQL Global Development Group
      38             :  *
      39             :  * IDENTIFICATION
      40             :  *    contrib/pg_stat_statements/pg_stat_statements.c
      41             :  *
      42             :  *-------------------------------------------------------------------------
      43             :  */
      44             : #include "postgres.h"
      45             : 
      46             : #include <math.h>
      47             : #include <sys/stat.h>
      48             : #include <unistd.h>
      49             : 
      50             : #include "access/parallel.h"
      51             : #include "catalog/pg_authid.h"
      52             : #include "common/int.h"
      53             : #include "executor/instrument.h"
      54             : #include "funcapi.h"
      55             : #include "jit/jit.h"
      56             : #include "mb/pg_wchar.h"
      57             : #include "miscadmin.h"
      58             : #include "nodes/queryjumble.h"
      59             : #include "optimizer/planner.h"
      60             : #include "parser/analyze.h"
      61             : #include "parser/scanner.h"
      62             : #include "pgstat.h"
      63             : #include "storage/fd.h"
      64             : #include "storage/ipc.h"
      65             : #include "storage/lwlock.h"
      66             : #include "storage/shmem.h"
      67             : #include "storage/spin.h"
      68             : #include "tcop/utility.h"
      69             : #include "utils/acl.h"
      70             : #include "utils/builtins.h"
      71             : #include "utils/memutils.h"
      72             : #include "utils/timestamp.h"
      73             : 
      74          16 : PG_MODULE_MAGIC;
      75             : 
      76             : /* Location of permanent stats file (valid when database is shut down) */
      77             : #define PGSS_DUMP_FILE  PGSTAT_STAT_PERMANENT_DIRECTORY "/pg_stat_statements.stat"
      78             : 
      79             : /*
      80             :  * Location of external query text file.
      81             :  */
      82             : #define PGSS_TEXT_FILE  PG_STAT_TMP_DIR "/pgss_query_texts.stat"
      83             : 
      84             : /* Magic number identifying the stats file format */
      85             : static const uint32 PGSS_FILE_HEADER = 0x20220408;
      86             : 
      87             : /* PostgreSQL major version number, changes in which invalidate all entries */
      88             : static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
      89             : 
      90             : /* XXX: Should USAGE_EXEC reflect execution time and/or buffer usage? */
      91             : #define USAGE_EXEC(duration)    (1.0)
      92             : #define USAGE_INIT              (1.0)   /* including initial planning */
      93             : #define ASSUMED_MEDIAN_INIT     (10.0)  /* initial assumed median usage */
      94             : #define ASSUMED_LENGTH_INIT     1024    /* initial assumed mean query length */
      95             : #define USAGE_DECREASE_FACTOR   (0.99)  /* decreased every entry_dealloc */
      96             : #define STICKY_DECREASE_FACTOR  (0.50)  /* factor for sticky entries */
      97             : #define USAGE_DEALLOC_PERCENT   5   /* free this % of entries at once */
      98             : #define IS_STICKY(c)    ((c.calls[PGSS_PLAN] + c.calls[PGSS_EXEC]) == 0)
      99             : 
     100             : /*
     101             :  * Extension version number, for supporting older extension versions' objects
     102             :  */
     103             : typedef enum pgssVersion
     104             : {
     105             :     PGSS_V1_0 = 0,
     106             :     PGSS_V1_1,
     107             :     PGSS_V1_2,
     108             :     PGSS_V1_3,
     109             :     PGSS_V1_8,
     110             :     PGSS_V1_9,
     111             :     PGSS_V1_10,
     112             :     PGSS_V1_11,
     113             :     PGSS_V1_12,
     114             : } pgssVersion;
     115             : 
     116             : typedef enum pgssStoreKind
     117             : {
     118             :     PGSS_INVALID = -1,
     119             : 
     120             :     /*
     121             :      * PGSS_PLAN and PGSS_EXEC must be respectively 0 and 1 as they're used to
     122             :      * reference the underlying values in the arrays in the Counters struct,
     123             :      * and this order is required in pg_stat_statements_internal().
     124             :      */
     125             :     PGSS_PLAN = 0,
     126             :     PGSS_EXEC,
     127             : } pgssStoreKind;
     128             : 
     129             : #define PGSS_NUMKIND (PGSS_EXEC + 1)
     130             : 
     131             : /*
     132             :  * Hashtable key that defines the identity of a hashtable entry.  We separate
     133             :  * queries by user and by database even if they are otherwise identical.
     134             :  *
     135             :  * If you add a new key to this struct, make sure to teach pgss_store() to
     136             :  * zero the padding bytes.  Otherwise, things will break, because pgss_hash is
     137             :  * created using HASH_BLOBS, and thus tag_hash is used to hash this.
     138             : 
     139             :  */
     140             : typedef struct pgssHashKey
     141             : {
     142             :     Oid         userid;         /* user OID */
     143             :     Oid         dbid;           /* database OID */
     144             :     uint64      queryid;        /* query identifier */
     145             :     bool        toplevel;       /* query executed at top level */
     146             : } pgssHashKey;
     147             : 
     148             : /*
     149             :  * The actual stats counters kept within pgssEntry.
     150             :  */
     151             : typedef struct Counters
     152             : {
     153             :     int64       calls[PGSS_NUMKIND];    /* # of times planned/executed */
     154             :     double      total_time[PGSS_NUMKIND];   /* total planning/execution time,
     155             :                                              * in msec */
     156             :     double      min_time[PGSS_NUMKIND]; /* minimum planning/execution time in
     157             :                                          * msec since min/max reset */
     158             :     double      max_time[PGSS_NUMKIND]; /* maximum planning/execution time in
     159             :                                          * msec since min/max reset */
     160             :     double      mean_time[PGSS_NUMKIND];    /* mean planning/execution time in
     161             :                                              * msec */
     162             :     double      sum_var_time[PGSS_NUMKIND]; /* sum of variances in
     163             :                                              * planning/execution time in msec */
     164             :     int64       rows;           /* total # of retrieved or affected rows */
     165             :     int64       shared_blks_hit;    /* # of shared buffer hits */
     166             :     int64       shared_blks_read;   /* # of shared disk blocks read */
     167             :     int64       shared_blks_dirtied;    /* # of shared disk blocks dirtied */
     168             :     int64       shared_blks_written;    /* # of shared disk blocks written */
     169             :     int64       local_blks_hit; /* # of local buffer hits */
     170             :     int64       local_blks_read;    /* # of local disk blocks read */
     171             :     int64       local_blks_dirtied; /* # of local disk blocks dirtied */
     172             :     int64       local_blks_written; /* # of local disk blocks written */
     173             :     int64       temp_blks_read; /* # of temp blocks read */
     174             :     int64       temp_blks_written;  /* # of temp blocks written */
     175             :     double      shared_blk_read_time;   /* time spent reading shared blocks,
     176             :                                          * in msec */
     177             :     double      shared_blk_write_time;  /* time spent writing shared blocks,
     178             :                                          * in msec */
     179             :     double      local_blk_read_time;    /* time spent reading local blocks, in
     180             :                                          * msec */
     181             :     double      local_blk_write_time;   /* time spent writing local blocks, in
     182             :                                          * msec */
     183             :     double      temp_blk_read_time; /* time spent reading temp blocks, in msec */
     184             :     double      temp_blk_write_time;    /* time spent writing temp blocks, in
     185             :                                          * msec */
     186             :     double      usage;          /* usage factor */
     187             :     int64       wal_records;    /* # of WAL records generated */
     188             :     int64       wal_fpi;        /* # of WAL full page images generated */
     189             :     uint64      wal_bytes;      /* total amount of WAL generated in bytes */
     190             :     int64       jit_functions;  /* total number of JIT functions emitted */
     191             :     double      jit_generation_time;    /* total time to generate jit code */
     192             :     int64       jit_inlining_count; /* number of times inlining time has been
     193             :                                      * > 0 */
     194             :     double      jit_deform_time;    /* total time to deform tuples in jit code */
     195             :     int64       jit_deform_count;   /* number of times deform time has been >
     196             :                                      * 0 */
     197             : 
     198             :     double      jit_inlining_time;  /* total time to inline jit code */
     199             :     int64       jit_optimization_count; /* number of times optimization time
     200             :                                          * has been > 0 */
     201             :     double      jit_optimization_time;  /* total time to optimize jit code */
     202             :     int64       jit_emission_count; /* number of times emission time has been
     203             :                                      * > 0 */
     204             :     double      jit_emission_time;  /* total time to emit jit code */
     205             :     int64       parallel_workers_to_launch; /* # of parallel workers planned
     206             :                                              * to be launched */
     207             :     int64       parallel_workers_launched;  /* # of parallel workers actually
     208             :                                              * launched */
     209             : } Counters;
     210             : 
     211             : /*
     212             :  * Global statistics for pg_stat_statements
     213             :  */
     214             : typedef struct pgssGlobalStats
     215             : {
     216             :     int64       dealloc;        /* # of times entries were deallocated */
     217             :     TimestampTz stats_reset;    /* timestamp with all stats reset */
     218             : } pgssGlobalStats;
     219             : 
     220             : /*
     221             :  * Statistics per statement
     222             :  *
     223             :  * Note: in event of a failure in garbage collection of the query text file,
     224             :  * we reset query_offset to zero and query_len to -1.  This will be seen as
     225             :  * an invalid state by qtext_fetch().
     226             :  */
     227             : typedef struct pgssEntry
     228             : {
     229             :     pgssHashKey key;            /* hash key of entry - MUST BE FIRST */
     230             :     Counters    counters;       /* the statistics for this query */
     231             :     Size        query_offset;   /* query text offset in external file */
     232             :     int         query_len;      /* # of valid bytes in query string, or -1 */
     233             :     int         encoding;       /* query text encoding */
     234             :     TimestampTz stats_since;    /* timestamp of entry allocation */
     235             :     TimestampTz minmax_stats_since; /* timestamp of last min/max values reset */
     236             :     slock_t     mutex;          /* protects the counters only */
     237             : } pgssEntry;
     238             : 
     239             : /*
     240             :  * Global shared state
     241             :  */
     242             : typedef struct pgssSharedState
     243             : {
     244             :     LWLock     *lock;           /* protects hashtable search/modification */
     245             :     double      cur_median_usage;   /* current median usage in hashtable */
     246             :     Size        mean_query_len; /* current mean entry text length */
     247             :     slock_t     mutex;          /* protects following fields only: */
     248             :     Size        extent;         /* current extent of query file */
     249             :     int         n_writers;      /* number of active writers to query file */
     250             :     int         gc_count;       /* query file garbage collection cycle count */
     251             :     pgssGlobalStats stats;      /* global statistics for pgss */
     252             : } pgssSharedState;
     253             : 
     254             : /*---- Local variables ----*/
     255             : 
     256             : /* Current nesting depth of planner/ExecutorRun/ProcessUtility calls */
     257             : static int  nesting_level = 0;
     258             : 
     259             : /* Saved hook values */
     260             : static shmem_request_hook_type prev_shmem_request_hook = NULL;
     261             : static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
     262             : static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
     263             : static planner_hook_type prev_planner_hook = NULL;
     264             : static ExecutorStart_hook_type prev_ExecutorStart = NULL;
     265             : static ExecutorRun_hook_type prev_ExecutorRun = NULL;
     266             : static ExecutorFinish_hook_type prev_ExecutorFinish = NULL;
     267             : static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
     268             : static ProcessUtility_hook_type prev_ProcessUtility = NULL;
     269             : 
     270             : /* Links to shared memory state */
     271             : static pgssSharedState *pgss = NULL;
     272             : static HTAB *pgss_hash = NULL;
     273             : 
     274             : /*---- GUC variables ----*/
     275             : 
     276             : typedef enum
     277             : {
     278             :     PGSS_TRACK_NONE,            /* track no statements */
     279             :     PGSS_TRACK_TOP,             /* only top level statements */
     280             :     PGSS_TRACK_ALL,             /* all statements, including nested ones */
     281             : }           PGSSTrackLevel;
     282             : 
     283             : static const struct config_enum_entry track_options[] =
     284             : {
     285             :     {"none", PGSS_TRACK_NONE, false},
     286             :     {"top", PGSS_TRACK_TOP, false},
     287             :     {"all", PGSS_TRACK_ALL, false},
     288             :     {NULL, 0, false}
     289             : };
     290             : 
     291             : static int  pgss_max = 5000;    /* max # statements to track */
     292             : static int  pgss_track = PGSS_TRACK_TOP;    /* tracking level */
     293             : static bool pgss_track_utility = true;  /* whether to track utility commands */
     294             : static bool pgss_track_planning = false;    /* whether to track planning
     295             :                                              * duration */
     296             : static bool pgss_save = true;   /* whether to save stats across shutdown */
     297             : 
     298             : 
     299             : #define pgss_enabled(level) \
     300             :     (!IsParallelWorker() && \
     301             :     (pgss_track == PGSS_TRACK_ALL || \
     302             :     (pgss_track == PGSS_TRACK_TOP && (level) == 0)))
     303             : 
     304             : #define record_gc_qtexts() \
     305             :     do { \
     306             :         SpinLockAcquire(&pgss->mutex); \
     307             :         pgss->gc_count++; \
     308             :         SpinLockRelease(&pgss->mutex); \
     309             :     } while(0)
     310             : 
     311             : /*---- Function declarations ----*/
     312             : 
     313          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_reset);
     314          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_reset_1_7);
     315          36 : PG_FUNCTION_INFO_V1(pg_stat_statements_reset_1_11);
     316           0 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_2);
     317          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_3);
     318          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_8);
     319          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_9);
     320          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
     321          12 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_11);
     322          44 : PG_FUNCTION_INFO_V1(pg_stat_statements_1_12);
     323           0 : PG_FUNCTION_INFO_V1(pg_stat_statements);
     324          14 : PG_FUNCTION_INFO_V1(pg_stat_statements_info);
     325             : 
     326             : static void pgss_shmem_request(void);
     327             : static void pgss_shmem_startup(void);
     328             : static void pgss_shmem_shutdown(int code, Datum arg);
     329             : static void pgss_post_parse_analyze(ParseState *pstate, Query *query,
     330             :                                     JumbleState *jstate);
     331             : static PlannedStmt *pgss_planner(Query *parse,
     332             :                                  const char *query_string,
     333             :                                  int cursorOptions,
     334             :                                  ParamListInfo boundParams);
     335             : static void pgss_ExecutorStart(QueryDesc *queryDesc, int eflags);
     336             : static void pgss_ExecutorRun(QueryDesc *queryDesc,
     337             :                              ScanDirection direction,
     338             :                              uint64 count);
     339             : static void pgss_ExecutorFinish(QueryDesc *queryDesc);
     340             : static void pgss_ExecutorEnd(QueryDesc *queryDesc);
     341             : static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
     342             :                                 bool readOnlyTree,
     343             :                                 ProcessUtilityContext context, ParamListInfo params,
     344             :                                 QueryEnvironment *queryEnv,
     345             :                                 DestReceiver *dest, QueryCompletion *qc);
     346             : static void pgss_store(const char *query, uint64 queryId,
     347             :                        int query_location, int query_len,
     348             :                        pgssStoreKind kind,
     349             :                        double total_time, uint64 rows,
     350             :                        const BufferUsage *bufusage,
     351             :                        const WalUsage *walusage,
     352             :                        const struct JitInstrumentation *jitusage,
     353             :                        JumbleState *jstate,
     354             :                        int parallel_workers_to_launch,
     355             :                        int parallel_workers_launched);
     356             : static void pg_stat_statements_internal(FunctionCallInfo fcinfo,
     357             :                                         pgssVersion api_version,
     358             :                                         bool showtext);
     359             : static Size pgss_memsize(void);
     360             : static pgssEntry *entry_alloc(pgssHashKey *key, Size query_offset, int query_len,
     361             :                               int encoding, bool sticky);
     362             : static void entry_dealloc(void);
     363             : static bool qtext_store(const char *query, int query_len,
     364             :                         Size *query_offset, int *gc_count);
     365             : static char *qtext_load_file(Size *buffer_size);
     366             : static char *qtext_fetch(Size query_offset, int query_len,
     367             :                          char *buffer, Size buffer_size);
     368             : static bool need_gc_qtexts(void);
     369             : static void gc_qtexts(void);
     370             : static TimestampTz entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only);
     371             : static char *generate_normalized_query(JumbleState *jstate, const char *query,
     372             :                                        int query_loc, int *query_len_p);
     373             : static void fill_in_constant_lengths(JumbleState *jstate, const char *query,
     374             :                                      int query_loc);
     375             : static int  comp_location(const void *a, const void *b);
     376             : 
     377             : 
     378             : /*
     379             :  * Module load callback
     380             :  */
     381             : void
     382          16 : _PG_init(void)
     383             : {
     384             :     /*
     385             :      * In order to create our shared memory area, we have to be loaded via
     386             :      * shared_preload_libraries.  If not, fall out without hooking into any of
     387             :      * the main system.  (We don't throw error here because it seems useful to
     388             :      * allow the pg_stat_statements functions to be created even when the
     389             :      * module isn't active.  The functions must protect themselves against
     390             :      * being called then, however.)
     391             :      */
     392          16 :     if (!process_shared_preload_libraries_in_progress)
     393           2 :         return;
     394             : 
     395             :     /*
     396             :      * Inform the postmaster that we want to enable query_id calculation if
     397             :      * compute_query_id is set to auto.
     398             :      */
     399          14 :     EnableQueryId();
     400             : 
     401             :     /*
     402             :      * Define (or redefine) custom GUC variables.
     403             :      */
     404          14 :     DefineCustomIntVariable("pg_stat_statements.max",
     405             :                             "Sets the maximum number of statements tracked by pg_stat_statements.",
     406             :                             NULL,
     407             :                             &pgss_max,
     408             :                             5000,
     409             :                             100,
     410             :                             INT_MAX / 2,
     411             :                             PGC_POSTMASTER,
     412             :                             0,
     413             :                             NULL,
     414             :                             NULL,
     415             :                             NULL);
     416             : 
     417          14 :     DefineCustomEnumVariable("pg_stat_statements.track",
     418             :                              "Selects which statements are tracked by pg_stat_statements.",
     419             :                              NULL,
     420             :                              &pgss_track,
     421             :                              PGSS_TRACK_TOP,
     422             :                              track_options,
     423             :                              PGC_SUSET,
     424             :                              0,
     425             :                              NULL,
     426             :                              NULL,
     427             :                              NULL);
     428             : 
     429          14 :     DefineCustomBoolVariable("pg_stat_statements.track_utility",
     430             :                              "Selects whether utility commands are tracked by pg_stat_statements.",
     431             :                              NULL,
     432             :                              &pgss_track_utility,
     433             :                              true,
     434             :                              PGC_SUSET,
     435             :                              0,
     436             :                              NULL,
     437             :                              NULL,
     438             :                              NULL);
     439             : 
     440          14 :     DefineCustomBoolVariable("pg_stat_statements.track_planning",
     441             :                              "Selects whether planning duration is tracked by pg_stat_statements.",
     442             :                              NULL,
     443             :                              &pgss_track_planning,
     444             :                              false,
     445             :                              PGC_SUSET,
     446             :                              0,
     447             :                              NULL,
     448             :                              NULL,
     449             :                              NULL);
     450             : 
     451          14 :     DefineCustomBoolVariable("pg_stat_statements.save",
     452             :                              "Save pg_stat_statements statistics across server shutdowns.",
     453             :                              NULL,
     454             :                              &pgss_save,
     455             :                              true,
     456             :                              PGC_SIGHUP,
     457             :                              0,
     458             :                              NULL,
     459             :                              NULL,
     460             :                              NULL);
     461             : 
     462          14 :     MarkGUCPrefixReserved("pg_stat_statements");
     463             : 
     464             :     /*
     465             :      * Install hooks.
     466             :      */
     467          14 :     prev_shmem_request_hook = shmem_request_hook;
     468          14 :     shmem_request_hook = pgss_shmem_request;
     469          14 :     prev_shmem_startup_hook = shmem_startup_hook;
     470          14 :     shmem_startup_hook = pgss_shmem_startup;
     471          14 :     prev_post_parse_analyze_hook = post_parse_analyze_hook;
     472          14 :     post_parse_analyze_hook = pgss_post_parse_analyze;
     473          14 :     prev_planner_hook = planner_hook;
     474          14 :     planner_hook = pgss_planner;
     475          14 :     prev_ExecutorStart = ExecutorStart_hook;
     476          14 :     ExecutorStart_hook = pgss_ExecutorStart;
     477          14 :     prev_ExecutorRun = ExecutorRun_hook;
     478          14 :     ExecutorRun_hook = pgss_ExecutorRun;
     479          14 :     prev_ExecutorFinish = ExecutorFinish_hook;
     480          14 :     ExecutorFinish_hook = pgss_ExecutorFinish;
     481          14 :     prev_ExecutorEnd = ExecutorEnd_hook;
     482          14 :     ExecutorEnd_hook = pgss_ExecutorEnd;
     483          14 :     prev_ProcessUtility = ProcessUtility_hook;
     484          14 :     ProcessUtility_hook = pgss_ProcessUtility;
     485             : }
     486             : 
     487             : /*
     488             :  * shmem_request hook: request additional shared resources.  We'll allocate or
     489             :  * attach to the shared resources in pgss_shmem_startup().
     490             :  */
     491             : static void
     492          14 : pgss_shmem_request(void)
     493             : {
     494          14 :     if (prev_shmem_request_hook)
     495           0 :         prev_shmem_request_hook();
     496             : 
     497          14 :     RequestAddinShmemSpace(pgss_memsize());
     498          14 :     RequestNamedLWLockTranche("pg_stat_statements", 1);
     499          14 : }
     500             : 
     501             : /*
     502             :  * shmem_startup hook: allocate or attach to shared memory,
     503             :  * then load any pre-existing statistics from file.
     504             :  * Also create and load the query-texts file, which is expected to exist
     505             :  * (even if empty) while the module is enabled.
     506             :  */
     507             : static void
     508          14 : pgss_shmem_startup(void)
     509             : {
     510             :     bool        found;
     511             :     HASHCTL     info;
     512          14 :     FILE       *file = NULL;
     513          14 :     FILE       *qfile = NULL;
     514             :     uint32      header;
     515             :     int32       num;
     516             :     int32       pgver;
     517             :     int32       i;
     518             :     int         buffer_size;
     519          14 :     char       *buffer = NULL;
     520             : 
     521          14 :     if (prev_shmem_startup_hook)
     522           0 :         prev_shmem_startup_hook();
     523             : 
     524             :     /* reset in case this is a restart within the postmaster */
     525          14 :     pgss = NULL;
     526          14 :     pgss_hash = NULL;
     527             : 
     528             :     /*
     529             :      * Create or attach to the shared memory state, including hash table
     530             :      */
     531          14 :     LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
     532             : 
     533          14 :     pgss = ShmemInitStruct("pg_stat_statements",
     534             :                            sizeof(pgssSharedState),
     535             :                            &found);
     536             : 
     537          14 :     if (!found)
     538             :     {
     539             :         /* First time through ... */
     540          14 :         pgss->lock = &(GetNamedLWLockTranche("pg_stat_statements"))->lock;
     541          14 :         pgss->cur_median_usage = ASSUMED_MEDIAN_INIT;
     542          14 :         pgss->mean_query_len = ASSUMED_LENGTH_INIT;
     543          14 :         SpinLockInit(&pgss->mutex);
     544          14 :         pgss->extent = 0;
     545          14 :         pgss->n_writers = 0;
     546          14 :         pgss->gc_count = 0;
     547          14 :         pgss->stats.dealloc = 0;
     548          14 :         pgss->stats.stats_reset = GetCurrentTimestamp();
     549             :     }
     550             : 
     551          14 :     info.keysize = sizeof(pgssHashKey);
     552          14 :     info.entrysize = sizeof(pgssEntry);
     553          14 :     pgss_hash = ShmemInitHash("pg_stat_statements hash",
     554             :                               pgss_max, pgss_max,
     555             :                               &info,
     556             :                               HASH_ELEM | HASH_BLOBS);
     557             : 
     558          14 :     LWLockRelease(AddinShmemInitLock);
     559             : 
     560             :     /*
     561             :      * If we're in the postmaster (or a standalone backend...), set up a shmem
     562             :      * exit hook to dump the statistics to disk.
     563             :      */
     564          14 :     if (!IsUnderPostmaster)
     565          14 :         on_shmem_exit(pgss_shmem_shutdown, (Datum) 0);
     566             : 
     567             :     /*
     568             :      * Done if some other process already completed our initialization.
     569             :      */
     570          14 :     if (found)
     571          14 :         return;
     572             : 
     573             :     /*
     574             :      * Note: we don't bother with locks here, because there should be no other
     575             :      * processes running when this code is reached.
     576             :      */
     577             : 
     578             :     /* Unlink query text file possibly left over from crash */
     579          14 :     unlink(PGSS_TEXT_FILE);
     580             : 
     581             :     /* Allocate new query text temp file */
     582          14 :     qfile = AllocateFile(PGSS_TEXT_FILE, PG_BINARY_W);
     583          14 :     if (qfile == NULL)
     584           0 :         goto write_error;
     585             : 
     586             :     /*
     587             :      * If we were told not to load old statistics, we're done.  (Note we do
     588             :      * not try to unlink any old dump file in this case.  This seems a bit
     589             :      * questionable but it's the historical behavior.)
     590             :      */
     591          14 :     if (!pgss_save)
     592             :     {
     593           2 :         FreeFile(qfile);
     594           2 :         return;
     595             :     }
     596             : 
     597             :     /*
     598             :      * Attempt to load old statistics from the dump file.
     599             :      */
     600          12 :     file = AllocateFile(PGSS_DUMP_FILE, PG_BINARY_R);
     601          12 :     if (file == NULL)
     602             :     {
     603           8 :         if (errno != ENOENT)
     604           0 :             goto read_error;
     605             :         /* No existing persisted stats file, so we're done */
     606           8 :         FreeFile(qfile);
     607           8 :         return;
     608             :     }
     609             : 
     610           4 :     buffer_size = 2048;
     611           4 :     buffer = (char *) palloc(buffer_size);
     612             : 
     613           8 :     if (fread(&header, sizeof(uint32), 1, file) != 1 ||
     614           8 :         fread(&pgver, sizeof(uint32), 1, file) != 1 ||
     615           4 :         fread(&num, sizeof(int32), 1, file) != 1)
     616           0 :         goto read_error;
     617             : 
     618           4 :     if (header != PGSS_FILE_HEADER ||
     619           4 :         pgver != PGSS_PG_MAJOR_VERSION)
     620           0 :         goto data_error;
     621             : 
     622       51664 :     for (i = 0; i < num; i++)
     623             :     {
     624             :         pgssEntry   temp;
     625             :         pgssEntry  *entry;
     626             :         Size        query_offset;
     627             : 
     628       51660 :         if (fread(&temp, sizeof(pgssEntry), 1, file) != 1)
     629           0 :             goto read_error;
     630             : 
     631             :         /* Encoding is the only field we can easily sanity-check */
     632       51660 :         if (!PG_VALID_BE_ENCODING(temp.encoding))
     633           0 :             goto data_error;
     634             : 
     635             :         /* Resize buffer as needed */
     636       51660 :         if (temp.query_len >= buffer_size)
     637             :         {
     638           6 :             buffer_size = Max(buffer_size * 2, temp.query_len + 1);
     639           6 :             buffer = repalloc(buffer, buffer_size);
     640             :         }
     641             : 
     642       51660 :         if (fread(buffer, 1, temp.query_len + 1, file) != temp.query_len + 1)
     643           0 :             goto read_error;
     644             : 
     645             :         /* Should have a trailing null, but let's make sure */
     646       51660 :         buffer[temp.query_len] = '\0';
     647             : 
     648             :         /* Skip loading "sticky" entries */
     649       51660 :         if (IS_STICKY(temp.counters))
     650        1458 :             continue;
     651             : 
     652             :         /* Store the query text */
     653       50202 :         query_offset = pgss->extent;
     654       50202 :         if (fwrite(buffer, 1, temp.query_len + 1, qfile) != temp.query_len + 1)
     655           0 :             goto write_error;
     656       50202 :         pgss->extent += temp.query_len + 1;
     657             : 
     658             :         /* make the hashtable entry (discards old entries if too many) */
     659       50202 :         entry = entry_alloc(&temp.key, query_offset, temp.query_len,
     660             :                             temp.encoding,
     661             :                             false);
     662             : 
     663             :         /* copy in the actual stats */
     664       50202 :         entry->counters = temp.counters;
     665       50202 :         entry->stats_since = temp.stats_since;
     666       50202 :         entry->minmax_stats_since = temp.minmax_stats_since;
     667             :     }
     668             : 
     669             :     /* Read global statistics for pg_stat_statements */
     670           4 :     if (fread(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
     671           0 :         goto read_error;
     672             : 
     673           4 :     pfree(buffer);
     674           4 :     FreeFile(file);
     675           4 :     FreeFile(qfile);
     676             : 
     677             :     /*
     678             :      * Remove the persisted stats file so it's not included in
     679             :      * backups/replication standbys, etc.  A new file will be written on next
     680             :      * shutdown.
     681             :      *
     682             :      * Note: it's okay if the PGSS_TEXT_FILE is included in a basebackup,
     683             :      * because we remove that file on startup; it acts inversely to
     684             :      * PGSS_DUMP_FILE, in that it is only supposed to be around when the
     685             :      * server is running, whereas PGSS_DUMP_FILE is only supposed to be around
     686             :      * when the server is not running.  Leaving the file creates no danger of
     687             :      * a newly restored database having a spurious record of execution costs,
     688             :      * which is what we're really concerned about here.
     689             :      */
     690           4 :     unlink(PGSS_DUMP_FILE);
     691             : 
     692           4 :     return;
     693             : 
     694           0 : read_error:
     695           0 :     ereport(LOG,
     696             :             (errcode_for_file_access(),
     697             :              errmsg("could not read file \"%s\": %m",
     698             :                     PGSS_DUMP_FILE)));
     699           0 :     goto fail;
     700           0 : data_error:
     701           0 :     ereport(LOG,
     702             :             (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
     703             :              errmsg("ignoring invalid data in file \"%s\"",
     704             :                     PGSS_DUMP_FILE)));
     705           0 :     goto fail;
     706           0 : write_error:
     707           0 :     ereport(LOG,
     708             :             (errcode_for_file_access(),
     709             :              errmsg("could not write file \"%s\": %m",
     710             :                     PGSS_TEXT_FILE)));
     711           0 : fail:
     712           0 :     if (buffer)
     713           0 :         pfree(buffer);
     714           0 :     if (file)
     715           0 :         FreeFile(file);
     716           0 :     if (qfile)
     717           0 :         FreeFile(qfile);
     718             :     /* If possible, throw away the bogus file; ignore any error */
     719           0 :     unlink(PGSS_DUMP_FILE);
     720             : 
     721             :     /*
     722             :      * Don't unlink PGSS_TEXT_FILE here; it should always be around while the
     723             :      * server is running with pg_stat_statements enabled
     724             :      */
     725             : }
     726             : 
     727             : /*
     728             :  * shmem_shutdown hook: Dump statistics into file.
     729             :  *
     730             :  * Note: we don't bother with acquiring lock, because there should be no
     731             :  * other processes running when this is called.
     732             :  */
     733             : static void
     734          14 : pgss_shmem_shutdown(int code, Datum arg)
     735             : {
     736             :     FILE       *file;
     737          14 :     char       *qbuffer = NULL;
     738          14 :     Size        qbuffer_size = 0;
     739             :     HASH_SEQ_STATUS hash_seq;
     740             :     int32       num_entries;
     741             :     pgssEntry  *entry;
     742             : 
     743             :     /* Don't try to dump during a crash. */
     744          14 :     if (code)
     745          14 :         return;
     746             : 
     747             :     /* Safety check ... shouldn't get here unless shmem is set up. */
     748          14 :     if (!pgss || !pgss_hash)
     749           0 :         return;
     750             : 
     751             :     /* Don't dump if told not to. */
     752          14 :     if (!pgss_save)
     753           4 :         return;
     754             : 
     755          10 :     file = AllocateFile(PGSS_DUMP_FILE ".tmp", PG_BINARY_W);
     756          10 :     if (file == NULL)
     757           0 :         goto error;
     758             : 
     759          10 :     if (fwrite(&PGSS_FILE_HEADER, sizeof(uint32), 1, file) != 1)
     760           0 :         goto error;
     761          10 :     if (fwrite(&PGSS_PG_MAJOR_VERSION, sizeof(uint32), 1, file) != 1)
     762           0 :         goto error;
     763          10 :     num_entries = hash_get_num_entries(pgss_hash);
     764          10 :     if (fwrite(&num_entries, sizeof(int32), 1, file) != 1)
     765           0 :         goto error;
     766             : 
     767          10 :     qbuffer = qtext_load_file(&qbuffer_size);
     768          10 :     if (qbuffer == NULL)
     769           0 :         goto error;
     770             : 
     771             :     /*
     772             :      * When serializing to disk, we store query texts immediately after their
     773             :      * entry data.  Any orphaned query texts are thereby excluded.
     774             :      */
     775          10 :     hash_seq_init(&hash_seq, pgss_hash);
     776      103744 :     while ((entry = hash_seq_search(&hash_seq)) != NULL)
     777             :     {
     778      103734 :         int         len = entry->query_len;
     779      103734 :         char       *qstr = qtext_fetch(entry->query_offset, len,
     780             :                                        qbuffer, qbuffer_size);
     781             : 
     782      103734 :         if (qstr == NULL)
     783           0 :             continue;           /* Ignore any entries with bogus texts */
     784             : 
     785      103734 :         if (fwrite(entry, sizeof(pgssEntry), 1, file) != 1 ||
     786      103734 :             fwrite(qstr, 1, len + 1, file) != len + 1)
     787             :         {
     788             :             /* note: we assume hash_seq_term won't change errno */
     789           0 :             hash_seq_term(&hash_seq);
     790           0 :             goto error;
     791             :         }
     792             :     }
     793             : 
     794             :     /* Dump global statistics for pg_stat_statements */
     795          10 :     if (fwrite(&pgss->stats, sizeof(pgssGlobalStats), 1, file) != 1)
     796           0 :         goto error;
     797             : 
     798          10 :     free(qbuffer);
     799          10 :     qbuffer = NULL;
     800             : 
     801          10 :     if (FreeFile(file))
     802             :     {
     803           0 :         file = NULL;
     804           0 :         goto error;
     805             :     }
     806             : 
     807             :     /*
     808             :      * Rename file into place, so we atomically replace any old one.
     809             :      */
     810          10 :     (void) durable_rename(PGSS_DUMP_FILE ".tmp", PGSS_DUMP_FILE, LOG);
     811             : 
     812             :     /* Unlink query-texts file; it's not needed while shutdown */
     813          10 :     unlink(PGSS_TEXT_FILE);
     814             : 
     815          10 :     return;
     816             : 
     817           0 : error:
     818           0 :     ereport(LOG,
     819             :             (errcode_for_file_access(),
     820             :              errmsg("could not write file \"%s\": %m",
     821             :                     PGSS_DUMP_FILE ".tmp")));
     822           0 :     free(qbuffer);
     823           0 :     if (file)
     824           0 :         FreeFile(file);
     825           0 :     unlink(PGSS_DUMP_FILE ".tmp");
     826           0 :     unlink(PGSS_TEXT_FILE);
     827             : }
     828             : 
     829             : /*
     830             :  * Post-parse-analysis hook: mark query with a queryId
     831             :  */
     832             : static void
     833      148770 : pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
     834             : {
     835      148770 :     if (prev_post_parse_analyze_hook)
     836           0 :         prev_post_parse_analyze_hook(pstate, query, jstate);
     837             : 
     838             :     /* Safety check... */
     839      148770 :     if (!pgss || !pgss_hash || !pgss_enabled(nesting_level))
     840       25376 :         return;
     841             : 
     842             :     /*
     843             :      * If it's EXECUTE, clear the queryId so that stats will accumulate for
     844             :      * the underlying PREPARE.  But don't do this if we're not tracking
     845             :      * utility statements, to avoid messing up another extension that might be
     846             :      * tracking them.
     847             :      */
     848      123394 :     if (query->utilityStmt)
     849             :     {
     850       55130 :         if (pgss_track_utility && IsA(query->utilityStmt, ExecuteStmt))
     851             :         {
     852        6348 :             query->queryId = UINT64CONST(0);
     853        6348 :             return;
     854             :         }
     855             :     }
     856             : 
     857             :     /*
     858             :      * If query jumbling were able to identify any ignorable constants, we
     859             :      * immediately create a hash table entry for the query, so that we can
     860             :      * record the normalized form of the query string.  If there were no such
     861             :      * constants, the normalized string would be the same as the query text
     862             :      * anyway, so there's no need for an early entry.
     863             :      */
     864      117046 :     if (jstate && jstate->clocations_count > 0)
     865       67398 :         pgss_store(pstate->p_sourcetext,
     866             :                    query->queryId,
     867             :                    query->stmt_location,
     868             :                    query->stmt_len,
     869             :                    PGSS_INVALID,
     870             :                    0,
     871             :                    0,
     872             :                    NULL,
     873             :                    NULL,
     874             :                    NULL,
     875             :                    jstate,
     876             :                    0,
     877             :                    0);
     878             : }
     879             : 
     880             : /*
     881             :  * Planner hook: forward to regular planner, but measure planning time
     882             :  * if needed.
     883             :  */
     884             : static PlannedStmt *
     885       91660 : pgss_planner(Query *parse,
     886             :              const char *query_string,
     887             :              int cursorOptions,
     888             :              ParamListInfo boundParams)
     889             : {
     890             :     PlannedStmt *result;
     891             : 
     892             :     /*
     893             :      * We can't process the query if no query_string is provided, as
     894             :      * pgss_store needs it.  We also ignore query without queryid, as it would
     895             :      * be treated as a utility statement, which may not be the case.
     896             :      */
     897       91660 :     if (pgss_enabled(nesting_level)
     898       68572 :         && pgss_track_planning && query_string
     899         146 :         && parse->queryId != UINT64CONST(0))
     900         146 :     {
     901             :         instr_time  start;
     902             :         instr_time  duration;
     903             :         BufferUsage bufusage_start,
     904             :                     bufusage;
     905             :         WalUsage    walusage_start,
     906             :                     walusage;
     907             : 
     908             :         /* We need to track buffer usage as the planner can access them. */
     909         146 :         bufusage_start = pgBufferUsage;
     910             : 
     911             :         /*
     912             :          * Similarly the planner could write some WAL records in some cases
     913             :          * (e.g. setting a hint bit with those being WAL-logged)
     914             :          */
     915         146 :         walusage_start = pgWalUsage;
     916         146 :         INSTR_TIME_SET_CURRENT(start);
     917             : 
     918         146 :         nesting_level++;
     919         146 :         PG_TRY();
     920             :         {
     921         146 :             if (prev_planner_hook)
     922           0 :                 result = prev_planner_hook(parse, query_string, cursorOptions,
     923             :                                            boundParams);
     924             :             else
     925         146 :                 result = standard_planner(parse, query_string, cursorOptions,
     926             :                                           boundParams);
     927             :         }
     928           0 :         PG_FINALLY();
     929             :         {
     930         146 :             nesting_level--;
     931             :         }
     932         146 :         PG_END_TRY();
     933             : 
     934         146 :         INSTR_TIME_SET_CURRENT(duration);
     935         146 :         INSTR_TIME_SUBTRACT(duration, start);
     936             : 
     937             :         /* calc differences of buffer counters. */
     938         146 :         memset(&bufusage, 0, sizeof(BufferUsage));
     939         146 :         BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
     940             : 
     941             :         /* calc differences of WAL counters. */
     942         146 :         memset(&walusage, 0, sizeof(WalUsage));
     943         146 :         WalUsageAccumDiff(&walusage, &pgWalUsage, &walusage_start);
     944             : 
     945         146 :         pgss_store(query_string,
     946             :                    parse->queryId,
     947             :                    parse->stmt_location,
     948             :                    parse->stmt_len,
     949             :                    PGSS_PLAN,
     950         146 :                    INSTR_TIME_GET_MILLISEC(duration),
     951             :                    0,
     952             :                    &bufusage,
     953             :                    &walusage,
     954             :                    NULL,
     955             :                    NULL,
     956             :                    0,
     957             :                    0);
     958             :     }
     959             :     else
     960             :     {
     961             :         /*
     962             :          * Even though we're not tracking plan time for this statement, we
     963             :          * must still increment the nesting level, to ensure that functions
     964             :          * evaluated during planning are not seen as top-level calls.
     965             :          */
     966       91514 :         nesting_level++;
     967       91514 :         PG_TRY();
     968             :         {
     969       91514 :             if (prev_planner_hook)
     970           0 :                 result = prev_planner_hook(parse, query_string, cursorOptions,
     971             :                                            boundParams);
     972             :             else
     973       91514 :                 result = standard_planner(parse, query_string, cursorOptions,
     974             :                                           boundParams);
     975             :         }
     976        1314 :         PG_FINALLY();
     977             :         {
     978       91514 :             nesting_level--;
     979             :         }
     980       91514 :         PG_END_TRY();
     981             :     }
     982             : 
     983       90346 :     return result;
     984             : }
     985             : 
     986             : /*
     987             :  * ExecutorStart hook: start up tracking if needed
     988             :  */
     989             : static void
     990      109692 : pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
     991             : {
     992      109692 :     if (prev_ExecutorStart)
     993           0 :         prev_ExecutorStart(queryDesc, eflags);
     994             :     else
     995      109692 :         standard_ExecutorStart(queryDesc, eflags);
     996             : 
     997             :     /*
     998             :      * If query has queryId zero, don't track it.  This prevents double
     999             :      * counting of optimizable statements that are directly contained in
    1000             :      * utility statements.
    1001             :      */
    1002      109068 :     if (pgss_enabled(nesting_level) && queryDesc->plannedstmt->queryId != UINT64CONST(0))
    1003             :     {
    1004             :         /*
    1005             :          * Set up to track total elapsed time in ExecutorRun.  Make sure the
    1006             :          * space is allocated in the per-query context so it will go away at
    1007             :          * ExecutorEnd.
    1008             :          */
    1009       72432 :         if (queryDesc->totaltime == NULL)
    1010             :         {
    1011             :             MemoryContext oldcxt;
    1012             : 
    1013       72432 :             oldcxt = MemoryContextSwitchTo(queryDesc->estate->es_query_cxt);
    1014       72432 :             queryDesc->totaltime = InstrAlloc(1, INSTRUMENT_ALL, false);
    1015       72432 :             MemoryContextSwitchTo(oldcxt);
    1016             :         }
    1017             :     }
    1018      109068 : }
    1019             : 
    1020             : /*
    1021             :  * ExecutorRun hook: all we need do is track nesting depth
    1022             :  */
    1023             : static void
    1024      106812 : pgss_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
    1025             : {
    1026      106812 :     nesting_level++;
    1027      106812 :     PG_TRY();
    1028             :     {
    1029      106812 :         if (prev_ExecutorRun)
    1030           0 :             prev_ExecutorRun(queryDesc, direction, count);
    1031             :         else
    1032      106812 :             standard_ExecutorRun(queryDesc, direction, count);
    1033             :     }
    1034        6850 :     PG_FINALLY();
    1035             :     {
    1036      106812 :         nesting_level--;
    1037             :     }
    1038      106812 :     PG_END_TRY();
    1039       99962 : }
    1040             : 
    1041             : /*
    1042             :  * ExecutorFinish hook: all we need do is track nesting depth
    1043             :  */
    1044             : static void
    1045       96706 : pgss_ExecutorFinish(QueryDesc *queryDesc)
    1046             : {
    1047       96706 :     nesting_level++;
    1048       96706 :     PG_TRY();
    1049             :     {
    1050       96706 :         if (prev_ExecutorFinish)
    1051           0 :             prev_ExecutorFinish(queryDesc);
    1052             :         else
    1053       96706 :             standard_ExecutorFinish(queryDesc);
    1054             :     }
    1055         330 :     PG_FINALLY();
    1056             :     {
    1057       96706 :         nesting_level--;
    1058             :     }
    1059       96706 :     PG_END_TRY();
    1060       96376 : }
    1061             : 
    1062             : /*
    1063             :  * ExecutorEnd hook: store results if needed
    1064             :  */
    1065             : static void
    1066      101808 : pgss_ExecutorEnd(QueryDesc *queryDesc)
    1067             : {
    1068      101808 :     uint64      queryId = queryDesc->plannedstmt->queryId;
    1069             : 
    1070      101808 :     if (queryId != UINT64CONST(0) && queryDesc->totaltime &&
    1071       69450 :         pgss_enabled(nesting_level))
    1072             :     {
    1073             :         /*
    1074             :          * Make sure stats accumulation is done.  (Note: it's okay if several
    1075             :          * levels of hook all do this.)
    1076             :          */
    1077       69450 :         InstrEndLoop(queryDesc->totaltime);
    1078             : 
    1079       69292 :         pgss_store(queryDesc->sourceText,
    1080             :                    queryId,
    1081       69450 :                    queryDesc->plannedstmt->stmt_location,
    1082       69450 :                    queryDesc->plannedstmt->stmt_len,
    1083             :                    PGSS_EXEC,
    1084       69450 :                    queryDesc->totaltime->total * 1000.0,  /* convert to msec */
    1085       69450 :                    queryDesc->estate->es_total_processed,
    1086       69450 :                    &queryDesc->totaltime->bufusage,
    1087       69450 :                    &queryDesc->totaltime->walusage,
    1088         158 :                    queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL,
    1089             :                    NULL,
    1090       69450 :                    queryDesc->estate->es_parallel_workers_to_launch,
    1091       69450 :                    queryDesc->estate->es_parallel_workers_launched);
    1092             :     }
    1093             : 
    1094      101808 :     if (prev_ExecutorEnd)
    1095           0 :         prev_ExecutorEnd(queryDesc);
    1096             :     else
    1097      101808 :         standard_ExecutorEnd(queryDesc);
    1098      101808 : }
    1099             : 
    1100             : /*
    1101             :  * ProcessUtility hook
    1102             :  */
    1103             : static void
    1104       64402 : pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
    1105             :                     bool readOnlyTree,
    1106             :                     ProcessUtilityContext context,
    1107             :                     ParamListInfo params, QueryEnvironment *queryEnv,
    1108             :                     DestReceiver *dest, QueryCompletion *qc)
    1109             : {
    1110       64402 :     Node       *parsetree = pstmt->utilityStmt;
    1111       64402 :     uint64      saved_queryId = pstmt->queryId;
    1112       64402 :     int         saved_stmt_location = pstmt->stmt_location;
    1113       64402 :     int         saved_stmt_len = pstmt->stmt_len;
    1114       64402 :     bool        enabled = pgss_track_utility && pgss_enabled(nesting_level);
    1115             : 
    1116             :     /*
    1117             :      * Force utility statements to get queryId zero.  We do this even in cases
    1118             :      * where the statement contains an optimizable statement for which a
    1119             :      * queryId could be derived (such as EXPLAIN or DECLARE CURSOR).  For such
    1120             :      * cases, runtime control will first go through ProcessUtility and then
    1121             :      * the executor, and we don't want the executor hooks to do anything,
    1122             :      * since we are already measuring the statement's costs at the utility
    1123             :      * level.
    1124             :      *
    1125             :      * Note that this is only done if pg_stat_statements is enabled and
    1126             :      * configured to track utility statements, in the unlikely possibility
    1127             :      * that user configured another extension to handle utility statements
    1128             :      * only.
    1129             :      */
    1130       64402 :     if (enabled)
    1131       54978 :         pstmt->queryId = UINT64CONST(0);
    1132             : 
    1133             :     /*
    1134             :      * If it's an EXECUTE statement, we don't track it and don't increment the
    1135             :      * nesting level.  This allows the cycles to be charged to the underlying
    1136             :      * PREPARE instead (by the Executor hooks), which is much more useful.
    1137             :      *
    1138             :      * We also don't track execution of PREPARE.  If we did, we would get one
    1139             :      * hash table entry for the PREPARE (with hash calculated from the query
    1140             :      * string), and then a different one with the same query string (but hash
    1141             :      * calculated from the query tree) would be used to accumulate costs of
    1142             :      * ensuing EXECUTEs.  This would be confusing.  Since PREPARE doesn't
    1143             :      * actually run the planner (only parse+rewrite), its costs are generally
    1144             :      * pretty negligible and it seems okay to just ignore it.
    1145             :      */
    1146       64402 :     if (enabled &&
    1147       54978 :         !IsA(parsetree, ExecuteStmt) &&
    1148       48634 :         !IsA(parsetree, PrepareStmt))
    1149       43912 :     {
    1150             :         instr_time  start;
    1151             :         instr_time  duration;
    1152             :         uint64      rows;
    1153             :         BufferUsage bufusage_start,
    1154             :                     bufusage;
    1155             :         WalUsage    walusage_start,
    1156             :                     walusage;
    1157             : 
    1158       48402 :         bufusage_start = pgBufferUsage;
    1159       48402 :         walusage_start = pgWalUsage;
    1160       48402 :         INSTR_TIME_SET_CURRENT(start);
    1161             : 
    1162       48402 :         nesting_level++;
    1163       48402 :         PG_TRY();
    1164             :         {
    1165       48402 :             if (prev_ProcessUtility)
    1166           0 :                 prev_ProcessUtility(pstmt, queryString, readOnlyTree,
    1167             :                                     context, params, queryEnv,
    1168             :                                     dest, qc);
    1169             :             else
    1170       48402 :                 standard_ProcessUtility(pstmt, queryString, readOnlyTree,
    1171             :                                         context, params, queryEnv,
    1172             :                                         dest, qc);
    1173             :         }
    1174        4490 :         PG_FINALLY();
    1175             :         {
    1176       48402 :             nesting_level--;
    1177             :         }
    1178       48402 :         PG_END_TRY();
    1179             : 
    1180             :         /*
    1181             :          * CAUTION: do not access the *pstmt data structure again below here.
    1182             :          * If it was a ROLLBACK or similar, that data structure may have been
    1183             :          * freed.  We must copy everything we still need into local variables,
    1184             :          * which we did above.
    1185             :          *
    1186             :          * For the same reason, we can't risk restoring pstmt->queryId to its
    1187             :          * former value, which'd otherwise be a good idea.
    1188             :          */
    1189             : 
    1190       43912 :         INSTR_TIME_SET_CURRENT(duration);
    1191       43912 :         INSTR_TIME_SUBTRACT(duration, start);
    1192             : 
    1193             :         /*
    1194             :          * Track the total number of rows retrieved or affected by the utility
    1195             :          * statements of COPY, FETCH, CREATE TABLE AS, CREATE MATERIALIZED
    1196             :          * VIEW, REFRESH MATERIALIZED VIEW and SELECT INTO.
    1197             :          */
    1198       43906 :         rows = (qc && (qc->commandTag == CMDTAG_COPY ||
    1199       40902 :                        qc->commandTag == CMDTAG_FETCH ||
    1200       40436 :                        qc->commandTag == CMDTAG_SELECT ||
    1201       40068 :                        qc->commandTag == CMDTAG_REFRESH_MATERIALIZED_VIEW)) ?
    1202       87818 :             qc->nprocessed : 0;
    1203             : 
    1204             :         /* calc differences of buffer counters. */
    1205       43912 :         memset(&bufusage, 0, sizeof(BufferUsage));
    1206       43912 :         BufferUsageAccumDiff(&bufusage, &pgBufferUsage, &bufusage_start);
    1207             : 
    1208             :         /* calc differences of WAL counters. */
    1209       43912 :         memset(&walusage, 0, sizeof(WalUsage));
    1210       43912 :         WalUsageAccumDiff(&walusage, &pgWalUsage, &walusage_start);
    1211             : 
    1212       43912 :         pgss_store(queryString,
    1213             :                    saved_queryId,
    1214             :                    saved_stmt_location,
    1215             :                    saved_stmt_len,
    1216             :                    PGSS_EXEC,
    1217       43912 :                    INSTR_TIME_GET_MILLISEC(duration),
    1218             :                    rows,
    1219             :                    &bufusage,
    1220             :                    &walusage,
    1221             :                    NULL,
    1222             :                    NULL,
    1223             :                    0,
    1224             :                    0);
    1225             :     }
    1226             :     else
    1227             :     {
    1228             :         /*
    1229             :          * Even though we're not tracking execution time for this statement,
    1230             :          * we must still increment the nesting level, to ensure that functions
    1231             :          * evaluated within it are not seen as top-level calls.  But don't do
    1232             :          * so for EXECUTE; that way, when control reaches pgss_planner or
    1233             :          * pgss_ExecutorStart, we will treat the costs as top-level if
    1234             :          * appropriate.  Likewise, don't bump for PREPARE, so that parse
    1235             :          * analysis will treat the statement as top-level if appropriate.
    1236             :          *
    1237             :          * To be absolutely certain we don't mess up the nesting level,
    1238             :          * evaluate the bump_level condition just once.
    1239             :          */
    1240       16000 :         bool        bump_level =
    1241       25654 :             !IsA(parsetree, ExecuteStmt) &&
    1242        9654 :             !IsA(parsetree, PrepareStmt);
    1243             : 
    1244       16000 :         if (bump_level)
    1245        9420 :             nesting_level++;
    1246       16000 :         PG_TRY();
    1247             :         {
    1248       16000 :             if (prev_ProcessUtility)
    1249           0 :                 prev_ProcessUtility(pstmt, queryString, readOnlyTree,
    1250             :                                     context, params, queryEnv,
    1251             :                                     dest, qc);
    1252             :             else
    1253       16000 :                 standard_ProcessUtility(pstmt, queryString, readOnlyTree,
    1254             :                                         context, params, queryEnv,
    1255             :                                         dest, qc);
    1256             :         }
    1257         240 :         PG_FINALLY();
    1258             :         {
    1259       16000 :             if (bump_level)
    1260        9420 :                 nesting_level--;
    1261             :         }
    1262       16000 :         PG_END_TRY();
    1263             :     }
    1264       59672 : }
    1265             : 
    1266             : /*
    1267             :  * Store some statistics for a statement.
    1268             :  *
    1269             :  * If jstate is not NULL then we're trying to create an entry for which
    1270             :  * we have no statistics as yet; we just want to record the normalized
    1271             :  * query string.  total_time, rows, bufusage and walusage are ignored in this
    1272             :  * case.
    1273             :  *
    1274             :  * If kind is PGSS_PLAN or PGSS_EXEC, its value is used as the array position
    1275             :  * for the arrays in the Counters field.
    1276             :  */
    1277             : static void
    1278      180906 : pgss_store(const char *query, uint64 queryId,
    1279             :            int query_location, int query_len,
    1280             :            pgssStoreKind kind,
    1281             :            double total_time, uint64 rows,
    1282             :            const BufferUsage *bufusage,
    1283             :            const WalUsage *walusage,
    1284             :            const struct JitInstrumentation *jitusage,
    1285             :            JumbleState *jstate,
    1286             :            int parallel_workers_to_launch,
    1287             :            int parallel_workers_launched)
    1288             : {
    1289             :     pgssHashKey key;
    1290             :     pgssEntry  *entry;
    1291      180906 :     char       *norm_query = NULL;
    1292      180906 :     int         encoding = GetDatabaseEncoding();
    1293             : 
    1294             :     Assert(query != NULL);
    1295             : 
    1296             :     /* Safety check... */
    1297      180906 :     if (!pgss || !pgss_hash)
    1298           0 :         return;
    1299             : 
    1300             :     /*
    1301             :      * Nothing to do if compute_query_id isn't enabled and no other module
    1302             :      * computed a query identifier.
    1303             :      */
    1304      180906 :     if (queryId == UINT64CONST(0))
    1305           0 :         return;
    1306             : 
    1307             :     /*
    1308             :      * Confine our attention to the relevant part of the string, if the query
    1309             :      * is a portion of a multi-statement source string, and update query
    1310             :      * location and length if needed.
    1311             :      */
    1312      180906 :     query = CleanQuerytext(query, &query_location, &query_len);
    1313             : 
    1314             :     /* Set up key for hashtable search */
    1315             : 
    1316             :     /* clear padding */
    1317      180906 :     memset(&key, 0, sizeof(pgssHashKey));
    1318             : 
    1319      180906 :     key.userid = GetUserId();
    1320      180906 :     key.dbid = MyDatabaseId;
    1321      180906 :     key.queryid = queryId;
    1322      180906 :     key.toplevel = (nesting_level == 0);
    1323             : 
    1324             :     /* Lookup the hash table entry with shared lock. */
    1325      180906 :     LWLockAcquire(pgss->lock, LW_SHARED);
    1326             : 
    1327      180906 :     entry = (pgssEntry *) hash_search(pgss_hash, &key, HASH_FIND, NULL);
    1328             : 
    1329             :     /* Create new entry, if not present */
    1330      180906 :     if (!entry)
    1331             :     {
    1332             :         Size        query_offset;
    1333             :         int         gc_count;
    1334             :         bool        stored;
    1335             :         bool        do_gc;
    1336             : 
    1337             :         /*
    1338             :          * Create a new, normalized query string if caller asked.  We don't
    1339             :          * need to hold the lock while doing this work.  (Note: in any case,
    1340             :          * it's possible that someone else creates a duplicate hashtable entry
    1341             :          * in the interval where we don't hold the lock below.  That case is
    1342             :          * handled by entry_alloc.)
    1343             :          */
    1344       54870 :         if (jstate)
    1345             :         {
    1346       20748 :             LWLockRelease(pgss->lock);
    1347       20748 :             norm_query = generate_normalized_query(jstate, query,
    1348             :                                                    query_location,
    1349             :                                                    &query_len);
    1350       20748 :             LWLockAcquire(pgss->lock, LW_SHARED);
    1351             :         }
    1352             : 
    1353             :         /* Append new query text to file with only shared lock held */
    1354       54870 :         stored = qtext_store(norm_query ? norm_query : query, query_len,
    1355             :                              &query_offset, &gc_count);
    1356             : 
    1357             :         /*
    1358             :          * Determine whether we need to garbage collect external query texts
    1359             :          * while the shared lock is still held.  This micro-optimization
    1360             :          * avoids taking the time to decide this while holding exclusive lock.
    1361             :          */
    1362       54870 :         do_gc = need_gc_qtexts();
    1363             : 
    1364             :         /* Need exclusive lock to make a new hashtable entry - promote */
    1365       54870 :         LWLockRelease(pgss->lock);
    1366       54870 :         LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
    1367             : 
    1368             :         /*
    1369             :          * A garbage collection may have occurred while we weren't holding the
    1370             :          * lock.  In the unlikely event that this happens, the query text we
    1371             :          * stored above will have been garbage collected, so write it again.
    1372             :          * This should be infrequent enough that doing it while holding
    1373             :          * exclusive lock isn't a performance problem.
    1374             :          */
    1375       54870 :         if (!stored || pgss->gc_count != gc_count)
    1376           0 :             stored = qtext_store(norm_query ? norm_query : query, query_len,
    1377             :                                  &query_offset, NULL);
    1378             : 
    1379             :         /* If we failed to write to the text file, give up */
    1380       54870 :         if (!stored)
    1381           0 :             goto done;
    1382             : 
    1383             :         /* OK to create a new hashtable entry */
    1384       54870 :         entry = entry_alloc(&key, query_offset, query_len, encoding,
    1385             :                             jstate != NULL);
    1386             : 
    1387             :         /* If needed, perform garbage collection while exclusive lock held */
    1388       54870 :         if (do_gc)
    1389           0 :             gc_qtexts();
    1390             :     }
    1391             : 
    1392             :     /* Increment the counts, except when jstate is not NULL */
    1393      180906 :     if (!jstate)
    1394             :     {
    1395             :         Assert(kind == PGSS_PLAN || kind == PGSS_EXEC);
    1396             : 
    1397             :         /*
    1398             :          * Grab the spinlock while updating the counters (see comment about
    1399             :          * locking rules at the head of the file)
    1400             :          */
    1401      113508 :         SpinLockAcquire(&entry->mutex);
    1402             : 
    1403             :         /* "Unstick" entry if it was previously sticky */
    1404      113508 :         if (IS_STICKY(entry->counters))
    1405       53364 :             entry->counters.usage = USAGE_INIT;
    1406             : 
    1407      113508 :         entry->counters.calls[kind] += 1;
    1408      113508 :         entry->counters.total_time[kind] += total_time;
    1409             : 
    1410      113508 :         if (entry->counters.calls[kind] == 1)
    1411             :         {
    1412       53472 :             entry->counters.min_time[kind] = total_time;
    1413       53472 :             entry->counters.max_time[kind] = total_time;
    1414       53472 :             entry->counters.mean_time[kind] = total_time;
    1415             :         }
    1416             :         else
    1417             :         {
    1418             :             /*
    1419             :              * Welford's method for accurately computing variance. See
    1420             :              * <http://www.johndcook.com/blog/standard_deviation/>
    1421             :              */
    1422       60036 :             double      old_mean = entry->counters.mean_time[kind];
    1423             : 
    1424       60036 :             entry->counters.mean_time[kind] +=
    1425       60036 :                 (total_time - old_mean) / entry->counters.calls[kind];
    1426       60036 :             entry->counters.sum_var_time[kind] +=
    1427       60036 :                 (total_time - old_mean) * (total_time - entry->counters.mean_time[kind]);
    1428             : 
    1429             :             /*
    1430             :              * Calculate min and max time. min = 0 and max = 0 means that the
    1431             :              * min/max statistics were reset
    1432             :              */
    1433       60036 :             if (entry->counters.min_time[kind] == 0
    1434           4 :                 && entry->counters.max_time[kind] == 0)
    1435             :             {
    1436           4 :                 entry->counters.min_time[kind] = total_time;
    1437           4 :                 entry->counters.max_time[kind] = total_time;
    1438             :             }
    1439             :             else
    1440             :             {
    1441       60032 :                 if (entry->counters.min_time[kind] > total_time)
    1442       12030 :                     entry->counters.min_time[kind] = total_time;
    1443       60032 :                 if (entry->counters.max_time[kind] < total_time)
    1444        5754 :                     entry->counters.max_time[kind] = total_time;
    1445             :             }
    1446             :         }
    1447      113508 :         entry->counters.rows += rows;
    1448      113508 :         entry->counters.shared_blks_hit += bufusage->shared_blks_hit;
    1449      113508 :         entry->counters.shared_blks_read += bufusage->shared_blks_read;
    1450      113508 :         entry->counters.shared_blks_dirtied += bufusage->shared_blks_dirtied;
    1451      113508 :         entry->counters.shared_blks_written += bufusage->shared_blks_written;
    1452      113508 :         entry->counters.local_blks_hit += bufusage->local_blks_hit;
    1453      113508 :         entry->counters.local_blks_read += bufusage->local_blks_read;
    1454      113508 :         entry->counters.local_blks_dirtied += bufusage->local_blks_dirtied;
    1455      113508 :         entry->counters.local_blks_written += bufusage->local_blks_written;
    1456      113508 :         entry->counters.temp_blks_read += bufusage->temp_blks_read;
    1457      113508 :         entry->counters.temp_blks_written += bufusage->temp_blks_written;
    1458      113508 :         entry->counters.shared_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_read_time);
    1459      113508 :         entry->counters.shared_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->shared_blk_write_time);
    1460      113508 :         entry->counters.local_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->local_blk_read_time);
    1461      113508 :         entry->counters.local_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->local_blk_write_time);
    1462      113508 :         entry->counters.temp_blk_read_time += INSTR_TIME_GET_MILLISEC(bufusage->temp_blk_read_time);
    1463      113508 :         entry->counters.temp_blk_write_time += INSTR_TIME_GET_MILLISEC(bufusage->temp_blk_write_time);
    1464      113508 :         entry->counters.usage += USAGE_EXEC(total_time);
    1465      113508 :         entry->counters.wal_records += walusage->wal_records;
    1466      113508 :         entry->counters.wal_fpi += walusage->wal_fpi;
    1467      113508 :         entry->counters.wal_bytes += walusage->wal_bytes;
    1468      113508 :         if (jitusage)
    1469             :         {
    1470         158 :             entry->counters.jit_functions += jitusage->created_functions;
    1471         158 :             entry->counters.jit_generation_time += INSTR_TIME_GET_MILLISEC(jitusage->generation_counter);
    1472             : 
    1473         158 :             if (INSTR_TIME_GET_MILLISEC(jitusage->deform_counter))
    1474         154 :                 entry->counters.jit_deform_count++;
    1475         158 :             entry->counters.jit_deform_time += INSTR_TIME_GET_MILLISEC(jitusage->deform_counter);
    1476             : 
    1477         158 :             if (INSTR_TIME_GET_MILLISEC(jitusage->inlining_counter))
    1478          74 :                 entry->counters.jit_inlining_count++;
    1479         158 :             entry->counters.jit_inlining_time += INSTR_TIME_GET_MILLISEC(jitusage->inlining_counter);
    1480             : 
    1481         158 :             if (INSTR_TIME_GET_MILLISEC(jitusage->optimization_counter))
    1482         154 :                 entry->counters.jit_optimization_count++;
    1483         158 :             entry->counters.jit_optimization_time += INSTR_TIME_GET_MILLISEC(jitusage->optimization_counter);
    1484             : 
    1485         158 :             if (INSTR_TIME_GET_MILLISEC(jitusage->emission_counter))
    1486         154 :                 entry->counters.jit_emission_count++;
    1487         158 :             entry->counters.jit_emission_time += INSTR_TIME_GET_MILLISEC(jitusage->emission_counter);
    1488             :         }
    1489             : 
    1490             :         /* parallel worker counters */
    1491      113508 :         entry->counters.parallel_workers_to_launch += parallel_workers_to_launch;
    1492      113508 :         entry->counters.parallel_workers_launched += parallel_workers_launched;
    1493             : 
    1494      113508 :         SpinLockRelease(&entry->mutex);
    1495             :     }
    1496             : 
    1497       67398 : done:
    1498      180906 :     LWLockRelease(pgss->lock);
    1499             : 
    1500             :     /* We postpone this clean-up until we're out of the lock */
    1501      180906 :     if (norm_query)
    1502       20748 :         pfree(norm_query);
    1503             : }
    1504             : 
    1505             : /*
    1506             :  * Reset statement statistics corresponding to userid, dbid, and queryid.
    1507             :  */
    1508             : Datum
    1509           2 : pg_stat_statements_reset_1_7(PG_FUNCTION_ARGS)
    1510             : {
    1511             :     Oid         userid;
    1512             :     Oid         dbid;
    1513             :     uint64      queryid;
    1514             : 
    1515           2 :     userid = PG_GETARG_OID(0);
    1516           2 :     dbid = PG_GETARG_OID(1);
    1517           2 :     queryid = (uint64) PG_GETARG_INT64(2);
    1518             : 
    1519           2 :     entry_reset(userid, dbid, queryid, false);
    1520             : 
    1521           2 :     PG_RETURN_VOID();
    1522             : }
    1523             : 
    1524             : Datum
    1525         156 : pg_stat_statements_reset_1_11(PG_FUNCTION_ARGS)
    1526             : {
    1527             :     Oid         userid;
    1528             :     Oid         dbid;
    1529             :     uint64      queryid;
    1530             :     bool        minmax_only;
    1531             : 
    1532         156 :     userid = PG_GETARG_OID(0);
    1533         156 :     dbid = PG_GETARG_OID(1);
    1534         156 :     queryid = (uint64) PG_GETARG_INT64(2);
    1535         156 :     minmax_only = PG_GETARG_BOOL(3);
    1536             : 
    1537         156 :     PG_RETURN_TIMESTAMPTZ(entry_reset(userid, dbid, queryid, minmax_only));
    1538             : }
    1539             : 
    1540             : /*
    1541             :  * Reset statement statistics.
    1542             :  */
    1543             : Datum
    1544           2 : pg_stat_statements_reset(PG_FUNCTION_ARGS)
    1545             : {
    1546           2 :     entry_reset(0, 0, 0, false);
    1547             : 
    1548           2 :     PG_RETURN_VOID();
    1549             : }
    1550             : 
    1551             : /* Number of output arguments (columns) for various API versions */
    1552             : #define PG_STAT_STATEMENTS_COLS_V1_0    14
    1553             : #define PG_STAT_STATEMENTS_COLS_V1_1    18
    1554             : #define PG_STAT_STATEMENTS_COLS_V1_2    19
    1555             : #define PG_STAT_STATEMENTS_COLS_V1_3    23
    1556             : #define PG_STAT_STATEMENTS_COLS_V1_8    32
    1557             : #define PG_STAT_STATEMENTS_COLS_V1_9    33
    1558             : #define PG_STAT_STATEMENTS_COLS_V1_10   43
    1559             : #define PG_STAT_STATEMENTS_COLS_V1_11   49
    1560             : #define PG_STAT_STATEMENTS_COLS_V1_12   51
    1561             : #define PG_STAT_STATEMENTS_COLS         51  /* maximum of above */
    1562             : 
    1563             : /*
    1564             :  * Retrieve statement statistics.
    1565             :  *
    1566             :  * The SQL API of this function has changed multiple times, and will likely
    1567             :  * do so again in future.  To support the case where a newer version of this
    1568             :  * loadable module is being used with an old SQL declaration of the function,
    1569             :  * we continue to support the older API versions.  For 1.2 and later, the
    1570             :  * expected API version is identified by embedding it in the C name of the
    1571             :  * function.  Unfortunately we weren't bright enough to do that for 1.1.
    1572             :  */
    1573             : Datum
    1574         176 : pg_stat_statements_1_12(PG_FUNCTION_ARGS)
    1575             : {
    1576         176 :     bool        showtext = PG_GETARG_BOOL(0);
    1577             : 
    1578         176 :     pg_stat_statements_internal(fcinfo, PGSS_V1_12, showtext);
    1579             : 
    1580         176 :     return (Datum) 0;
    1581             : }
    1582             : 
    1583             : Datum
    1584           2 : pg_stat_statements_1_11(PG_FUNCTION_ARGS)
    1585             : {
    1586           2 :     bool        showtext = PG_GETARG_BOOL(0);
    1587             : 
    1588           2 :     pg_stat_statements_internal(fcinfo, PGSS_V1_11, showtext);
    1589             : 
    1590           2 :     return (Datum) 0;
    1591             : }
    1592             : 
    1593             : Datum
    1594           2 : pg_stat_statements_1_10(PG_FUNCTION_ARGS)
    1595             : {
    1596           2 :     bool        showtext = PG_GETARG_BOOL(0);
    1597             : 
    1598           2 :     pg_stat_statements_internal(fcinfo, PGSS_V1_10, showtext);
    1599             : 
    1600           2 :     return (Datum) 0;
    1601             : }
    1602             : 
    1603             : Datum
    1604           2 : pg_stat_statements_1_9(PG_FUNCTION_ARGS)
    1605             : {
    1606           2 :     bool        showtext = PG_GETARG_BOOL(0);
    1607             : 
    1608           2 :     pg_stat_statements_internal(fcinfo, PGSS_V1_9, showtext);
    1609             : 
    1610           2 :     return (Datum) 0;
    1611             : }
    1612             : 
    1613             : Datum
    1614           2 : pg_stat_statements_1_8(PG_FUNCTION_ARGS)
    1615             : {
    1616           2 :     bool        showtext = PG_GETARG_BOOL(0);
    1617             : 
    1618           2 :     pg_stat_statements_internal(fcinfo, PGSS_V1_8, showtext);
    1619             : 
    1620           2 :     return (Datum) 0;
    1621             : }
    1622             : 
    1623             : Datum
    1624           2 : pg_stat_statements_1_3(PG_FUNCTION_ARGS)
    1625             : {
    1626           2 :     bool        showtext = PG_GETARG_BOOL(0);
    1627             : 
    1628           2 :     pg_stat_statements_internal(fcinfo, PGSS_V1_3, showtext);
    1629             : 
    1630           2 :     return (Datum) 0;
    1631             : }
    1632             : 
    1633             : Datum
    1634           0 : pg_stat_statements_1_2(PG_FUNCTION_ARGS)
    1635             : {
    1636           0 :     bool        showtext = PG_GETARG_BOOL(0);
    1637             : 
    1638           0 :     pg_stat_statements_internal(fcinfo, PGSS_V1_2, showtext);
    1639             : 
    1640           0 :     return (Datum) 0;
    1641             : }
    1642             : 
    1643             : /*
    1644             :  * Legacy entry point for pg_stat_statements() API versions 1.0 and 1.1.
    1645             :  * This can be removed someday, perhaps.
    1646             :  */
    1647             : Datum
    1648           0 : pg_stat_statements(PG_FUNCTION_ARGS)
    1649             : {
    1650             :     /* If it's really API 1.1, we'll figure that out below */
    1651           0 :     pg_stat_statements_internal(fcinfo, PGSS_V1_0, true);
    1652             : 
    1653           0 :     return (Datum) 0;
    1654             : }
    1655             : 
    1656             : /* Common code for all versions of pg_stat_statements() */
    1657             : static void
    1658         186 : pg_stat_statements_internal(FunctionCallInfo fcinfo,
    1659             :                             pgssVersion api_version,
    1660             :                             bool showtext)
    1661             : {
    1662         186 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    1663         186 :     Oid         userid = GetUserId();
    1664         186 :     bool        is_allowed_role = false;
    1665         186 :     char       *qbuffer = NULL;
    1666         186 :     Size        qbuffer_size = 0;
    1667         186 :     Size        extent = 0;
    1668         186 :     int         gc_count = 0;
    1669             :     HASH_SEQ_STATUS hash_seq;
    1670             :     pgssEntry  *entry;
    1671             : 
    1672             :     /*
    1673             :      * Superusers or roles with the privileges of pg_read_all_stats members
    1674             :      * are allowed
    1675             :      */
    1676         186 :     is_allowed_role = has_privs_of_role(userid, ROLE_PG_READ_ALL_STATS);
    1677             : 
    1678             :     /* hash table must exist already */
    1679         186 :     if (!pgss || !pgss_hash)
    1680           0 :         ereport(ERROR,
    1681             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1682             :                  errmsg("pg_stat_statements must be loaded via \"shared_preload_libraries\"")));
    1683             : 
    1684         186 :     InitMaterializedSRF(fcinfo, 0);
    1685             : 
    1686             :     /*
    1687             :      * Check we have the expected number of output arguments.  Aside from
    1688             :      * being a good safety check, we need a kluge here to detect API version
    1689             :      * 1.1, which was wedged into the code in an ill-considered way.
    1690             :      */
    1691         186 :     switch (rsinfo->setDesc->natts)
    1692             :     {
    1693           0 :         case PG_STAT_STATEMENTS_COLS_V1_0:
    1694           0 :             if (api_version != PGSS_V1_0)
    1695           0 :                 elog(ERROR, "incorrect number of output arguments");
    1696           0 :             break;
    1697           0 :         case PG_STAT_STATEMENTS_COLS_V1_1:
    1698             :             /* pg_stat_statements() should have told us 1.0 */
    1699           0 :             if (api_version != PGSS_V1_0)
    1700           0 :                 elog(ERROR, "incorrect number of output arguments");
    1701           0 :             api_version = PGSS_V1_1;
    1702           0 :             break;
    1703           0 :         case PG_STAT_STATEMENTS_COLS_V1_2:
    1704           0 :             if (api_version != PGSS_V1_2)
    1705           0 :                 elog(ERROR, "incorrect number of output arguments");
    1706           0 :             break;
    1707           2 :         case PG_STAT_STATEMENTS_COLS_V1_3:
    1708           2 :             if (api_version != PGSS_V1_3)
    1709           0 :                 elog(ERROR, "incorrect number of output arguments");
    1710           2 :             break;
    1711           2 :         case PG_STAT_STATEMENTS_COLS_V1_8:
    1712           2 :             if (api_version != PGSS_V1_8)
    1713           0 :                 elog(ERROR, "incorrect number of output arguments");
    1714           2 :             break;
    1715           2 :         case PG_STAT_STATEMENTS_COLS_V1_9:
    1716           2 :             if (api_version != PGSS_V1_9)
    1717           0 :                 elog(ERROR, "incorrect number of output arguments");
    1718           2 :             break;
    1719           2 :         case PG_STAT_STATEMENTS_COLS_V1_10:
    1720           2 :             if (api_version != PGSS_V1_10)
    1721           0 :                 elog(ERROR, "incorrect number of output arguments");
    1722           2 :             break;
    1723           2 :         case PG_STAT_STATEMENTS_COLS_V1_11:
    1724           2 :             if (api_version != PGSS_V1_11)
    1725           0 :                 elog(ERROR, "incorrect number of output arguments");
    1726           2 :             break;
    1727         176 :         case PG_STAT_STATEMENTS_COLS_V1_12:
    1728         176 :             if (api_version != PGSS_V1_12)
    1729           0 :                 elog(ERROR, "incorrect number of output arguments");
    1730         176 :             break;
    1731           0 :         default:
    1732           0 :             elog(ERROR, "incorrect number of output arguments");
    1733             :     }
    1734             : 
    1735             :     /*
    1736             :      * We'd like to load the query text file (if needed) while not holding any
    1737             :      * lock on pgss->lock.  In the worst case we'll have to do this again
    1738             :      * after we have the lock, but it's unlikely enough to make this a win
    1739             :      * despite occasional duplicated work.  We need to reload if anybody
    1740             :      * writes to the file (either a retail qtext_store(), or a garbage
    1741             :      * collection) between this point and where we've gotten shared lock.  If
    1742             :      * a qtext_store is actually in progress when we look, we might as well
    1743             :      * skip the speculative load entirely.
    1744             :      */
    1745         186 :     if (showtext)
    1746             :     {
    1747             :         int         n_writers;
    1748             : 
    1749             :         /* Take the mutex so we can examine variables */
    1750         186 :         SpinLockAcquire(&pgss->mutex);
    1751         186 :         extent = pgss->extent;
    1752         186 :         n_writers = pgss->n_writers;
    1753         186 :         gc_count = pgss->gc_count;
    1754         186 :         SpinLockRelease(&pgss->mutex);
    1755             : 
    1756             :         /* No point in loading file now if there are active writers */
    1757         186 :         if (n_writers == 0)
    1758         186 :             qbuffer = qtext_load_file(&qbuffer_size);
    1759             :     }
    1760             : 
    1761             :     /*
    1762             :      * Get shared lock, load or reload the query text file if we must, and
    1763             :      * iterate over the hashtable entries.
    1764             :      *
    1765             :      * With a large hash table, we might be holding the lock rather longer
    1766             :      * than one could wish.  However, this only blocks creation of new hash
    1767             :      * table entries, and the larger the hash table the less likely that is to
    1768             :      * be needed.  So we can hope this is okay.  Perhaps someday we'll decide
    1769             :      * we need to partition the hash table to limit the time spent holding any
    1770             :      * one lock.
    1771             :      */
    1772         186 :     LWLockAcquire(pgss->lock, LW_SHARED);
    1773             : 
    1774         186 :     if (showtext)
    1775             :     {
    1776             :         /*
    1777             :          * Here it is safe to examine extent and gc_count without taking the
    1778             :          * mutex.  Note that although other processes might change
    1779             :          * pgss->extent just after we look at it, the strings they then write
    1780             :          * into the file cannot yet be referenced in the hashtable, so we
    1781             :          * don't care whether we see them or not.
    1782             :          *
    1783             :          * If qtext_load_file fails, we just press on; we'll return NULL for
    1784             :          * every query text.
    1785             :          */
    1786         186 :         if (qbuffer == NULL ||
    1787         186 :             pgss->extent != extent ||
    1788         186 :             pgss->gc_count != gc_count)
    1789             :         {
    1790           0 :             free(qbuffer);
    1791           0 :             qbuffer = qtext_load_file(&qbuffer_size);
    1792             :         }
    1793             :     }
    1794             : 
    1795         186 :     hash_seq_init(&hash_seq, pgss_hash);
    1796       52006 :     while ((entry = hash_seq_search(&hash_seq)) != NULL)
    1797             :     {
    1798             :         Datum       values[PG_STAT_STATEMENTS_COLS];
    1799             :         bool        nulls[PG_STAT_STATEMENTS_COLS];
    1800       51820 :         int         i = 0;
    1801             :         Counters    tmp;
    1802             :         double      stddev;
    1803       51820 :         int64       queryid = entry->key.queryid;
    1804             :         TimestampTz stats_since;
    1805             :         TimestampTz minmax_stats_since;
    1806             : 
    1807       51820 :         memset(values, 0, sizeof(values));
    1808       51820 :         memset(nulls, 0, sizeof(nulls));
    1809             : 
    1810       51820 :         values[i++] = ObjectIdGetDatum(entry->key.userid);
    1811       51820 :         values[i++] = ObjectIdGetDatum(entry->key.dbid);
    1812       51820 :         if (api_version >= PGSS_V1_9)
    1813       51794 :             values[i++] = BoolGetDatum(entry->key.toplevel);
    1814             : 
    1815       51820 :         if (is_allowed_role || entry->key.userid == userid)
    1816             :         {
    1817       51812 :             if (api_version >= PGSS_V1_2)
    1818       51812 :                 values[i++] = Int64GetDatumFast(queryid);
    1819             : 
    1820       51812 :             if (showtext)
    1821             :             {
    1822       51812 :                 char       *qstr = qtext_fetch(entry->query_offset,
    1823             :                                                entry->query_len,
    1824             :                                                qbuffer,
    1825             :                                                qbuffer_size);
    1826             : 
    1827       51812 :                 if (qstr)
    1828             :                 {
    1829             :                     char       *enc;
    1830             : 
    1831       51812 :                     enc = pg_any_to_server(qstr,
    1832             :                                            entry->query_len,
    1833             :                                            entry->encoding);
    1834             : 
    1835       51812 :                     values[i++] = CStringGetTextDatum(enc);
    1836             : 
    1837       51812 :                     if (enc != qstr)
    1838           0 :                         pfree(enc);
    1839             :                 }
    1840             :                 else
    1841             :                 {
    1842             :                     /* Just return a null if we fail to find the text */
    1843           0 :                     nulls[i++] = true;
    1844             :                 }
    1845             :             }
    1846             :             else
    1847             :             {
    1848             :                 /* Query text not requested */
    1849           0 :                 nulls[i++] = true;
    1850             :             }
    1851             :         }
    1852             :         else
    1853             :         {
    1854             :             /* Don't show queryid */
    1855           8 :             if (api_version >= PGSS_V1_2)
    1856           8 :                 nulls[i++] = true;
    1857             : 
    1858             :             /*
    1859             :              * Don't show query text, but hint as to the reason for not doing
    1860             :              * so if it was requested
    1861             :              */
    1862           8 :             if (showtext)
    1863           8 :                 values[i++] = CStringGetTextDatum("<insufficient privilege>");
    1864             :             else
    1865           0 :                 nulls[i++] = true;
    1866             :         }
    1867             : 
    1868             :         /* copy counters to a local variable to keep locking time short */
    1869       51820 :         SpinLockAcquire(&entry->mutex);
    1870       51820 :         tmp = entry->counters;
    1871       51820 :         SpinLockRelease(&entry->mutex);
    1872             : 
    1873             :         /*
    1874             :          * The spinlock is not required when reading these two as they are
    1875             :          * always updated when holding pgss->lock exclusively.
    1876             :          */
    1877       51820 :         stats_since = entry->stats_since;
    1878       51820 :         minmax_stats_since = entry->minmax_stats_since;
    1879             : 
    1880             :         /* Skip entry if unexecuted (ie, it's a pending "sticky" entry) */
    1881       51820 :         if (IS_STICKY(tmp))
    1882          78 :             continue;
    1883             : 
    1884             :         /* Note that we rely on PGSS_PLAN being 0 and PGSS_EXEC being 1. */
    1885      155226 :         for (int kind = 0; kind < PGSS_NUMKIND; kind++)
    1886             :         {
    1887      103484 :             if (kind == PGSS_EXEC || api_version >= PGSS_V1_8)
    1888             :             {
    1889      103476 :                 values[i++] = Int64GetDatumFast(tmp.calls[kind]);
    1890      103476 :                 values[i++] = Float8GetDatumFast(tmp.total_time[kind]);
    1891             :             }
    1892             : 
    1893      103484 :             if ((kind == PGSS_EXEC && api_version >= PGSS_V1_3) ||
    1894             :                 api_version >= PGSS_V1_8)
    1895             :             {
    1896      103476 :                 values[i++] = Float8GetDatumFast(tmp.min_time[kind]);
    1897      103476 :                 values[i++] = Float8GetDatumFast(tmp.max_time[kind]);
    1898      103476 :                 values[i++] = Float8GetDatumFast(tmp.mean_time[kind]);
    1899             : 
    1900             :                 /*
    1901             :                  * Note we are calculating the population variance here, not
    1902             :                  * the sample variance, as we have data for the whole
    1903             :                  * population, so Bessel's correction is not used, and we
    1904             :                  * don't divide by tmp.calls - 1.
    1905             :                  */
    1906      103476 :                 if (tmp.calls[kind] > 1)
    1907        9156 :                     stddev = sqrt(tmp.sum_var_time[kind] / tmp.calls[kind]);
    1908             :                 else
    1909       94320 :                     stddev = 0.0;
    1910      103476 :                 values[i++] = Float8GetDatumFast(stddev);
    1911             :             }
    1912             :         }
    1913       51742 :         values[i++] = Int64GetDatumFast(tmp.rows);
    1914       51742 :         values[i++] = Int64GetDatumFast(tmp.shared_blks_hit);
    1915       51742 :         values[i++] = Int64GetDatumFast(tmp.shared_blks_read);
    1916       51742 :         if (api_version >= PGSS_V1_1)
    1917       51742 :             values[i++] = Int64GetDatumFast(tmp.shared_blks_dirtied);
    1918       51742 :         values[i++] = Int64GetDatumFast(tmp.shared_blks_written);
    1919       51742 :         values[i++] = Int64GetDatumFast(tmp.local_blks_hit);
    1920       51742 :         values[i++] = Int64GetDatumFast(tmp.local_blks_read);
    1921       51742 :         if (api_version >= PGSS_V1_1)
    1922       51742 :             values[i++] = Int64GetDatumFast(tmp.local_blks_dirtied);
    1923       51742 :         values[i++] = Int64GetDatumFast(tmp.local_blks_written);
    1924       51742 :         values[i++] = Int64GetDatumFast(tmp.temp_blks_read);
    1925       51742 :         values[i++] = Int64GetDatumFast(tmp.temp_blks_written);
    1926       51742 :         if (api_version >= PGSS_V1_1)
    1927             :         {
    1928       51742 :             values[i++] = Float8GetDatumFast(tmp.shared_blk_read_time);
    1929       51742 :             values[i++] = Float8GetDatumFast(tmp.shared_blk_write_time);
    1930             :         }
    1931       51742 :         if (api_version >= PGSS_V1_11)
    1932             :         {
    1933       51680 :             values[i++] = Float8GetDatumFast(tmp.local_blk_read_time);
    1934       51680 :             values[i++] = Float8GetDatumFast(tmp.local_blk_write_time);
    1935             :         }
    1936       51742 :         if (api_version >= PGSS_V1_10)
    1937             :         {
    1938       51702 :             values[i++] = Float8GetDatumFast(tmp.temp_blk_read_time);
    1939       51702 :             values[i++] = Float8GetDatumFast(tmp.temp_blk_write_time);
    1940             :         }
    1941       51742 :         if (api_version >= PGSS_V1_8)
    1942             :         {
    1943             :             char        buf[256];
    1944             :             Datum       wal_bytes;
    1945             : 
    1946       51734 :             values[i++] = Int64GetDatumFast(tmp.wal_records);
    1947       51734 :             values[i++] = Int64GetDatumFast(tmp.wal_fpi);
    1948             : 
    1949       51734 :             snprintf(buf, sizeof buf, UINT64_FORMAT, tmp.wal_bytes);
    1950             : 
    1951             :             /* Convert to numeric. */
    1952       51734 :             wal_bytes = DirectFunctionCall3(numeric_in,
    1953             :                                             CStringGetDatum(buf),
    1954             :                                             ObjectIdGetDatum(0),
    1955             :                                             Int32GetDatum(-1));
    1956       51734 :             values[i++] = wal_bytes;
    1957             :         }
    1958       51742 :         if (api_version >= PGSS_V1_10)
    1959             :         {
    1960       51702 :             values[i++] = Int64GetDatumFast(tmp.jit_functions);
    1961       51702 :             values[i++] = Float8GetDatumFast(tmp.jit_generation_time);
    1962       51702 :             values[i++] = Int64GetDatumFast(tmp.jit_inlining_count);
    1963       51702 :             values[i++] = Float8GetDatumFast(tmp.jit_inlining_time);
    1964       51702 :             values[i++] = Int64GetDatumFast(tmp.jit_optimization_count);
    1965       51702 :             values[i++] = Float8GetDatumFast(tmp.jit_optimization_time);
    1966       51702 :             values[i++] = Int64GetDatumFast(tmp.jit_emission_count);
    1967       51702 :             values[i++] = Float8GetDatumFast(tmp.jit_emission_time);
    1968             :         }
    1969       51742 :         if (api_version >= PGSS_V1_11)
    1970             :         {
    1971       51680 :             values[i++] = Int64GetDatumFast(tmp.jit_deform_count);
    1972       51680 :             values[i++] = Float8GetDatumFast(tmp.jit_deform_time);
    1973             :         }
    1974       51742 :         if (api_version >= PGSS_V1_12)
    1975             :         {
    1976       51654 :             values[i++] = Int64GetDatumFast(tmp.parallel_workers_to_launch);
    1977       51654 :             values[i++] = Int64GetDatumFast(tmp.parallel_workers_launched);
    1978             :         }
    1979       51742 :         if (api_version >= PGSS_V1_11)
    1980             :         {
    1981       51680 :             values[i++] = TimestampTzGetDatum(stats_since);
    1982       51680 :             values[i++] = TimestampTzGetDatum(minmax_stats_since);
    1983             :         }
    1984             : 
    1985             :         Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 :
    1986             :                      api_version == PGSS_V1_1 ? PG_STAT_STATEMENTS_COLS_V1_1 :
    1987             :                      api_version == PGSS_V1_2 ? PG_STAT_STATEMENTS_COLS_V1_2 :
    1988             :                      api_version == PGSS_V1_3 ? PG_STAT_STATEMENTS_COLS_V1_3 :
    1989             :                      api_version == PGSS_V1_8 ? PG_STAT_STATEMENTS_COLS_V1_8 :
    1990             :                      api_version == PGSS_V1_9 ? PG_STAT_STATEMENTS_COLS_V1_9 :
    1991             :                      api_version == PGSS_V1_10 ? PG_STAT_STATEMENTS_COLS_V1_10 :
    1992             :                      api_version == PGSS_V1_11 ? PG_STAT_STATEMENTS_COLS_V1_11 :
    1993             :                      api_version == PGSS_V1_12 ? PG_STAT_STATEMENTS_COLS_V1_12 :
    1994             :                      -1 /* fail if you forget to update this assert */ ));
    1995             : 
    1996       51742 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
    1997             :     }
    1998             : 
    1999         186 :     LWLockRelease(pgss->lock);
    2000             : 
    2001         186 :     free(qbuffer);
    2002         186 : }
    2003             : 
    2004             : /* Number of output arguments (columns) for pg_stat_statements_info */
    2005             : #define PG_STAT_STATEMENTS_INFO_COLS    2
    2006             : 
    2007             : /*
    2008             :  * Return statistics of pg_stat_statements.
    2009             :  */
    2010             : Datum
    2011           4 : pg_stat_statements_info(PG_FUNCTION_ARGS)
    2012             : {
    2013             :     pgssGlobalStats stats;
    2014             :     TupleDesc   tupdesc;
    2015           4 :     Datum       values[PG_STAT_STATEMENTS_INFO_COLS] = {0};
    2016           4 :     bool        nulls[PG_STAT_STATEMENTS_INFO_COLS] = {0};
    2017             : 
    2018           4 :     if (!pgss || !pgss_hash)
    2019           0 :         ereport(ERROR,
    2020             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    2021             :                  errmsg("pg_stat_statements must be loaded via \"shared_preload_libraries\"")));
    2022             : 
    2023             :     /* Build a tuple descriptor for our result type */
    2024           4 :     if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
    2025           0 :         elog(ERROR, "return type must be a row type");
    2026             : 
    2027             :     /* Read global statistics for pg_stat_statements */
    2028           4 :     SpinLockAcquire(&pgss->mutex);
    2029           4 :     stats = pgss->stats;
    2030           4 :     SpinLockRelease(&pgss->mutex);
    2031             : 
    2032           4 :     values[0] = Int64GetDatum(stats.dealloc);
    2033           4 :     values[1] = TimestampTzGetDatum(stats.stats_reset);
    2034             : 
    2035           4 :     PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls)));
    2036             : }
    2037             : 
    2038             : /*
    2039             :  * Estimate shared memory space needed.
    2040             :  */
    2041             : static Size
    2042          14 : pgss_memsize(void)
    2043             : {
    2044             :     Size        size;
    2045             : 
    2046          14 :     size = MAXALIGN(sizeof(pgssSharedState));
    2047          14 :     size = add_size(size, hash_estimate_size(pgss_max, sizeof(pgssEntry)));
    2048             : 
    2049          14 :     return size;
    2050             : }
    2051             : 
    2052             : /*
    2053             :  * Allocate a new hashtable entry.
    2054             :  * caller must hold an exclusive lock on pgss->lock
    2055             :  *
    2056             :  * "query" need not be null-terminated; we rely on query_len instead
    2057             :  *
    2058             :  * If "sticky" is true, make the new entry artificially sticky so that it will
    2059             :  * probably still be there when the query finishes execution.  We do this by
    2060             :  * giving it a median usage value rather than the normal value.  (Strictly
    2061             :  * speaking, query strings are normalized on a best effort basis, though it
    2062             :  * would be difficult to demonstrate this even under artificial conditions.)
    2063             :  *
    2064             :  * Note: despite needing exclusive lock, it's not an error for the target
    2065             :  * entry to already exist.  This is because pgss_store releases and
    2066             :  * reacquires lock after failing to find a match; so someone else could
    2067             :  * have made the entry while we waited to get exclusive lock.
    2068             :  */
    2069             : static pgssEntry *
    2070      105072 : entry_alloc(pgssHashKey *key, Size query_offset, int query_len, int encoding,
    2071             :             bool sticky)
    2072             : {
    2073             :     pgssEntry  *entry;
    2074             :     bool        found;
    2075             : 
    2076             :     /* Make space if needed */
    2077      105072 :     while (hash_get_num_entries(pgss_hash) >= pgss_max)
    2078           0 :         entry_dealloc();
    2079             : 
    2080             :     /* Find or create an entry with desired hash code */
    2081      105072 :     entry = (pgssEntry *) hash_search(pgss_hash, key, HASH_ENTER, &found);
    2082             : 
    2083      105072 :     if (!found)
    2084             :     {
    2085             :         /* New entry, initialize it */
    2086             : 
    2087             :         /* reset the statistics */
    2088      105072 :         memset(&entry->counters, 0, sizeof(Counters));
    2089             :         /* set the appropriate initial usage count */
    2090      105072 :         entry->counters.usage = sticky ? pgss->cur_median_usage : USAGE_INIT;
    2091             :         /* re-initialize the mutex each time ... we assume no one using it */
    2092      105072 :         SpinLockInit(&entry->mutex);
    2093             :         /* ... and don't forget the query text metadata */
    2094             :         Assert(query_len >= 0);
    2095      105072 :         entry->query_offset = query_offset;
    2096      105072 :         entry->query_len = query_len;
    2097      105072 :         entry->encoding = encoding;
    2098      105072 :         entry->stats_since = GetCurrentTimestamp();
    2099      105072 :         entry->minmax_stats_since = entry->stats_since;
    2100             :     }
    2101             : 
    2102      105072 :     return entry;
    2103             : }
    2104             : 
    2105             : /*
    2106             :  * qsort comparator for sorting into increasing usage order
    2107             :  */
    2108             : static int
    2109           0 : entry_cmp(const void *lhs, const void *rhs)
    2110             : {
    2111           0 :     double      l_usage = (*(pgssEntry *const *) lhs)->counters.usage;
    2112           0 :     double      r_usage = (*(pgssEntry *const *) rhs)->counters.usage;
    2113             : 
    2114           0 :     if (l_usage < r_usage)
    2115           0 :         return -1;
    2116           0 :     else if (l_usage > r_usage)
    2117           0 :         return +1;
    2118             :     else
    2119           0 :         return 0;
    2120             : }
    2121             : 
    2122             : /*
    2123             :  * Deallocate least-used entries.
    2124             :  *
    2125             :  * Caller must hold an exclusive lock on pgss->lock.
    2126             :  */
    2127             : static void
    2128           0 : entry_dealloc(void)
    2129             : {
    2130             :     HASH_SEQ_STATUS hash_seq;
    2131             :     pgssEntry **entries;
    2132             :     pgssEntry  *entry;
    2133             :     int         nvictims;
    2134             :     int         i;
    2135             :     Size        tottextlen;
    2136             :     int         nvalidtexts;
    2137             : 
    2138             :     /*
    2139             :      * Sort entries by usage and deallocate USAGE_DEALLOC_PERCENT of them.
    2140             :      * While we're scanning the table, apply the decay factor to the usage
    2141             :      * values, and update the mean query length.
    2142             :      *
    2143             :      * Note that the mean query length is almost immediately obsolete, since
    2144             :      * we compute it before not after discarding the least-used entries.
    2145             :      * Hopefully, that doesn't affect the mean too much; it doesn't seem worth
    2146             :      * making two passes to get a more current result.  Likewise, the new
    2147             :      * cur_median_usage includes the entries we're about to zap.
    2148             :      */
    2149             : 
    2150           0 :     entries = palloc(hash_get_num_entries(pgss_hash) * sizeof(pgssEntry *));
    2151             : 
    2152           0 :     i = 0;
    2153           0 :     tottextlen = 0;
    2154           0 :     nvalidtexts = 0;
    2155             : 
    2156           0 :     hash_seq_init(&hash_seq, pgss_hash);
    2157           0 :     while ((entry = hash_seq_search(&hash_seq)) != NULL)
    2158             :     {
    2159           0 :         entries[i++] = entry;
    2160             :         /* "Sticky" entries get a different usage decay rate. */
    2161           0 :         if (IS_STICKY(entry->counters))
    2162           0 :             entry->counters.usage *= STICKY_DECREASE_FACTOR;
    2163             :         else
    2164           0 :             entry->counters.usage *= USAGE_DECREASE_FACTOR;
    2165             :         /* In the mean length computation, ignore dropped texts. */
    2166           0 :         if (entry->query_len >= 0)
    2167             :         {
    2168           0 :             tottextlen += entry->query_len + 1;
    2169           0 :             nvalidtexts++;
    2170             :         }
    2171             :     }
    2172             : 
    2173             :     /* Sort into increasing order by usage */
    2174           0 :     qsort(entries, i, sizeof(pgssEntry *), entry_cmp);
    2175             : 
    2176             :     /* Record the (approximate) median usage */
    2177           0 :     if (i > 0)
    2178           0 :         pgss->cur_median_usage = entries[i / 2]->counters.usage;
    2179             :     /* Record the mean query length */
    2180           0 :     if (nvalidtexts > 0)
    2181           0 :         pgss->mean_query_len = tottextlen / nvalidtexts;
    2182             :     else
    2183           0 :         pgss->mean_query_len = ASSUMED_LENGTH_INIT;
    2184             : 
    2185             :     /* Now zap an appropriate fraction of lowest-usage entries */
    2186           0 :     nvictims = Max(10, i * USAGE_DEALLOC_PERCENT / 100);
    2187           0 :     nvictims = Min(nvictims, i);
    2188             : 
    2189           0 :     for (i = 0; i < nvictims; i++)
    2190             :     {
    2191           0 :         hash_search(pgss_hash, &entries[i]->key, HASH_REMOVE, NULL);
    2192             :     }
    2193             : 
    2194           0 :     pfree(entries);
    2195             : 
    2196             :     /* Increment the number of times entries are deallocated */
    2197           0 :     SpinLockAcquire(&pgss->mutex);
    2198           0 :     pgss->stats.dealloc += 1;
    2199           0 :     SpinLockRelease(&pgss->mutex);
    2200           0 : }
    2201             : 
    2202             : /*
    2203             :  * Given a query string (not necessarily null-terminated), allocate a new
    2204             :  * entry in the external query text file and store the string there.
    2205             :  *
    2206             :  * If successful, returns true, and stores the new entry's offset in the file
    2207             :  * into *query_offset.  Also, if gc_count isn't NULL, *gc_count is set to the
    2208             :  * number of garbage collections that have occurred so far.
    2209             :  *
    2210             :  * On failure, returns false.
    2211             :  *
    2212             :  * At least a shared lock on pgss->lock must be held by the caller, so as
    2213             :  * to prevent a concurrent garbage collection.  Share-lock-holding callers
    2214             :  * should pass a gc_count pointer to obtain the number of garbage collections,
    2215             :  * so that they can recheck the count after obtaining exclusive lock to
    2216             :  * detect whether a garbage collection occurred (and removed this entry).
    2217             :  */
    2218             : static bool
    2219       54870 : qtext_store(const char *query, int query_len,
    2220             :             Size *query_offset, int *gc_count)
    2221             : {
    2222             :     Size        off;
    2223             :     int         fd;
    2224             : 
    2225             :     /*
    2226             :      * We use a spinlock to protect extent/n_writers/gc_count, so that
    2227             :      * multiple processes may execute this function concurrently.
    2228             :      */
    2229       54870 :     SpinLockAcquire(&pgss->mutex);
    2230       54870 :     off = pgss->extent;
    2231       54870 :     pgss->extent += query_len + 1;
    2232       54870 :     pgss->n_writers++;
    2233       54870 :     if (gc_count)
    2234       54870 :         *gc_count = pgss->gc_count;
    2235       54870 :     SpinLockRelease(&pgss->mutex);
    2236             : 
    2237       54870 :     *query_offset = off;
    2238             : 
    2239             :     /*
    2240             :      * Don't allow the file to grow larger than what qtext_load_file can
    2241             :      * (theoretically) handle.  This has been seen to be reachable on 32-bit
    2242             :      * platforms.
    2243             :      */
    2244       54870 :     if (unlikely(query_len >= MaxAllocHugeSize - off))
    2245             :     {
    2246           0 :         errno = EFBIG;          /* not quite right, but it'll do */
    2247           0 :         fd = -1;
    2248           0 :         goto error;
    2249             :     }
    2250             : 
    2251             :     /* Now write the data into the successfully-reserved part of the file */
    2252       54870 :     fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDWR | O_CREAT | PG_BINARY);
    2253       54870 :     if (fd < 0)
    2254           0 :         goto error;
    2255             : 
    2256       54870 :     if (pg_pwrite(fd, query, query_len, off) != query_len)
    2257           0 :         goto error;
    2258       54870 :     if (pg_pwrite(fd, "\0", 1, off + query_len) != 1)
    2259           0 :         goto error;
    2260             : 
    2261       54870 :     CloseTransientFile(fd);
    2262             : 
    2263             :     /* Mark our write complete */
    2264       54870 :     SpinLockAcquire(&pgss->mutex);
    2265       54870 :     pgss->n_writers--;
    2266       54870 :     SpinLockRelease(&pgss->mutex);
    2267             : 
    2268       54870 :     return true;
    2269             : 
    2270           0 : error:
    2271           0 :     ereport(LOG,
    2272             :             (errcode_for_file_access(),
    2273             :              errmsg("could not write file \"%s\": %m",
    2274             :                     PGSS_TEXT_FILE)));
    2275             : 
    2276           0 :     if (fd >= 0)
    2277           0 :         CloseTransientFile(fd);
    2278             : 
    2279             :     /* Mark our write complete */
    2280           0 :     SpinLockAcquire(&pgss->mutex);
    2281           0 :     pgss->n_writers--;
    2282           0 :     SpinLockRelease(&pgss->mutex);
    2283             : 
    2284           0 :     return false;
    2285             : }
    2286             : 
    2287             : /*
    2288             :  * Read the external query text file into a malloc'd buffer.
    2289             :  *
    2290             :  * Returns NULL (without throwing an error) if unable to read, eg
    2291             :  * file not there or insufficient memory.
    2292             :  *
    2293             :  * On success, the buffer size is also returned into *buffer_size.
    2294             :  *
    2295             :  * This can be called without any lock on pgss->lock, but in that case
    2296             :  * the caller is responsible for verifying that the result is sane.
    2297             :  */
    2298             : static char *
    2299         196 : qtext_load_file(Size *buffer_size)
    2300             : {
    2301             :     char       *buf;
    2302             :     int         fd;
    2303             :     struct stat stat;
    2304             :     Size        nread;
    2305             : 
    2306         196 :     fd = OpenTransientFile(PGSS_TEXT_FILE, O_RDONLY | PG_BINARY);
    2307         196 :     if (fd < 0)
    2308             :     {
    2309           0 :         if (errno != ENOENT)
    2310           0 :             ereport(LOG,
    2311             :                     (errcode_for_file_access(),
    2312             :                      errmsg("could not read file \"%s\": %m",
    2313             :                             PGSS_TEXT_FILE)));
    2314           0 :         return NULL;
    2315             :     }
    2316             : 
    2317             :     /* Get file length */
    2318         196 :     if (fstat(fd, &stat))
    2319             :     {
    2320           0 :         ereport(LOG,
    2321             :                 (errcode_for_file_access(),
    2322             :                  errmsg("could not stat file \"%s\": %m",
    2323             :                         PGSS_TEXT_FILE)));
    2324           0 :         CloseTransientFile(fd);
    2325           0 :         return NULL;
    2326             :     }
    2327             : 
    2328             :     /* Allocate buffer; beware that off_t might be wider than size_t */
    2329         196 :     if (stat.st_size <= MaxAllocHugeSize)
    2330         196 :         buf = (char *) malloc(stat.st_size);
    2331             :     else
    2332           0 :         buf = NULL;
    2333         196 :     if (buf == NULL)
    2334             :     {
    2335           0 :         ereport(LOG,
    2336             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    2337             :                  errmsg("out of memory"),
    2338             :                  errdetail("Could not allocate enough memory to read file \"%s\".",
    2339             :                            PGSS_TEXT_FILE)));
    2340           0 :         CloseTransientFile(fd);
    2341           0 :         return NULL;
    2342             :     }
    2343             : 
    2344             :     /*
    2345             :      * OK, slurp in the file.  Windows fails if we try to read more than
    2346             :      * INT_MAX bytes at once, and other platforms might not like that either,
    2347             :      * so read a very large file in 1GB segments.
    2348             :      */
    2349         196 :     nread = 0;
    2350         390 :     while (nread < stat.st_size)
    2351             :     {
    2352         194 :         int         toread = Min(1024 * 1024 * 1024, stat.st_size - nread);
    2353             : 
    2354             :         /*
    2355             :          * If we get a short read and errno doesn't get set, the reason is
    2356             :          * probably that garbage collection truncated the file since we did
    2357             :          * the fstat(), so we don't log a complaint --- but we don't return
    2358             :          * the data, either, since it's most likely corrupt due to concurrent
    2359             :          * writes from garbage collection.
    2360             :          */
    2361         194 :         errno = 0;
    2362         194 :         if (read(fd, buf + nread, toread) != toread)
    2363             :         {
    2364           0 :             if (errno)
    2365           0 :                 ereport(LOG,
    2366             :                         (errcode_for_file_access(),
    2367             :                          errmsg("could not read file \"%s\": %m",
    2368             :                                 PGSS_TEXT_FILE)));
    2369           0 :             free(buf);
    2370           0 :             CloseTransientFile(fd);
    2371           0 :             return NULL;
    2372             :         }
    2373         194 :         nread += toread;
    2374             :     }
    2375             : 
    2376         196 :     if (CloseTransientFile(fd) != 0)
    2377           0 :         ereport(LOG,
    2378             :                 (errcode_for_file_access(),
    2379             :                  errmsg("could not close file \"%s\": %m", PGSS_TEXT_FILE)));
    2380             : 
    2381         196 :     *buffer_size = nread;
    2382         196 :     return buf;
    2383             : }
    2384             : 
    2385             : /*
    2386             :  * Locate a query text in the file image previously read by qtext_load_file().
    2387             :  *
    2388             :  * We validate the given offset/length, and return NULL if bogus.  Otherwise,
    2389             :  * the result points to a null-terminated string within the buffer.
    2390             :  */
    2391             : static char *
    2392      155546 : qtext_fetch(Size query_offset, int query_len,
    2393             :             char *buffer, Size buffer_size)
    2394             : {
    2395             :     /* File read failed? */
    2396      155546 :     if (buffer == NULL)
    2397           0 :         return NULL;
    2398             :     /* Bogus offset/length? */
    2399      155546 :     if (query_len < 0 ||
    2400      155546 :         query_offset + query_len >= buffer_size)
    2401           0 :         return NULL;
    2402             :     /* As a further sanity check, make sure there's a trailing null */
    2403      155546 :     if (buffer[query_offset + query_len] != '\0')
    2404           0 :         return NULL;
    2405             :     /* Looks OK */
    2406      155546 :     return buffer + query_offset;
    2407             : }
    2408             : 
    2409             : /*
    2410             :  * Do we need to garbage-collect the external query text file?
    2411             :  *
    2412             :  * Caller should hold at least a shared lock on pgss->lock.
    2413             :  */
    2414             : static bool
    2415       54870 : need_gc_qtexts(void)
    2416             : {
    2417             :     Size        extent;
    2418             : 
    2419             :     /* Read shared extent pointer */
    2420       54870 :     SpinLockAcquire(&pgss->mutex);
    2421       54870 :     extent = pgss->extent;
    2422       54870 :     SpinLockRelease(&pgss->mutex);
    2423             : 
    2424             :     /*
    2425             :      * Don't proceed if file does not exceed 512 bytes per possible entry.
    2426             :      *
    2427             :      * Here and in the next test, 32-bit machines have overflow hazards if
    2428             :      * pgss_max and/or mean_query_len are large.  Force the multiplications
    2429             :      * and comparisons to be done in uint64 arithmetic to forestall trouble.
    2430             :      */
    2431       54870 :     if ((uint64) extent < (uint64) 512 * pgss_max)
    2432       54870 :         return false;
    2433             : 
    2434             :     /*
    2435             :      * Don't proceed if file is less than about 50% bloat.  Nothing can or
    2436             :      * should be done in the event of unusually large query texts accounting
    2437             :      * for file's large size.  We go to the trouble of maintaining the mean
    2438             :      * query length in order to prevent garbage collection from thrashing
    2439             :      * uselessly.
    2440             :      */
    2441           0 :     if ((uint64) extent < (uint64) pgss->mean_query_len * pgss_max * 2)
    2442           0 :         return false;
    2443             : 
    2444           0 :     return true;
    2445             : }
    2446             : 
    2447             : /*
    2448             :  * Garbage-collect orphaned query texts in external file.
    2449             :  *
    2450             :  * This won't be called often in the typical case, since it's likely that
    2451             :  * there won't be too much churn, and besides, a similar compaction process
    2452             :  * occurs when serializing to disk at shutdown or as part of resetting.
    2453             :  * Despite this, it seems prudent to plan for the edge case where the file
    2454             :  * becomes unreasonably large, with no other method of compaction likely to
    2455             :  * occur in the foreseeable future.
    2456             :  *
    2457             :  * The caller must hold an exclusive lock on pgss->lock.
    2458             :  *
    2459             :  * At the first sign of trouble we unlink the query text file to get a clean
    2460             :  * slate (although existing statistics are retained), rather than risk
    2461             :  * thrashing by allowing the same problem case to recur indefinitely.
    2462             :  */
    2463             : static void
    2464           0 : gc_qtexts(void)
    2465             : {
    2466             :     char       *qbuffer;
    2467             :     Size        qbuffer_size;
    2468           0 :     FILE       *qfile = NULL;
    2469             :     HASH_SEQ_STATUS hash_seq;
    2470             :     pgssEntry  *entry;
    2471             :     Size        extent;
    2472             :     int         nentries;
    2473             : 
    2474             :     /*
    2475             :      * When called from pgss_store, some other session might have proceeded
    2476             :      * with garbage collection in the no-lock-held interim of lock strength
    2477             :      * escalation.  Check once more that this is actually necessary.
    2478             :      */
    2479           0 :     if (!need_gc_qtexts())
    2480           0 :         return;
    2481             : 
    2482             :     /*
    2483             :      * Load the old texts file.  If we fail (out of memory, for instance),
    2484             :      * invalidate query texts.  Hopefully this is rare.  It might seem better
    2485             :      * to leave things alone on an OOM failure, but the problem is that the
    2486             :      * file is only going to get bigger; hoping for a future non-OOM result is
    2487             :      * risky and can easily lead to complete denial of service.
    2488             :      */
    2489           0 :     qbuffer = qtext_load_file(&qbuffer_size);
    2490           0 :     if (qbuffer == NULL)
    2491           0 :         goto gc_fail;
    2492             : 
    2493             :     /*
    2494             :      * We overwrite the query texts file in place, so as to reduce the risk of
    2495             :      * an out-of-disk-space failure.  Since the file is guaranteed not to get
    2496             :      * larger, this should always work on traditional filesystems; though we
    2497             :      * could still lose on copy-on-write filesystems.
    2498             :      */
    2499           0 :     qfile = AllocateFile(PGSS_TEXT_FILE, PG_BINARY_W);
    2500           0 :     if (qfile == NULL)
    2501             :     {
    2502           0 :         ereport(LOG,
    2503             :                 (errcode_for_file_access(),
    2504             :                  errmsg("could not write file \"%s\": %m",
    2505             :                         PGSS_TEXT_FILE)));
    2506           0 :         goto gc_fail;
    2507             :     }
    2508             : 
    2509           0 :     extent = 0;
    2510           0 :     nentries = 0;
    2511             : 
    2512           0 :     hash_seq_init(&hash_seq, pgss_hash);
    2513           0 :     while ((entry = hash_seq_search(&hash_seq)) != NULL)
    2514             :     {
    2515           0 :         int         query_len = entry->query_len;
    2516           0 :         char       *qry = qtext_fetch(entry->query_offset,
    2517             :                                       query_len,
    2518             :                                       qbuffer,
    2519             :                                       qbuffer_size);
    2520             : 
    2521           0 :         if (qry == NULL)
    2522             :         {
    2523             :             /* Trouble ... drop the text */
    2524           0 :             entry->query_offset = 0;
    2525           0 :             entry->query_len = -1;
    2526             :             /* entry will not be counted in mean query length computation */
    2527           0 :             continue;
    2528             :         }
    2529             : 
    2530           0 :         if (fwrite(qry, 1, query_len + 1, qfile) != query_len + 1)
    2531             :         {
    2532           0 :             ereport(LOG,
    2533             :                     (errcode_for_file_access(),
    2534             :                      errmsg("could not write file \"%s\": %m",
    2535             :                             PGSS_TEXT_FILE)));
    2536           0 :             hash_seq_term(&hash_seq);
    2537           0 :             goto gc_fail;
    2538             :         }
    2539             : 
    2540           0 :         entry->query_offset = extent;
    2541           0 :         extent += query_len + 1;
    2542           0 :         nentries++;
    2543             :     }
    2544             : 
    2545             :     /*
    2546             :      * Truncate away any now-unused space.  If this fails for some odd reason,
    2547             :      * we log it, but there's no need to fail.
    2548             :      */
    2549           0 :     if (ftruncate(fileno(qfile), extent) != 0)
    2550           0 :         ereport(LOG,
    2551             :                 (errcode_for_file_access(),
    2552             :                  errmsg("could not truncate file \"%s\": %m",
    2553             :                         PGSS_TEXT_FILE)));
    2554             : 
    2555           0 :     if (FreeFile(qfile))
    2556             :     {
    2557           0 :         ereport(LOG,
    2558             :                 (errcode_for_file_access(),
    2559             :                  errmsg("could not write file \"%s\": %m",
    2560             :                         PGSS_TEXT_FILE)));
    2561           0 :         qfile = NULL;
    2562           0 :         goto gc_fail;
    2563             :     }
    2564             : 
    2565           0 :     elog(DEBUG1, "pgss gc of queries file shrunk size from %zu to %zu",
    2566             :          pgss->extent, extent);
    2567             : 
    2568             :     /* Reset the shared extent pointer */
    2569           0 :     pgss->extent = extent;
    2570             : 
    2571             :     /*
    2572             :      * Also update the mean query length, to be sure that need_gc_qtexts()
    2573             :      * won't still think we have a problem.
    2574             :      */
    2575           0 :     if (nentries > 0)
    2576           0 :         pgss->mean_query_len = extent / nentries;
    2577             :     else
    2578           0 :         pgss->mean_query_len = ASSUMED_LENGTH_INIT;
    2579             : 
    2580           0 :     free(qbuffer);
    2581             : 
    2582             :     /*
    2583             :      * OK, count a garbage collection cycle.  (Note: even though we have
    2584             :      * exclusive lock on pgss->lock, we must take pgss->mutex for this, since
    2585             :      * other processes may examine gc_count while holding only the mutex.
    2586             :      * Also, we have to advance the count *after* we've rewritten the file,
    2587             :      * else other processes might not realize they read a stale file.)
    2588             :      */
    2589           0 :     record_gc_qtexts();
    2590             : 
    2591           0 :     return;
    2592             : 
    2593           0 : gc_fail:
    2594             :     /* clean up resources */
    2595           0 :     if (qfile)
    2596           0 :         FreeFile(qfile);
    2597           0 :     free(qbuffer);
    2598             : 
    2599             :     /*
    2600             :      * Since the contents of the external file are now uncertain, mark all
    2601             :      * hashtable entries as having invalid texts.
    2602             :      */
    2603           0 :     hash_seq_init(&hash_seq, pgss_hash);
    2604           0 :     while ((entry = hash_seq_search(&hash_seq)) != NULL)
    2605             :     {
    2606           0 :         entry->query_offset = 0;
    2607           0 :         entry->query_len = -1;
    2608             :     }
    2609             : 
    2610             :     /*
    2611             :      * Destroy the query text file and create a new, empty one
    2612             :      */
    2613           0 :     (void) unlink(PGSS_TEXT_FILE);
    2614           0 :     qfile = AllocateFile(PGSS_TEXT_FILE, PG_BINARY_W);
    2615           0 :     if (qfile == NULL)
    2616           0 :         ereport(LOG,
    2617             :                 (errcode_for_file_access(),
    2618             :                  errmsg("could not recreate file \"%s\": %m",
    2619             :                         PGSS_TEXT_FILE)));
    2620             :     else
    2621           0 :         FreeFile(qfile);
    2622             : 
    2623             :     /* Reset the shared extent pointer */
    2624           0 :     pgss->extent = 0;
    2625             : 
    2626             :     /* Reset mean_query_len to match the new state */
    2627           0 :     pgss->mean_query_len = ASSUMED_LENGTH_INIT;
    2628             : 
    2629             :     /*
    2630             :      * Bump the GC count even though we failed.
    2631             :      *
    2632             :      * This is needed to make concurrent readers of file without any lock on
    2633             :      * pgss->lock notice existence of new version of file.  Once readers
    2634             :      * subsequently observe a change in GC count with pgss->lock held, that
    2635             :      * forces a safe reopen of file.  Writers also require that we bump here,
    2636             :      * of course.  (As required by locking protocol, readers and writers don't
    2637             :      * trust earlier file contents until gc_count is found unchanged after
    2638             :      * pgss->lock acquired in shared or exclusive mode respectively.)
    2639             :      */
    2640           0 :     record_gc_qtexts();
    2641             : }
    2642             : 
    2643             : #define SINGLE_ENTRY_RESET(e) \
    2644             : if (e) { \
    2645             :     if (minmax_only) { \
    2646             :         /* When requested reset only min/max statistics of an entry */ \
    2647             :         for (int kind = 0; kind < PGSS_NUMKIND; kind++) \
    2648             :         { \
    2649             :             e->counters.max_time[kind] = 0; \
    2650             :             e->counters.min_time[kind] = 0; \
    2651             :         } \
    2652             :         e->minmax_stats_since = stats_reset; \
    2653             :     } \
    2654             :     else \
    2655             :     { \
    2656             :         /* Remove the key otherwise  */ \
    2657             :         hash_search(pgss_hash, &e->key, HASH_REMOVE, NULL); \
    2658             :         num_remove++; \
    2659             :     } \
    2660             : }
    2661             : 
    2662             : /*
    2663             :  * Reset entries corresponding to parameters passed.
    2664             :  */
    2665             : static TimestampTz
    2666         160 : entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only)
    2667             : {
    2668             :     HASH_SEQ_STATUS hash_seq;
    2669             :     pgssEntry  *entry;
    2670             :     FILE       *qfile;
    2671             :     long        num_entries;
    2672         160 :     long        num_remove = 0;
    2673             :     pgssHashKey key;
    2674             :     TimestampTz stats_reset;
    2675             : 
    2676         160 :     if (!pgss || !pgss_hash)
    2677           0 :         ereport(ERROR,
    2678             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    2679             :                  errmsg("pg_stat_statements must be loaded via \"shared_preload_libraries\"")));
    2680             : 
    2681         160 :     LWLockAcquire(pgss->lock, LW_EXCLUSIVE);
    2682         160 :     num_entries = hash_get_num_entries(pgss_hash);
    2683             : 
    2684         160 :     stats_reset = GetCurrentTimestamp();
    2685             : 
    2686         160 :     if (userid != 0 && dbid != 0 && queryid != UINT64CONST(0))
    2687             :     {
    2688             :         /* If all the parameters are available, use the fast path. */
    2689           2 :         memset(&key, 0, sizeof(pgssHashKey));
    2690           2 :         key.userid = userid;
    2691           2 :         key.dbid = dbid;
    2692           2 :         key.queryid = queryid;
    2693             : 
    2694             :         /*
    2695             :          * Reset the entry if it exists, starting with the non-top-level
    2696             :          * entry.
    2697             :          */
    2698           2 :         key.toplevel = false;
    2699           2 :         entry = (pgssEntry *) hash_search(pgss_hash, &key, HASH_FIND, NULL);
    2700             : 
    2701           2 :         SINGLE_ENTRY_RESET(entry);
    2702             : 
    2703             :         /* Also reset the top-level entry if it exists. */
    2704           2 :         key.toplevel = true;
    2705           2 :         entry = (pgssEntry *) hash_search(pgss_hash, &key, HASH_FIND, NULL);
    2706             : 
    2707           2 :         SINGLE_ENTRY_RESET(entry);
    2708             :     }
    2709         158 :     else if (userid != 0 || dbid != 0 || queryid != UINT64CONST(0))
    2710             :     {
    2711             :         /* Reset entries corresponding to valid parameters. */
    2712           8 :         hash_seq_init(&hash_seq, pgss_hash);
    2713         102 :         while ((entry = hash_seq_search(&hash_seq)) != NULL)
    2714             :         {
    2715          94 :             if ((!userid || entry->key.userid == userid) &&
    2716          72 :                 (!dbid || entry->key.dbid == dbid) &&
    2717          68 :                 (!queryid || entry->key.queryid == queryid))
    2718             :             {
    2719          14 :                 SINGLE_ENTRY_RESET(entry);
    2720             :             }
    2721             :         }
    2722             :     }
    2723             :     else
    2724             :     {
    2725             :         /* Reset all entries. */
    2726         150 :         hash_seq_init(&hash_seq, pgss_hash);
    2727        1490 :         while ((entry = hash_seq_search(&hash_seq)) != NULL)
    2728             :         {
    2729        1384 :             SINGLE_ENTRY_RESET(entry);
    2730             :         }
    2731             :     }
    2732             : 
    2733             :     /* All entries are removed? */
    2734         160 :     if (num_entries != num_remove)
    2735          12 :         goto release_lock;
    2736             : 
    2737             :     /*
    2738             :      * Reset global statistics for pg_stat_statements since all entries are
    2739             :      * removed.
    2740             :      */
    2741         148 :     SpinLockAcquire(&pgss->mutex);
    2742         148 :     pgss->stats.dealloc = 0;
    2743         148 :     pgss->stats.stats_reset = stats_reset;
    2744         148 :     SpinLockRelease(&pgss->mutex);
    2745             : 
    2746             :     /*
    2747             :      * Write new empty query file, perhaps even creating a new one to recover
    2748             :      * if the file was missing.
    2749             :      */
    2750         148 :     qfile = AllocateFile(PGSS_TEXT_FILE, PG_BINARY_W);
    2751         148 :     if (qfile == NULL)
    2752             :     {
    2753           0 :         ereport(LOG,
    2754             :                 (errcode_for_file_access(),
    2755             :                  errmsg("could not create file \"%s\": %m",
    2756             :                         PGSS_TEXT_FILE)));
    2757           0 :         goto done;
    2758             :     }
    2759             : 
    2760             :     /* If ftruncate fails, log it, but it's not a fatal problem */
    2761         148 :     if (ftruncate(fileno(qfile), 0) != 0)
    2762           0 :         ereport(LOG,
    2763             :                 (errcode_for_file_access(),
    2764             :                  errmsg("could not truncate file \"%s\": %m",
    2765             :                         PGSS_TEXT_FILE)));
    2766             : 
    2767         148 :     FreeFile(qfile);
    2768             : 
    2769         148 : done:
    2770         148 :     pgss->extent = 0;
    2771             :     /* This counts as a query text garbage collection for our purposes */
    2772         148 :     record_gc_qtexts();
    2773             : 
    2774         160 : release_lock:
    2775         160 :     LWLockRelease(pgss->lock);
    2776             : 
    2777         160 :     return stats_reset;
    2778             : }
    2779             : 
    2780             : /*
    2781             :  * Generate a normalized version of the query string that will be used to
    2782             :  * represent all similar queries.
    2783             :  *
    2784             :  * Note that the normalized representation may well vary depending on
    2785             :  * just which "equivalent" query is used to create the hashtable entry.
    2786             :  * We assume this is OK.
    2787             :  *
    2788             :  * If query_loc > 0, then "query" has been advanced by that much compared to
    2789             :  * the original string start, so we need to translate the provided locations
    2790             :  * to compensate.  (This lets us avoid re-scanning statements before the one
    2791             :  * of interest, so it's worth doing.)
    2792             :  *
    2793             :  * *query_len_p contains the input string length, and is updated with
    2794             :  * the result string length on exit.  The resulting string might be longer
    2795             :  * or shorter depending on what happens with replacement of constants.
    2796             :  *
    2797             :  * Returns a palloc'd string.
    2798             :  */
    2799             : static char *
    2800       20748 : generate_normalized_query(JumbleState *jstate, const char *query,
    2801             :                           int query_loc, int *query_len_p)
    2802             : {
    2803             :     char       *norm_query;
    2804       20748 :     int         query_len = *query_len_p;
    2805             :     int         i,
    2806             :                 norm_query_buflen,  /* Space allowed for norm_query */
    2807             :                 len_to_wrt,     /* Length (in bytes) to write */
    2808       20748 :                 quer_loc = 0,   /* Source query byte location */
    2809       20748 :                 n_quer_loc = 0, /* Normalized query byte location */
    2810       20748 :                 last_off = 0,   /* Offset from start for previous tok */
    2811       20748 :                 last_tok_len = 0;   /* Length (in bytes) of that tok */
    2812             : 
    2813             :     /*
    2814             :      * Get constants' lengths (core system only gives us locations).  Note
    2815             :      * this also ensures the items are sorted by location.
    2816             :      */
    2817       20748 :     fill_in_constant_lengths(jstate, query, query_loc);
    2818             : 
    2819             :     /*
    2820             :      * Allow for $n symbols to be longer than the constants they replace.
    2821             :      * Constants must take at least one byte in text form, while a $n symbol
    2822             :      * certainly isn't more than 11 bytes, even if n reaches INT_MAX.  We
    2823             :      * could refine that limit based on the max value of n for the current
    2824             :      * query, but it hardly seems worth any extra effort to do so.
    2825             :      */
    2826       20748 :     norm_query_buflen = query_len + jstate->clocations_count * 10;
    2827             : 
    2828             :     /* Allocate result buffer */
    2829       20748 :     norm_query = palloc(norm_query_buflen + 1);
    2830             : 
    2831       85240 :     for (i = 0; i < jstate->clocations_count; i++)
    2832             :     {
    2833             :         int         off,        /* Offset from start for cur tok */
    2834             :                     tok_len;    /* Length (in bytes) of that tok */
    2835             : 
    2836       64492 :         off = jstate->clocations[i].location;
    2837             :         /* Adjust recorded location if we're dealing with partial string */
    2838       64492 :         off -= query_loc;
    2839             : 
    2840       64492 :         tok_len = jstate->clocations[i].length;
    2841             : 
    2842       64492 :         if (tok_len < 0)
    2843         466 :             continue;           /* ignore any duplicates */
    2844             : 
    2845             :         /* Copy next chunk (what precedes the next constant) */
    2846       64026 :         len_to_wrt = off - last_off;
    2847       64026 :         len_to_wrt -= last_tok_len;
    2848             : 
    2849             :         Assert(len_to_wrt >= 0);
    2850       64026 :         memcpy(norm_query + n_quer_loc, query + quer_loc, len_to_wrt);
    2851       64026 :         n_quer_loc += len_to_wrt;
    2852             : 
    2853             :         /* And insert a param symbol in place of the constant token */
    2854      128052 :         n_quer_loc += sprintf(norm_query + n_quer_loc, "$%d",
    2855       64026 :                               i + 1 + jstate->highest_extern_param_id);
    2856             : 
    2857       64026 :         quer_loc = off + tok_len;
    2858       64026 :         last_off = off;
    2859       64026 :         last_tok_len = tok_len;
    2860             :     }
    2861             : 
    2862             :     /*
    2863             :      * We've copied up until the last ignorable constant.  Copy over the
    2864             :      * remaining bytes of the original query string.
    2865             :      */
    2866       20748 :     len_to_wrt = query_len - quer_loc;
    2867             : 
    2868             :     Assert(len_to_wrt >= 0);
    2869       20748 :     memcpy(norm_query + n_quer_loc, query + quer_loc, len_to_wrt);
    2870       20748 :     n_quer_loc += len_to_wrt;
    2871             : 
    2872             :     Assert(n_quer_loc <= norm_query_buflen);
    2873       20748 :     norm_query[n_quer_loc] = '\0';
    2874             : 
    2875       20748 :     *query_len_p = n_quer_loc;
    2876       20748 :     return norm_query;
    2877             : }
    2878             : 
    2879             : /*
    2880             :  * Given a valid SQL string and an array of constant-location records,
    2881             :  * fill in the textual lengths of those constants.
    2882             :  *
    2883             :  * The constants may use any allowed constant syntax, such as float literals,
    2884             :  * bit-strings, single-quoted strings and dollar-quoted strings.  This is
    2885             :  * accomplished by using the public API for the core scanner.
    2886             :  *
    2887             :  * It is the caller's job to ensure that the string is a valid SQL statement
    2888             :  * with constants at the indicated locations.  Since in practice the string
    2889             :  * has already been parsed, and the locations that the caller provides will
    2890             :  * have originated from within the authoritative parser, this should not be
    2891             :  * a problem.
    2892             :  *
    2893             :  * Duplicate constant pointers are possible, and will have their lengths
    2894             :  * marked as '-1', so that they are later ignored.  (Actually, we assume the
    2895             :  * lengths were initialized as -1 to start with, and don't change them here.)
    2896             :  *
    2897             :  * If query_loc > 0, then "query" has been advanced by that much compared to
    2898             :  * the original string start, so we need to translate the provided locations
    2899             :  * to compensate.  (This lets us avoid re-scanning statements before the one
    2900             :  * of interest, so it's worth doing.)
    2901             :  *
    2902             :  * N.B. There is an assumption that a '-' character at a Const location begins
    2903             :  * a negative numeric constant.  This precludes there ever being another
    2904             :  * reason for a constant to start with a '-'.
    2905             :  */
    2906             : static void
    2907       20748 : fill_in_constant_lengths(JumbleState *jstate, const char *query,
    2908             :                          int query_loc)
    2909             : {
    2910             :     LocationLen *locs;
    2911             :     core_yyscan_t yyscanner;
    2912             :     core_yy_extra_type yyextra;
    2913             :     core_YYSTYPE yylval;
    2914             :     YYLTYPE     yylloc;
    2915       20748 :     int         last_loc = -1;
    2916             :     int         i;
    2917             : 
    2918             :     /*
    2919             :      * Sort the records by location so that we can process them in order while
    2920             :      * scanning the query text.
    2921             :      */
    2922       20748 :     if (jstate->clocations_count > 1)
    2923       13500 :         qsort(jstate->clocations, jstate->clocations_count,
    2924             :               sizeof(LocationLen), comp_location);
    2925       20748 :     locs = jstate->clocations;
    2926             : 
    2927             :     /* initialize the flex scanner --- should match raw_parser() */
    2928       20748 :     yyscanner = scanner_init(query,
    2929             :                              &yyextra,
    2930             :                              &ScanKeywords,
    2931             :                              ScanKeywordTokens);
    2932             : 
    2933             :     /* we don't want to re-emit any escape string warnings */
    2934       20748 :     yyextra.escape_string_warning = false;
    2935             : 
    2936             :     /* Search for each constant, in sequence */
    2937       85240 :     for (i = 0; i < jstate->clocations_count; i++)
    2938             :     {
    2939       64492 :         int         loc = locs[i].location;
    2940             :         int         tok;
    2941             : 
    2942             :         /* Adjust recorded location if we're dealing with partial string */
    2943       64492 :         loc -= query_loc;
    2944             : 
    2945             :         Assert(loc >= 0);
    2946             : 
    2947       64492 :         if (loc <= last_loc)
    2948         466 :             continue;           /* Duplicate constant, ignore */
    2949             : 
    2950             :         /* Lex tokens until we find the desired constant */
    2951             :         for (;;)
    2952             :         {
    2953      474512 :             tok = core_yylex(&yylval, &yylloc, yyscanner);
    2954             : 
    2955             :             /* We should not hit end-of-string, but if we do, behave sanely */
    2956      474512 :             if (tok == 0)
    2957           0 :                 break;          /* out of inner for-loop */
    2958             : 
    2959             :             /*
    2960             :              * We should find the token position exactly, but if we somehow
    2961             :              * run past it, work with that.
    2962             :              */
    2963      474512 :             if (yylloc >= loc)
    2964             :             {
    2965       64026 :                 if (query[loc] == '-')
    2966             :                 {
    2967             :                     /*
    2968             :                      * It's a negative value - this is the one and only case
    2969             :                      * where we replace more than a single token.
    2970             :                      *
    2971             :                      * Do not compensate for the core system's special-case
    2972             :                      * adjustment of location to that of the leading '-'
    2973             :                      * operator in the event of a negative constant.  It is
    2974             :                      * also useful for our purposes to start from the minus
    2975             :                      * symbol.  In this way, queries like "select * from foo
    2976             :                      * where bar = 1" and "select * from foo where bar = -2"
    2977             :                      * will have identical normalized query strings.
    2978             :                      */
    2979         816 :                     tok = core_yylex(&yylval, &yylloc, yyscanner);
    2980         816 :                     if (tok == 0)
    2981           0 :                         break;  /* out of inner for-loop */
    2982             :                 }
    2983             : 
    2984             :                 /*
    2985             :                  * We now rely on the assumption that flex has placed a zero
    2986             :                  * byte after the text of the current token in scanbuf.
    2987             :                  */
    2988       64026 :                 locs[i].length = strlen(yyextra.scanbuf + loc);
    2989       64026 :                 break;          /* out of inner for-loop */
    2990             :             }
    2991             :         }
    2992             : 
    2993             :         /* If we hit end-of-string, give up, leaving remaining lengths -1 */
    2994       64026 :         if (tok == 0)
    2995           0 :             break;
    2996             : 
    2997       64026 :         last_loc = loc;
    2998             :     }
    2999             : 
    3000       20748 :     scanner_finish(yyscanner);
    3001       20748 : }
    3002             : 
    3003             : /*
    3004             :  * comp_location: comparator for qsorting LocationLen structs by location
    3005             :  */
    3006             : static int
    3007       75416 : comp_location(const void *a, const void *b)
    3008             : {
    3009       75416 :     int         l = ((const LocationLen *) a)->location;
    3010       75416 :     int         r = ((const LocationLen *) b)->location;
    3011             : 
    3012       75416 :     return pg_cmp_s32(l, r);
    3013             : }

Generated by: LCOV version 1.14