mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
Teach contrib/pg_stat_statements to handle multi-statement commands better.
Make use of the statement boundary info added by commit ab1f0c822
to let pg_stat_statements behave more sanely when multiple SQL queries
are jammed into one query string. It now records just the relevant
part of the source string, not the whole thing, for each individual
query.
Even when no multi-statement strings are involved, users may notice small
changes in the output: leading and trailing whitespace and semicolons will
be stripped from statements, which did not happen before.
Also, significantly expand pg_stat_statements' regression test script.
Fabien Coelho, reviewed by Craig Ringer and Kyotaro Horiguchi,
some mods by me
Discussion: https://postgr.es/m/alpine.DEB.2.20.1612200926310.29821@lancre
This commit is contained in:
@ -69,6 +69,7 @@
|
||||
#include "parser/analyze.h"
|
||||
#include "parser/parsetree.h"
|
||||
#include "parser/scanner.h"
|
||||
#include "parser/scansup.h"
|
||||
#include "pgstat.h"
|
||||
#include "storage/fd.h"
|
||||
#include "storage/ipc.h"
|
||||
@ -297,8 +298,9 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
DestReceiver *dest, char *completionTag);
|
||||
static uint32 pgss_hash_fn(const void *key, Size keysize);
|
||||
static int pgss_match_fn(const void *key1, const void *key2, Size keysize);
|
||||
static uint32 pgss_hash_string(const char *str);
|
||||
static uint32 pgss_hash_string(const char *str, int len);
|
||||
static void pgss_store(const char *query, uint32 queryId,
|
||||
int query_location, int query_len,
|
||||
double total_time, uint64 rows,
|
||||
const BufferUsage *bufusage,
|
||||
pgssJumbleState *jstate);
|
||||
@ -324,8 +326,9 @@ static void JumbleRangeTable(pgssJumbleState *jstate, List *rtable);
|
||||
static void JumbleExpr(pgssJumbleState *jstate, Node *node);
|
||||
static void RecordConstLocation(pgssJumbleState *jstate, int location);
|
||||
static char *generate_normalized_query(pgssJumbleState *jstate, const char *query,
|
||||
int *query_len_p, int encoding);
|
||||
static void fill_in_constant_lengths(pgssJumbleState *jstate, const char *query);
|
||||
int query_loc, int *query_len_p, int encoding);
|
||||
static void fill_in_constant_lengths(pgssJumbleState *jstate, const char *query,
|
||||
int query_loc);
|
||||
static int comp_location(const void *a, const void *b);
|
||||
|
||||
|
||||
@ -822,6 +825,8 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query)
|
||||
if (jstate.clocations_count > 0)
|
||||
pgss_store(pstate->p_sourcetext,
|
||||
query->queryId,
|
||||
query->stmt_location,
|
||||
query->stmt_len,
|
||||
0,
|
||||
0,
|
||||
NULL,
|
||||
@ -926,6 +931,8 @@ pgss_ExecutorEnd(QueryDesc *queryDesc)
|
||||
|
||||
pgss_store(queryDesc->sourceText,
|
||||
queryId,
|
||||
queryDesc->plannedstmt->stmt_location,
|
||||
queryDesc->plannedstmt->stmt_len,
|
||||
queryDesc->totaltime->total * 1000.0, /* convert to msec */
|
||||
queryDesc->estate->es_processed,
|
||||
&queryDesc->totaltime->bufusage,
|
||||
@ -972,7 +979,6 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
uint64 rows;
|
||||
BufferUsage bufusage_start,
|
||||
bufusage;
|
||||
uint32 queryId;
|
||||
|
||||
bufusage_start = pgBufferUsage;
|
||||
INSTR_TIME_SET_CURRENT(start);
|
||||
@ -1033,11 +1039,10 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
|
||||
bufusage.blk_write_time = pgBufferUsage.blk_write_time;
|
||||
INSTR_TIME_SUBTRACT(bufusage.blk_write_time, bufusage_start.blk_write_time);
|
||||
|
||||
/* For utility statements, we just hash the query string directly */
|
||||
queryId = pgss_hash_string(queryString);
|
||||
|
||||
pgss_store(queryString,
|
||||
queryId,
|
||||
0, /* signal that it's a utility stmt */
|
||||
pstmt->stmt_location,
|
||||
pstmt->stmt_len,
|
||||
INSTR_TIME_GET_MILLISEC(duration),
|
||||
rows,
|
||||
&bufusage,
|
||||
@ -1092,20 +1097,24 @@ pgss_match_fn(const void *key1, const void *key2, Size keysize)
|
||||
* utility statements.
|
||||
*/
|
||||
static uint32
|
||||
pgss_hash_string(const char *str)
|
||||
pgss_hash_string(const char *str, int len)
|
||||
{
|
||||
return hash_any((const unsigned char *) str, strlen(str));
|
||||
return hash_any((const unsigned char *) str, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Store some statistics for a statement.
|
||||
*
|
||||
* If queryId is 0 then this is a utility statement and we should compute
|
||||
* a suitable queryId internally.
|
||||
*
|
||||
* If jstate is not NULL then we're trying to create an entry for which
|
||||
* we have no statistics as yet; we just want to record the normalized
|
||||
* query string. total_time, rows, bufusage are ignored in this case.
|
||||
*/
|
||||
static void
|
||||
pgss_store(const char *query, uint32 queryId,
|
||||
int query_location, int query_len,
|
||||
double total_time, uint64 rows,
|
||||
const BufferUsage *bufusage,
|
||||
pgssJumbleState *jstate)
|
||||
@ -1114,7 +1123,6 @@ pgss_store(const char *query, uint32 queryId,
|
||||
pgssEntry *entry;
|
||||
char *norm_query = NULL;
|
||||
int encoding = GetDatabaseEncoding();
|
||||
int query_len;
|
||||
|
||||
Assert(query != NULL);
|
||||
|
||||
@ -1122,7 +1130,43 @@ pgss_store(const char *query, uint32 queryId,
|
||||
if (!pgss || !pgss_hash)
|
||||
return;
|
||||
|
||||
query_len = strlen(query);
|
||||
/*
|
||||
* Confine our attention to the relevant part of the string, if the query
|
||||
* is a portion of a multi-statement source string.
|
||||
*
|
||||
* First apply starting offset, unless it's -1 (unknown).
|
||||
*/
|
||||
if (query_location >= 0)
|
||||
{
|
||||
Assert(query_location <= strlen(query));
|
||||
query += query_location;
|
||||
/* Length of 0 (or -1) means "rest of string" */
|
||||
if (query_len <= 0)
|
||||
query_len = strlen(query);
|
||||
else
|
||||
Assert(query_len <= strlen(query));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If query location is unknown, distrust query_len as well */
|
||||
query_location = 0;
|
||||
query_len = strlen(query);
|
||||
}
|
||||
|
||||
/*
|
||||
* Discard leading and trailing whitespace, too. Use scanner_isspace()
|
||||
* not libc's isspace(), because we want to match the lexer's behavior.
|
||||
*/
|
||||
while (query_len > 0 && scanner_isspace(query[0]))
|
||||
query++, query_location++, query_len--;
|
||||
while (query_len > 0 && scanner_isspace(query[query_len - 1]))
|
||||
query_len--;
|
||||
|
||||
/*
|
||||
* For utility statements, we just hash the query string to get an ID.
|
||||
*/
|
||||
if (queryId == 0)
|
||||
queryId = pgss_hash_string(query, query_len);
|
||||
|
||||
/* Set up key for hashtable search */
|
||||
key.userid = GetUserId();
|
||||
@ -1153,6 +1197,7 @@ pgss_store(const char *query, uint32 queryId,
|
||||
{
|
||||
LWLockRelease(pgss->lock);
|
||||
norm_query = generate_normalized_query(jstate, query,
|
||||
query_location,
|
||||
&query_len,
|
||||
encoding);
|
||||
LWLockAcquire(pgss->lock, LW_SHARED);
|
||||
@ -1772,11 +1817,8 @@ entry_dealloc(void)
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a null-terminated string, allocate a new entry in the external query
|
||||
* text file and store the string there.
|
||||
*
|
||||
* Although we could compute the string length via strlen(), callers already
|
||||
* have it handy, so we require them to pass it too.
|
||||
* Given a query string (not necessarily null-terminated), allocate a new
|
||||
* entry in the external query text file and store the string there.
|
||||
*
|
||||
* If successful, returns true, and stores the new entry's offset in the file
|
||||
* into *query_offset. Also, if gc_count isn't NULL, *gc_count is set to the
|
||||
@ -1824,7 +1866,9 @@ qtext_store(const char *query, int query_len,
|
||||
if (lseek(fd, off, SEEK_SET) != off)
|
||||
goto error;
|
||||
|
||||
if (write(fd, query, query_len + 1) != query_len + 1)
|
||||
if (write(fd, query, query_len) != query_len)
|
||||
goto error;
|
||||
if (write(fd, "\0", 1) != 1)
|
||||
goto error;
|
||||
|
||||
CloseTransientFile(fd);
|
||||
@ -2877,6 +2921,11 @@ RecordConstLocation(pgssJumbleState *jstate, int location)
|
||||
* just which "equivalent" query is used to create the hashtable entry.
|
||||
* We assume this is OK.
|
||||
*
|
||||
* If query_loc > 0, then "query" has been advanced by that much compared to
|
||||
* the original string start, so we need to translate the provided locations
|
||||
* to compensate. (This lets us avoid re-scanning statements before the one
|
||||
* of interest, so it's worth doing.)
|
||||
*
|
||||
* *query_len_p contains the input string length, and is updated with
|
||||
* the result string length (which cannot be longer) on exit.
|
||||
*
|
||||
@ -2884,7 +2933,7 @@ RecordConstLocation(pgssJumbleState *jstate, int location)
|
||||
*/
|
||||
static char *
|
||||
generate_normalized_query(pgssJumbleState *jstate, const char *query,
|
||||
int *query_len_p, int encoding)
|
||||
int query_loc, int *query_len_p, int encoding)
|
||||
{
|
||||
char *norm_query;
|
||||
int query_len = *query_len_p;
|
||||
@ -2899,7 +2948,7 @@ generate_normalized_query(pgssJumbleState *jstate, const char *query,
|
||||
* Get constants' lengths (core system only gives us locations). Note
|
||||
* this also ensures the items are sorted by location.
|
||||
*/
|
||||
fill_in_constant_lengths(jstate, query);
|
||||
fill_in_constant_lengths(jstate, query, query_loc);
|
||||
|
||||
/* Allocate result buffer */
|
||||
norm_query = palloc(query_len + 1);
|
||||
@ -2910,6 +2959,9 @@ generate_normalized_query(pgssJumbleState *jstate, const char *query,
|
||||
tok_len; /* Length (in bytes) of that tok */
|
||||
|
||||
off = jstate->clocations[i].location;
|
||||
/* Adjust recorded location if we're dealing with partial string */
|
||||
off -= query_loc;
|
||||
|
||||
tok_len = jstate->clocations[i].length;
|
||||
|
||||
if (tok_len < 0)
|
||||
@ -2966,12 +3018,18 @@ generate_normalized_query(pgssJumbleState *jstate, const char *query,
|
||||
* marked as '-1', so that they are later ignored. (Actually, we assume the
|
||||
* lengths were initialized as -1 to start with, and don't change them here.)
|
||||
*
|
||||
* If query_loc > 0, then "query" has been advanced by that much compared to
|
||||
* the original string start, so we need to translate the provided locations
|
||||
* to compensate. (This lets us avoid re-scanning statements before the one
|
||||
* of interest, so it's worth doing.)
|
||||
*
|
||||
* N.B. There is an assumption that a '-' character at a Const location begins
|
||||
* a negative numeric constant. This precludes there ever being another
|
||||
* reason for a constant to start with a '-'.
|
||||
*/
|
||||
static void
|
||||
fill_in_constant_lengths(pgssJumbleState *jstate, const char *query)
|
||||
fill_in_constant_lengths(pgssJumbleState *jstate, const char *query,
|
||||
int query_loc)
|
||||
{
|
||||
pgssLocationLen *locs;
|
||||
core_yyscan_t yyscanner;
|
||||
@ -3005,6 +3063,9 @@ fill_in_constant_lengths(pgssJumbleState *jstate, const char *query)
|
||||
int loc = locs[i].location;
|
||||
int tok;
|
||||
|
||||
/* Adjust recorded location if we're dealing with partial string */
|
||||
loc -= query_loc;
|
||||
|
||||
Assert(loc >= 0);
|
||||
|
||||
if (loc <= last_loc)
|
||||
|
Reference in New Issue
Block a user