Re: Improve pg_stat_statements scalability
Sami Imseih <samimseih@gmail.com>
From: Sami Imseih <samimseih@gmail.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2026-05-20T22:59:23Z
Lists: pgsql-hackers
Attachments
- v2-0004-pg_stat_statements-extend-pg_stat_statements_info.patch (application/octet-stream)
- v2-0001-pgstat-Introduce-pg_stat_report_anytime-for-mid-t.patch (application/octet-stream)
- v2-0003-pg_stat_statements-add-DSA-based-query-text-stora.patch (application/octet-stream)
- v2-0002-pg_stat_statements-modernize-entry-storage-with-p.patch (application/octet-stream)
There was a failure on FreeBSD [1]. The test uses debug_parallel_query=regress which forces parallel plans. What happens is the parallel worker calls pg_stat_statements() (marked PARALLEL SAFE), tries to flush pending stats, but the leader is the one that actually accumulated those stats. I fixed this by: 1. Setting max_parallel_workers_per_gather = 0 in pg_stat_statements.conf, and only enabling it during parallel.sql when we actually want to track a parallel query. 2. Bumping pg_s_s to version 1.14 and marking pg_stat_statements() and pg_stat_statements_reset() as PARALLEL RESTRICTED to ensure these functions only execute in the leader, which is the process that accumulates the pending stats. These could also be marked UNSAFE, but RESTRICTED seems better since it doesn't completely prevent parallel plans if these functions are used with other tables; although it's hard to imagine a real-world case where this would matter. This also means old versions would have this issue with debug_parallel_query, but I don't think we should change function definitions for older versions, in case a user downgrades pg_s_s versions. Maybe others have a different opinion? [1] https://cirrus-ci.com/task/5948422076760064 -- Sami