1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

Make use of in-core query id added by commit 5fd9dfa5f5

Use the in-core query id computation for pg_stat_activity,
log_line_prefix, and EXPLAIN VERBOSE.

Similar to other fields in pg_stat_activity, only the queryid from the
top level statements are exposed, and if the backends status isn't
active then the queryid from the last executed statements is displayed.

Add a %Q placeholder to include the queryid in log_line_prefix, which
will also only expose top level statements.

For EXPLAIN VERBOSE, if a query identifier has been computed, either by
enabling compute_query_id or using a third-party module, display it.

Bump catalog version.

Discussion: https://postgr.es/m/20210407125726.tkvjdbw76hxnpwfi@nol

Author: Julien Rouhaud

Reviewed-by: Alvaro Herrera, Nitin Jadhav, Zhihong Yu
This commit is contained in:
Bruce Momjian
2021-04-07 14:03:56 -04:00
parent ec7ffb8096
commit 4f0b0966c8
21 changed files with 251 additions and 106 deletions

View File

@@ -544,6 +544,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
beentry->st_activity_start_timestamp = 0;
/* st_xact_start_timestamp and wait_event_info are also disabled */
beentry->st_xact_start_timestamp = 0;
beentry->st_queryid = UINT64CONST(0);
proc->wait_event_info = 0;
PGSTAT_END_WRITE_ACTIVITY(beentry);
}
@@ -598,6 +599,14 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
beentry->st_state = state;
beentry->st_state_start_timestamp = current_timestamp;
/*
* If a new query is started, we reset the query identifier as it'll only
* be known after parse analysis, to avoid reporting last query's
* identifier.
*/
if (state == STATE_RUNNING)
beentry->st_queryid = UINT64CONST(0);
if (cmd_str != NULL)
{
memcpy((char *) beentry->st_activity_raw, cmd_str, len);
@@ -608,6 +617,46 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
PGSTAT_END_WRITE_ACTIVITY(beentry);
}
/* --------
* pgstat_report_queryid() -
*
* Called to update top-level query identifier.
* --------
*/
void
pgstat_report_queryid(uint64 queryId, bool force)
{
volatile PgBackendStatus *beentry = MyBEEntry;
/*
* if track_activities is disabled, st_queryid should already have been
* reset
*/
if (!beentry || !pgstat_track_activities)
return;
/*
* We only report the top-level query identifiers. The stored queryid is
* reset when a backend calls pgstat_report_activity(STATE_RUNNING), or
* with an explicit call to this function using the force flag. If the
* saved query identifier is not zero it means that it's not a top-level
* command, so ignore the one provided unless it's an explicit call to
* reset the identifier.
*/
if (beentry->st_queryid != 0 && !force)
return;
/*
* Update my status entry, following the protocol of bumping
* st_changecount before and after. We use a volatile pointer here to
* ensure the compiler doesn't try to get cute.
*/
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
beentry->st_queryid = queryId;
PGSTAT_END_WRITE_ACTIVITY(beentry);
}
/* ----------
* pgstat_report_appname() -
*
@@ -972,6 +1021,25 @@ pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
return NULL;
}
/* ----------
* pgstat_get_my_queryid() -
*
* Return current backend's query identifier.
*/
uint64
pgstat_get_my_queryid(void)
{
if (!MyBEEntry)
return 0;
/* There's no need for a look around pgstat_begin_read_activity /
* pgstat_end_read_activity here as it's only called from
* pg_stat_get_activity which is already protected, or from the same
* backend which mean that there won't be concurrent write.
*/
return MyBEEntry->st_queryid;
}
/* ----------
* pgstat_fetch_stat_beentry() -