LCOV - differential code coverage report
Current view: top level - contrib/pg_stat_statements - pg_stat_statements.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC DCB
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 74.3 % 991 736 1 254 18 718 2
Current Date: 2025-10-26 12:31:50 -0700 Functions: 86.8 % 53 46 7 6 40
Baseline: lcov-20251027-010456-baseline Branches: 57.1 % 701 400 4 2 2 293 4 1 8 387
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 94.8 % 58 55 1 2 18 37
(360..) days: 73.0 % 933 681 252 681
Function coverage date bins:
(30,360] days: 100.0 % 6 6 4 2
(360..) days: 85.1 % 47 40 7 2 38
Branch coverage date bins:
(30,360] days: 83.9 % 56 47 4 5 8 39
(360..) days: 54.7 % 645 353 2 2 288 4 1 348

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

Generated by: LCOV version 2.4-beta