Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- Race condition where we could leak memory for the parent query
- `plans` now counts only actual planner invocations instead of every execution: utility statements and executions that reuse a cached plan no longer bump the counter
- Normalize the query text of utility statements ([PG-2623](https://perconadev.atlassian.net/browse/PG-2623))
- Ignore statements that have no query id

## [2.3.2] - 2026-03-02

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ REGRESS = basic \
version \
guc \
pgsm_query_id \
query_id \
functions \
counters \
relations \
Expand Down
7 changes: 3 additions & 4 deletions regression/expected/level_tracking_4.out
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ SELECT pg_stat_monitor_reset();
CALL proc_with_utility_stmt();
SELECT toplevel, calls, query FROM pg_stat_monitor
ORDER BY query COLLATE "C", toplevel;
toplevel | calls | query
----------+-------+---------------------------------
toplevel | calls | query
----------+-------+--------------------------------
t | 1 | CALL proc_with_utility_stmt()
t | 1 | SELECT pg_stat_monitor_reset()
f | 3 | SHOW pg_stat_monitor.pgsm_track
Comment thread
artemgavrilov marked this conversation as resolved.
(3 rows)
(2 rows)

-- top-level tracking.
SET pg_stat_monitor.pgsm_track = 'top';
Expand Down
7 changes: 3 additions & 4 deletions regression/expected/level_tracking_5.out
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ SELECT pg_stat_monitor_reset();
CALL proc_with_utility_stmt();
SELECT toplevel, calls, query FROM pg_stat_monitor
ORDER BY query COLLATE "C", toplevel;
toplevel | calls | query
----------+-------+---------------------------------
toplevel | calls | query
----------+-------+--------------------------------
t | 1 | CALL proc_with_utility_stmt()
t | 1 | SELECT pg_stat_monitor_reset()
f | 3 | SHOW pg_stat_monitor.pgsm_track
(3 rows)
(2 rows)

-- top-level tracking.
SET pg_stat_monitor.pgsm_track = 'top';
Expand Down
79 changes: 79 additions & 0 deletions regression/expected/query_id.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
CREATE EXTENSION pg_stat_monitor;
SET pg_stat_monitor.pgsm_track_utility = on;
SET pg_stat_monitor.pgsm_track_planning = on;
SET pg_stat_monitor.pgsm_normalized_query = on;
--
-- Do not store queries without a query identifier
--
SET compute_query_id = off;
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
t
---
t
(1 row)

SELECT 1 AS no_query_id;
no_query_id
-------------
1
(1 row)

CREATE TABLE no_query_id_tab (x int);
INSERT INTO no_query_id_tab VALUES (1);
SELECT x FROM no_query_id_tab;
x
---
1
(1 row)

PREPARE no_query_id_prep AS SELECT x FROM no_query_id_tab WHERE x = $1;
EXECUTE no_query_id_prep(1);
x
---
1
(1 row)

DEALLOCATE no_query_id_prep;
DROP TABLE no_query_id_tab;
DO $$
BEGIN
PERFORM 1;
END
$$;
-- Nothing of the above was recorded.
SELECT count(*) FROM pg_stat_monitor;
count
-------
0
(1 row)

--
-- Tracking resumes once query identifiers are computed again.
--
RESET compute_query_id;
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
t
---
t
(1 row)

SELECT 1 AS with_query_id;
with_query_id
---------------
1
(1 row)

SELECT calls, query FROM pg_stat_monitor
WHERE query LIKE '%with_query_id%' ORDER BY query COLLATE "C";
calls | query
-------+----------------------------
1 | SELECT $1 AS with_query_id
(1 row)

SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
t
---
t
(1 row)

DROP EXTENSION pg_stat_monitor;
37 changes: 37 additions & 0 deletions regression/sql/query_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
CREATE EXTENSION pg_stat_monitor;
SET pg_stat_monitor.pgsm_track_utility = on;
SET pg_stat_monitor.pgsm_track_planning = on;
SET pg_stat_monitor.pgsm_normalized_query = on;

--
-- Do not store queries without a query identifier
--
SET compute_query_id = off;
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
SELECT 1 AS no_query_id;
CREATE TABLE no_query_id_tab (x int);
INSERT INTO no_query_id_tab VALUES (1);
SELECT x FROM no_query_id_tab;
PREPARE no_query_id_prep AS SELECT x FROM no_query_id_tab WHERE x = $1;
EXECUTE no_query_id_prep(1);
DEALLOCATE no_query_id_prep;
DROP TABLE no_query_id_tab;
DO $$
BEGIN
PERFORM 1;
END
$$;
-- Nothing of the above was recorded.
SELECT count(*) FROM pg_stat_monitor;

--
-- Tracking resumes once query identifiers are computed again.
--
RESET compute_query_id;
SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
SELECT 1 AS with_query_id;
SELECT calls, query FROM pg_stat_monitor
WHERE query LIKE '%with_query_id%' ORDER BY query COLLATE "C";

SELECT pg_stat_monitor_reset() IS NOT NULL AS t;
DROP EXTENSION pg_stat_monitor;
16 changes: 14 additions & 2 deletions src/pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,13 @@ pgsm_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
if (!pgsm_enabled(nesting_level))
return;

/*
* Nothing to do if compute_query_id isn't enabled and no other module
* computed a query identifier.
*/
if (query->queryId == INT64CONST(0))
return;

/*
* If it's EXECUTE, clear the queryId so that stats will accumulate for
* the underlying PREPARE. But don't do this if we're not tracking
Expand Down Expand Up @@ -507,8 +514,6 @@ pgsm_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
if (query->utilityStmt && norm_query == NULL)
return;

Assert(query->queryId != INT64CONST(0));

/*
* pgsm_query_id always groups by the normalized form when we have one.
* But what gets stored as query text depends on the pgsm_normalized_query
Expand Down Expand Up @@ -1806,6 +1811,13 @@ pgsm_store(const pgsmQueryStats *stats)
if (!IsSystemInitialized())
return;

/*
* Nothing to do if compute_query_id isn't enabled and no other module
* computed a query identifier.
*/
if (key.queryid == INT64CONST(0))
return;

pgsm = pgsm_get_ss();

key.bucket_id = get_next_wbucket(pgsm);
Expand Down
Loading