Re: Improve pg_stat_statements scalability

From: Sami Imseih <samimseih(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Improve pg_stat_statements scalability
Date: 2026-05-20 22:59:23
Message-ID: CAA5RZ0vjpyU-RJzUYCkr0-9jqJtbS95cRFnWTiOEENE7iYNgcA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v2-0004-pg_stat_statements-extend-pg_stat_statements_info.patch application/octet-stream 5.0 KB
v2-0001-pgstat-Introduce-pg_stat_report_anytime-for-mid-t.patch application/octet-stream 28.5 KB
v2-0003-pg_stat_statements-add-DSA-based-query-text-stora.patch application/octet-stream 18.4 KB
v2-0002-pg_stat_statements-modernize-entry-storage-with-p.patch application/octet-stream 81.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-05-20 23:06:33 Re: Fix pg_stat_wal_receiver to show CONNECTING status
Previous Message Peter Smith 2026-05-20 22:27:15 Re: Add pg_get_publication_ddl function