[PATCH] Add hook for plugins to acquire sample rows during ANALYZE

From: Samba Siva Reddy Chinta <sambasivareddy(dot)ch(at)zohomail(dot)in>
To: "pgsql-hackers" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] Add hook for plugins to acquire sample rows during ANALYZE
Date: 2026-06-26 01:46:54
Message-ID: 19f019bc9d0.45f9a6aa271083.5147864197130401189@zohomail.in
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all,

Attached is a patch that adds a hook, AcquireSampleRowsFunc_hook, allowing extensions to override the row sampling function used 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 wanting accurate 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 extension to exercise meaningfully, so this just guards against regressions in the unset case).

​Regards,

Samba Siva Reddy

http://sambasivareddy.in

Attachment Content-Type Size
v1-acquire-sample-rows-hook.patch application/octet-stream 7.2 KB

Browse pgsql-hackers by date

  From Date Subject
Previous Message Bharath Rupireddy 2026-06-26 01:39:00 Re: Handle concurrent drop when doing whole database vacuum