Skip to content

Commit f035b41

Browse files
committed
Strip commented and empty lines from SQL queries
This avoids "polluting" server logs. A doctest for queries.get() is added along the way. Fix #442.
1 parent 705c2d3 commit f035b41

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
## Unreleased
4+
5+
### Fixed
6+
7+
* Strip comments from SQL queries emitted by pg\_activity (#442).
8+
39
## pg\_activity 3.6.1 - 2025-06-03
410

511
### Fixed

pgactivity/queries/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@
66

77
@functools.cache
88
def get(name: str) -> str:
9+
r"""Return the SQL query contained in named file, ignoring commented lines.
10+
11+
>>> get('get_version')
12+
'SELECT version() AS pg_version;'
13+
>>> print(get('get_pg_activity_post_130000')[:101])
14+
SELECT
15+
a.pid AS pid,
16+
a.backend_xmin AS xmin,
17+
a.application_name AS application_name
18+
"""
919
path = here / f"{name}.sql"
20+
s = "-- "
1021
with path.open() as f:
11-
return f.read()
22+
return "\n".join(
23+
line.rstrip().split(s, 1)[0]
24+
for line in f
25+
if line.strip() and not line.startswith(s)
26+
)

0 commit comments

Comments
 (0)