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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ REGRESS = basic \
squashing \
tags \
user \
rollback \
level_tracking \
decode_error_level \
parallel \
Expand Down
17 changes: 7 additions & 10 deletions pg_stat_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1743,19 +1743,16 @@ pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info)
{
datname = get_database_name(entry->key.dbid);
username = GetUserNameFromId(entry->key.userid, true);
}

if (!datname)
datname = pnstrdup("<database name not available>", sizeof(entry->datname) - 1);

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

snprintf(entry->datname, sizeof(entry->datname), "%s", datname);
snprintf(entry->username, sizeof(entry->username), "%s", username);
if (datname)
pfree(datname);

pfree(datname);
pfree(username);
if (username)
pfree(username);
}

MemoryContextSwitchTo(oldctx);

Expand Down
72 changes: 72 additions & 0 deletions regression/expected/rollback.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
CREATE EXTENSION pg_stat_monitor;
CREATE USER u WITH SUPERUSER;
SET ROLE u;
CREATE TABLE t (a int PRIMARY KEY) ;
SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

BEGIN;
INSERT INTO t VALUES (1);
COMMIT;
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | username | datname
--------------------------------+----------+--------------------
BEGIN | u | contrib_regression
COMMIT | u | contrib_regression
INSERT INTO t VALUES (1) | u | contrib_regression
SELECT pg_stat_monitor_reset() | u | contrib_regression
(4 rows)

SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

-- During manual rollback we will be able to get username and datname of the query that was rolled back.
BEGIN;
INSERT INTO t VALUES (2);
ROLLBACK;
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | username | datname
--------------------------------+----------+--------------------
BEGIN | u | contrib_regression
INSERT INTO t VALUES (2) | u | contrib_regression
ROLLBACK | u | contrib_regression
SELECT pg_stat_monitor_reset() | u | contrib_regression
(4 rows)

SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

-- 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.
BEGIN;
INSERT INTO t VALUES (1);
ERROR: duplicate key value violates unique constraint "t_pkey"
DETAIL: Key (a)=(1) already exists.
ROLLBACK;
SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
query | username | datname
--------------------------------+----------+--------------------
BEGIN | u | contrib_regression
INSERT INTO t VALUES (1); | u | contrib_regression
ROLLBACK | |
SELECT pg_stat_monitor_reset() | u | contrib_regression
(4 rows)

SELECT pg_stat_monitor_reset();
pg_stat_monitor_reset
-----------------------

(1 row)

DROP TABLE t;
SET ROLE NONE;
DROP USER u;
DROP EXTENSION pg_stat_monitor;
36 changes: 36 additions & 0 deletions regression/sql/rollback.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
CREATE EXTENSION pg_stat_monitor;

CREATE USER u WITH SUPERUSER;
SET ROLE u;
CREATE TABLE t (a int PRIMARY KEY) ;

SELECT pg_stat_monitor_reset();

BEGIN;
INSERT INTO t VALUES (1);
COMMIT;

SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();

-- During manual rollback we will be able to get username and datname of the query that was rolled back.
BEGIN;
INSERT INTO t VALUES (2);
ROLLBACK;

SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();

-- 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.
BEGIN;
INSERT INTO t VALUES (1);
ROLLBACK;

SELECT query, username, datname FROM pg_stat_monitor ORDER BY query COLLATE "C";
SELECT pg_stat_monitor_reset();

DROP TABLE t;
SET ROLE NONE;
DROP USER u;

DROP EXTENSION pg_stat_monitor;
Loading