Skip to content

Commit 638d42a

Browse files
committed
Show GIDs of two-phase commit commands as constants in pg_stat_statements
This relies on the "location" field added to TransactionStmt in 31de7e6, now applied to the "gid" field used by 2PC commands. These commands are now reported like: COMMIT PREPARED $1 PREPARE TRANSACTION $1 ROLLBACK PREPARED $1 Applying constants for these commands is a huge advantage for workloads that rely a lot on 2PC commands with different GIDs. Some tests are added to track the new behavior. Reviewed-by: Julien Rouhaud Discussion: https://postgr.es/m/ZMhT9kNtJJsHw6jK@paquier.xyz
1 parent 5dc456b commit 638d42a

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

contrib/pg_stat_statements/expected/utility.out

+23
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,29 @@ SELECT pg_stat_statements_reset();
197197

198198
(1 row)
199199

200+
-- Two-phase transactions
201+
BEGIN;
202+
PREPARE TRANSACTION 'stat_trans1';
203+
COMMIT PREPARED 'stat_trans1';
204+
BEGIN;
205+
PREPARE TRANSACTION 'stat_trans2';
206+
ROLLBACK PREPARED 'stat_trans2';
207+
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
208+
calls | rows | query
209+
-------+------+-----------------------------------
210+
2 | 0 | BEGIN
211+
1 | 0 | COMMIT PREPARED $1
212+
2 | 0 | PREPARE TRANSACTION $1
213+
1 | 0 | ROLLBACK PREPARED $1
214+
1 | 1 | SELECT pg_stat_statements_reset()
215+
(5 rows)
216+
217+
SELECT pg_stat_statements_reset();
218+
pg_stat_statements_reset
219+
--------------------------
220+
221+
(1 row)
222+
200223
-- Savepoints
201224
BEGIN;
202225
SAVEPOINT sp1;
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
shared_preload_libraries = 'pg_stat_statements'
2+
max_prepared_transactions = 5

contrib/pg_stat_statements/sql/utility.sql

+10
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ COMMIT;
115115
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
116116
SELECT pg_stat_statements_reset();
117117

118+
-- Two-phase transactions
119+
BEGIN;
120+
PREPARE TRANSACTION 'stat_trans1';
121+
COMMIT PREPARED 'stat_trans1';
122+
BEGIN;
123+
PREPARE TRANSACTION 'stat_trans2';
124+
ROLLBACK PREPARED 'stat_trans2';
125+
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
126+
SELECT pg_stat_statements_reset();
127+
118128
-- Savepoints
119129
BEGIN;
120130
SAVEPOINT sp1;

src/backend/parser/gram.y

+3-3
Original file line numberDiff line numberDiff line change
@@ -10924,7 +10924,7 @@ TransactionStmt:
1092410924

1092510925
n->kind = TRANS_STMT_PREPARE;
1092610926
n->gid = $3;
10927-
n->location = -1;
10927+
n->location = @3;
1092810928
$$ = (Node *) n;
1092910929
}
1093010930
| COMMIT PREPARED Sconst
@@ -10933,7 +10933,7 @@ TransactionStmt:
1093310933

1093410934
n->kind = TRANS_STMT_COMMIT_PREPARED;
1093510935
n->gid = $3;
10936-
n->location = -1;
10936+
n->location = @3;
1093710937
$$ = (Node *) n;
1093810938
}
1093910939
| ROLLBACK PREPARED Sconst
@@ -10942,7 +10942,7 @@ TransactionStmt:
1094210942

1094310943
n->kind = TRANS_STMT_ROLLBACK_PREPARED;
1094410944
n->gid = $3;
10945-
n->location = -1;
10945+
n->location = @3;
1094610946
$$ = (Node *) n;
1094710947
}
1094810948
;

src/include/nodes/parsenodes.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -3540,7 +3540,8 @@ typedef struct TransactionStmt
35403540
List *options; /* for BEGIN/START commands */
35413541
/* for savepoint commands */
35423542
char *savepoint_name pg_node_attr(query_jumble_ignore);
3543-
char *gid; /* for two-phase-commit related commands */
3543+
/* for two-phase-commit related commands */
3544+
char *gid pg_node_attr(query_jumble_ignore);
35443545
bool chain; /* AND CHAIN option */
35453546
/* token location, or -1 if unknown */
35463547
int location pg_node_attr(query_jumble_location);

0 commit comments

Comments
 (0)