PostgreSQL中失败查询上的度量XID使用
我正在调查Postgres数据库上常见的执行查询,以帮助减少XID的使用。我可以使用PostgreSQL中失败查询上的度量XID使用,postgresql,pg-stat-statements,Postgresql,Pg Stat Statements,我正在调查Postgres数据库上常见的执行查询,以帮助减少XID的使用。我可以使用pg_stat_语句获取执行的查询列表和调用次数,但是它不包括因唯一约束冲突等原因而失败的查询。有没有办法记录并统计这些失败的查询 例如: test_xid=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | not null Ind
pg_stat_语句
获取执行的查询列表和调用次数,但是它不包括因唯一约束冲突等原因而失败的查询。有没有办法记录并统计这些失败的查询
例如:
test_xid=# \d test
Table "public.test"
Column | Type | Modifiers
--------+---------+-----------
id | integer | not null
Indexes:
"test_pkey" PRIMARY KEY, btree (id)
test_xid=# truncate test;
TRUNCATE TABLE
test_xid=# select pg_stat_statements_reset();
pg_stat_statements_reset
--------------------------
(1 row)
test_xid=# select txid_current();
txid_current
--------------
224547
(1 row)
test_xid=# insert into test(id) values (1);
INSERT 0 1
test_xid=# insert into test(id) values (1);
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.
test_xid=# insert into test(id) values (1);
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.
test_xid=# insert into test(id) values (1);
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.
test_xid=# select txid_current();
txid_current
--------------
224552
(1 row)
test_xid=# select query, calls from pg_stat_statements;
query | calls
------------------------------------+-------
insert into test(id) values (?); | 1
select pg_stat_statements_reset(); | 1
select txid_current(); | 2
(3 rows)
test_xid=# select pg_stat_statements_reset();
pg_stat_statements_reset
--------------------------
(1 row)
test_xid=# insert into test(id) values (1);
ERROR: duplicate key value violates unique constraint "test_pkey"
DETAIL: Key (id)=(1) already exists.
test_xid=# select query, calls from pg_stat_statements;
query | calls
------------------------------------+-------
select pg_stat_statements_reset(); | 1
(1 row)
可以看出,如果INSERT查询总是失败,并且如果查询在成功执行后已经存在,则INSERT查询不会出现在
pg_stations
中,即使失败的查询导致当前XID增加,调用计数也不会因后续的失败查询而增加。对于常规统计,您可以查看pg_stat_database.xact_rollback。如果您想了解回滚的语句,我能想到的唯一不涉及C代码的事情就是记录所有语句,然后查看日志
如果您想深入研究C代码(或付钱给某人),我认为向pg_stat_语句添加回滚支持并不十分困难,我怀疑社区会对此表示欢迎。有关一般统计信息,您可以查看pg_stat_database.xact_rollback。如果您想了解回滚的语句,我能想到的唯一不涉及C代码的事情就是记录所有语句,然后查看日志 如果您想深入研究C代码(或付钱给某人),我认为向pg_stat_语句添加回滚支持并不十分困难,我怀疑社区会对此表示欢迎