Skip to content

Commit 17fbedc

Browse files
committed
test: bgworker does queries unnecessarily
Proves the issue on #164, the bgworker is polluting `pg_stat_statements` with lots of queries unnecessarily.
1 parent 0207e64 commit 17fbedc

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

test/init.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
shared_preload_libraries='pg_net'
1+
shared_preload_libraries='pg_net, pg_stat_statements'
22
log_min_messages=INFO

test/test_stat_statements.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
3+
import pytest
4+
from sqlalchemy import text
5+
6+
def test_query_stat_statements(sess):
7+
"""Check that the background worker executes queries despite no new requests arriving"""
8+
9+
(pg_version,) = sess.execute(text(
10+
"""
11+
select current_setting('server_version_num');
12+
"""
13+
)).fetchone()
14+
15+
if int(pg_version) < 140000:
16+
pytest.skip("Skipping fixture on pg version < 14. The query_id column on pg_stat_statements is only available on >= 14")
17+
18+
sess.execute(text(
19+
"""
20+
create extension pg_stat_statements;
21+
"""
22+
))
23+
24+
sess.commit()
25+
26+
time.sleep(1)
27+
28+
(old_calls,) = sess.execute(text(
29+
"""
30+
select
31+
pss.calls
32+
from
33+
pg_stat_activity sa
34+
join
35+
pg_stat_statements pss
36+
on sa.query_id = pss.queryid
37+
where
38+
sa.backend_type ilike '%pg_net%';
39+
"""
40+
)).fetchone()
41+
42+
time.sleep(3)
43+
44+
(new_calls,) = sess.execute(text(
45+
"""
46+
select
47+
pss.calls
48+
from
49+
pg_stat_activity sa
50+
join
51+
pg_stat_statements pss
52+
on sa.query_id = pss.queryid
53+
where
54+
sa.backend_type ilike '%pg_net%';
55+
"""
56+
)).fetchone()
57+
58+
assert new_calls > old_calls

0 commit comments

Comments
 (0)