Thread

  1. Re: Improve pg_stat_statements scalability

    Sami Imseih <samimseih@gmail.com> — 2026-05-20T22:59:23Z

    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