Skip to content

Commit 046589e

Browse files
committed
PG-1459 Keep username and datname empty if we cannont get real values
If we cannot get username and datname real values just keep these fields empty.
1 parent b6ffd37 commit 046589e

4 files changed

Lines changed: 116 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ REGRESS = basic \
2929
squashing \
3030
tags \
3131
user \
32+
rollback \
3233
level_tracking \
3334
decode_error_level \
3435
parallel \

pg_stat_monitor.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,19 +1743,16 @@ pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info)
17431743
{
17441744
datname = get_database_name(entry->key.dbid);
17451745
username = GetUserNameFromId(entry->key.userid, true);
1746-
}
1747-
1748-
if (!datname)
1749-
datname = pnstrdup("<database name not available>", sizeof(entry->datname) - 1);
17501746

1751-
if (!username)
1752-
username = pnstrdup("<user name not available>", sizeof(entry->username) - 1);
1747+
snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
1748+
snprintf(entry->username, sizeof(entry->username), "%s", username);
17531749

1754-
snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
1755-
snprintf(entry->username, sizeof(entry->username), "%s", username);
1750+
if (datname)
1751+
pfree(datname);
17561752

1757-
pfree(datname);
1758-
pfree(username);
1753+
if (username)
1754+
pfree(username);
1755+
}
17591756

17601757
MemoryContextSwitchTo(oldctx);
17611758

regression/expected/rollback.out

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
CREATE EXTENSION pg_stat_monitor;
2+
CREATE USER u WITH SUPERUSER;
3+
SET ROLE u;
4+
CREATE TABLE t (a int PRIMARY KEY) ;
5+
SELECT pg_stat_monitor_reset();
6+
pg_stat_monitor_reset
7+
-----------------------
8+
9+
(1 row)
10+
11+
BEGIN;
12+
INSERT INTO t VALUES (1);
13+
COMMIT;
14+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
15+
query | username | datname
16+
--------------------------------+----------+--------------------
17+
BEGIN | u | contrib_regression
18+
COMMIT | u | contrib_regression
19+
INSERT INTO t VALUES (1) | u | contrib_regression
20+
SELECT pg_stat_monitor_reset() | u | contrib_regression
21+
(4 rows)
22+
23+
SELECT pg_stat_monitor_reset();
24+
pg_stat_monitor_reset
25+
-----------------------
26+
27+
(1 row)
28+
29+
-- During manual rollback we will be able to get username and datname of the query that was rolled back.
30+
BEGIN;
31+
INSERT INTO t VALUES (2);
32+
ROLLBACK;
33+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
34+
query | username | datname
35+
--------------------------------+----------+--------------------
36+
BEGIN | u | contrib_regression
37+
INSERT INTO t VALUES (2) | u | contrib_regression
38+
ROLLBACK | u | contrib_regression
39+
SELECT pg_stat_monitor_reset() | u | contrib_regression
40+
(4 rows)
41+
42+
SELECT pg_stat_monitor_reset();
43+
pg_stat_monitor_reset
44+
-----------------------
45+
46+
(1 row)
47+
48+
-- If transaction is rolled back due an error, we won't be able to get username and datname of the query that was rolled back.
49+
BEGIN;
50+
INSERT INTO t VALUES (1);
51+
ERROR: duplicate key value violates unique constraint "t_pkey"
52+
DETAIL: Key (a)=(1) already exists.
53+
ROLLBACK;
54+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
55+
query | username | datname
56+
--------------------------------+----------+--------------------
57+
BEGIN | u | contrib_regression
58+
INSERT INTO t VALUES (1); | u | contrib_regression
59+
ROLLBACK | |
60+
SELECT pg_stat_monitor_reset() | u | contrib_regression
61+
(4 rows)
62+
63+
SELECT pg_stat_monitor_reset();
64+
pg_stat_monitor_reset
65+
-----------------------
66+
67+
(1 row)
68+
69+
DROP TABLE t;
70+
SET ROLE NONE;
71+
DROP USER u;
72+
DROP EXTENSION pg_stat_monitor;

regression/sql/rollback.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
CREATE EXTENSION pg_stat_monitor;
2+
3+
CREATE USER u WITH SUPERUSER;
4+
SET ROLE u;
5+
CREATE TABLE t (a int PRIMARY KEY) ;
6+
7+
SELECT pg_stat_monitor_reset();
8+
9+
BEGIN;
10+
INSERT INTO t VALUES (1);
11+
COMMIT;
12+
13+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
14+
SELECT pg_stat_monitor_reset();
15+
16+
-- During manual rollback we will be able to get username and datname of the query that was rolled back.
17+
BEGIN;
18+
INSERT INTO t VALUES (2);
19+
ROLLBACK;
20+
21+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
22+
SELECT pg_stat_monitor_reset();
23+
24+
-- If transaction is rolled back due an error, we won't be able to get username and datname of the query that was rolled back.
25+
BEGIN;
26+
INSERT INTO t VALUES (1);
27+
ROLLBACK;
28+
29+
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
30+
SELECT pg_stat_monitor_reset();
31+
32+
DROP TABLE t;
33+
SET ROLE NONE;
34+
DROP USER u;
35+
36+
DROP EXTENSION pg_stat_monitor;

0 commit comments

Comments
 (0)