PG-1621: fix cmd_type mostly showing 0 values (#538)

This was actually caused by two bugs internally:

* cmd_type was only set in some codepaths, other parts of the code
never set a value. Depending on which query / how was executed,
it was possibly never changed (after a reset to 0)
* the update first set the cmd_type, then reset all counters. As
the cmd_type is stored within the counters for some reason, this
reset its value to 0 in most execution paths, even if it was corretly
set before.

And according to this the fix is simple:

* cmd_type is now set in all codepaths except for failing queries,
as we only have the error string in this case, without the type.
* in the update logic, we again overwrite cmd_type with the proper
value after a reset
This commit is contained in:
Zsolt Parragi
2025-06-17 15:52:28 +02:00
committed by GitHub
parent f7dc7fb5fe
commit 61662cc58f
8 changed files with 51 additions and 15 deletions

View File

@@ -27,16 +27,16 @@ DROP TABLE t2;
SELECT query, cmd_type, cmd_type_text FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | cmd_type | cmd_type_text
--------------------------------+----------+---------------
CREATE TABLE t1 (a INTEGER) | 0 |
CREATE TABLE t2 (b INTEGER) | 0 |
CREATE TABLE t1 (a INTEGER) | 5 | UTILITY
CREATE TABLE t2 (b INTEGER) | 5 | UTILITY
DELETE FROM t1 | 4 | DELETE
DROP TABLE t1 | 0 |
DROP TABLE t2 | 0 |
DROP TABLE t1 | 5 | UTILITY
DROP TABLE t2 | 5 | UTILITY
INSERT INTO t1 VALUES(1) | 3 | INSERT
SELECT a FROM t1 | 1 | SELECT
SELECT b FROM t2 FOR UPDATE | 1 | SELECT
SELECT pg_stat_monitor_reset() | 1 | SELECT
TRUNCATE t1 | 0 |
TRUNCATE t1 | 5 | UTILITY
UPDATE t1 SET a = 2 | 2 | UPDATE
(11 rows)

View File

@@ -23,16 +23,22 @@ SELECT b FROM t2 FOR UPDATE;
TRUNCATE t1;
DROP TABLE t1;
DROP TABLE t2;
SELECT query, cmd_type, cmd_type_text FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | cmd_type | cmd_type_text
--------------------------------+----------+---------------
CREATE TABLE t1 (a INTEGER) | 6 | UTILITY
CREATE TABLE t2 (b INTEGER) | 6 | UTILITY
DELETE FROM t1 | 4 | DELETE
DROP TABLE t1 | 6 | UTILITY
DROP TABLE t2 | 6 | UTILITY
INSERT INTO t1 VALUES(1) | 3 | INSERT
SELECT a FROM t1 | 1 | SELECT
SELECT b FROM t2 FOR UPDATE | 1 | SELECT
SELECT pg_stat_monitor_reset() | 1 | SELECT
TRUNCATE t1 | 6 | UTILITY
UPDATE t1 SET a = 2 | 2 | UPDATE
(6 rows)
(11 rows)
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset

View File

@@ -2,7 +2,7 @@ CREATE EXTENSION pg_stat_monitor;
SELECT pg_stat_monitor_version();
pg_stat_monitor_version
-------------------------
2.1.1
2.2.0
(1 row)
DROP EXTENSION pg_stat_monitor;