Re: [PATCH] Add hook for plugins to acquire sample rows during ANALYZE - Mailing list pgsql-hackers

From Ashutosh Bapat
Subject Re: [PATCH] Add hook for plugins to acquire sample rows during ANALYZE
Date
Msg-id CAExHW5tgK7osrZqe=tx+fKW1JY9pMQ6GzeubSV3PADYx6rpmkw@mail.gmail.com
Whole thread
Responses Re: [PATCH] Add hook for plugins to acquire sample rows during ANALYZE
List pgsql-hackers
Hi Samba Siva,

On Fri, Jun 26, 2026 at 7:17 AM Samba Siva Reddy Chinta
<sambasivareddy.ch@zohomail.in> wrote:
>
> Hi all,
>
> Attached is a patch that adds a hook, AcquireSampleRowsFunc_hook, allowing extensions to override the row sampling
functionused during ANALYZE for regular heap relations. 
>
> Motivation
>
> Extensions that implement horizontal scaling of tables currently have no clean way to participate in ANALYZE's row
sampling.The default acquire_sample_rows() only knows how to sample the local heap, so a distributed-table extension
wantingaccurate statistics has to either: 
>
> maintain its own separate stats-collection machinery outside of ANALYZE entirely, or
> duplicate/reimplement parts of analyze.c's sampling logic to pull rows from remote nodes.
>
> This hook lets such an extension plug into the existing ANALYZE code path and supply its own row acquisition
function,without having to reinvent stats collection or duplicate logic that already exists in core. 
>
> What the patch does
>
> Adds AcquireSampleRowsFunc_hook (typed identically to AcquireSampleRowsFunc) in vacuum.h.
> In analyze.c, both analyze_rel() and acquire_inherited_sample_rows() check the hook and use it in place of
acquire_sample_rows()when set. 
> Adds doc text in xfunc.sgml describing the hook's contract (fill rows[] up to targrows, set *totalrows).
> Adds a regression test confirming ANALYZE still completes normally with the hook unset (the hook itself needs a C
extensionto exercise meaningfully, so this just guards against regressions in the unset case). 
>

If you have implemented sharding using inheritance or partitioning and
FDW, the facility to fetch statistics of foreign tables from foreign
server can be useful here. IIUC, there is a limitation on using it for
inherited foreign tables, but it's worth exploring and improving for
your usecase.

--
Best Wishes,
Ashutosh Bapat



pgsql-hackers by date:

Previous
From: Shlok Kyal
Date:
Subject: Re: Support EXCEPT for ALL SEQUENCES publications
Next
From: Robert Haas
Date:
Subject: Re: RFC: Logging plan of the running query