[PATCH] Add RetrieveInstrumentation hook for CustomScan providers
Thread notes
1 note#feature#moderate#enhancement
hackorum-bot#feature#moderate#enhancement
@global #feature #moderate #enhancement
Dear PostgreSQL Hackers,
This email proposes a patch to enhance the CustomScan provider interface.
The patch file,
0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patch, is
attached.
*Problem:*
CustomScan providers currently lack a standard method to aggregate
instrumentation data from parallel workers back to the leader process
before the Dynamic Shared Memory (DSM) segment is unlinked. This makes it
difficult to gather comprehensive performance metrics from parallelized
custom scans.
*Solution:*
This patch introduces a new optional hook, RetrieveInstrumentationCustomScan,
to the CustomExecMethods struct. This hook allows custom scan providers to
implement logic to collect and consolidate instrumentation from shared
memory or worker states during the parallel query cleanup phase. This hook
is invoked via the new ExecCustomScanRetrieveInstrumentation function,
called from ExecParallelRetrieveInstrumentation for T_CustomScanState nodes.
Since the hook is optional (checked for NULL before calling), it maintains
full backward compatibility.
*Testing & Compatibility:*
- The patch compiles and passes all core regression tests (make
check-world) on my x86_64 instance.
- The changes are not platform-specific.
- Regression Tests: This patch provides a new *capability* for custom
scan providers. Since the hook's functionality is only realized when
implemented by an extension, specific tests would naturally reside within
that extension rather than in the core regression suite.
This patch does not directly address a specific item on the official TODO
list but enhances the extensibility framework.
I believe this patch is complete and ready for review. I look forward to
any feedback and am happy to make revisions. I will also add this patch to
the next CommitFest.
Thank you,
Siddharth Kothari
Hi everyone,
I’m just checking in to see if anyone has had a chance to look at this or
if there’s any further information I should provide to help with the
review. I have also added the patch to PG20-1 CF queue, the link is
https://commitfest.postgresql.org/patch/6524/.
Thanks,
Siddharth
On Wed, Feb 18, 2026 at 3:09 PM Siddharth Kothari <sidkot@google.com> wrote:
Show quoted text
Dear PostgreSQL Hackers,
This email proposes a patch to enhance the CustomScan provider interface.
The patch file,
0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patch, is
attached.*Problem:*
CustomScan providers currently lack a standard method to aggregate
instrumentation data from parallel workers back to the leader process
before the Dynamic Shared Memory (DSM) segment is unlinked. This makes it
difficult to gather comprehensive performance metrics from parallelized
custom scans.*Solution:*
This patch introduces a new optional hook,
RetrieveInstrumentationCustomScan, to the CustomExecMethods struct. This
hook allows custom scan providers to implement logic to collect and
consolidate instrumentation from shared memory or worker states during the
parallel query cleanup phase. This hook is invoked via the new
ExecCustomScanRetrieveInstrumentation function, called from
ExecParallelRetrieveInstrumentation for T_CustomScanState nodes. Since
the hook is optional (checked for NULL before calling), it maintains full
backward compatibility.*Testing & Compatibility:*
- The patch compiles and passes all core regression tests (make
check-world) on my x86_64 instance.
- The changes are not platform-specific.
- Regression Tests: This patch provides a new *capability* for custom
scan providers. Since the hook's functionality is only realized when
implemented by an extension, specific tests would naturally reside within
that extension rather than in the core regression suite.This patch does not directly address a specific item on the official TODO
list but enhances the extensibility framework.I believe this patch is complete and ready for review. I look forward to
any feedback and am happy to make revisions. I will also add this patch to
the next CommitFest.Thank you,
Siddharth Kothari
Hi hackers,
Bumping this thread as this small patch is passing all CF checks but needs
a reviewer.
*The Problem:* Right now, if you write a parallel CustomScan extension, it
is impossible to get performance metrics from parallel workers. The data is
destroyed during DSM unlinking before the leader can grab it.
*The Fix:* This patch adds an optional RetrieveInstrumentation hook to
CustomExecMethods. It has zero overhead for existing extensions (it's a
simple NULL check) but unlocks metric aggregation for parallel custom scans.
Please let me know your thoughts.
Thanks,
Siddharth
On Wed, Apr 8, 2026 at 10:57 AM Siddharth Kothari <sidkot@google.com> wrote:
Show quoted text
Hi everyone,
I’m just checking in to see if anyone has had a chance to look at this or
if there’s any further information I should provide to help with the
review. I have also added the patch to PG20-1 CF queue, the link is
https://commitfest.postgresql.org/patch/6524/.Thanks,
SiddharthOn Wed, Feb 18, 2026 at 3:09 PM Siddharth Kothari <sidkot@google.com>
wrote:Dear PostgreSQL Hackers,
This email proposes a patch to enhance the CustomScan provider interface.
The patch file,
0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patch, is
attached.*Problem:*
CustomScan providers currently lack a standard method to aggregate
instrumentation data from parallel workers back to the leader process
before the Dynamic Shared Memory (DSM) segment is unlinked. This makes it
difficult to gather comprehensive performance metrics from parallelized
custom scans.*Solution:*
This patch introduces a new optional hook,
RetrieveInstrumentationCustomScan, to the CustomExecMethods struct. This
hook allows custom scan providers to implement logic to collect and
consolidate instrumentation from shared memory or worker states during the
parallel query cleanup phase. This hook is invoked via the new
ExecCustomScanRetrieveInstrumentation function, called from
ExecParallelRetrieveInstrumentation for T_CustomScanState nodes. Since
the hook is optional (checked for NULL before calling), it maintains full
backward compatibility.*Testing & Compatibility:*
- The patch compiles and passes all core regression tests (make
check-world) on my x86_64 instance.
- The changes are not platform-specific.
- Regression Tests: This patch provides a new *capability* for custom
scan providers. Since the hook's functionality is only realized when
implemented by an extension, specific tests would naturally reside within
that extension rather than in the core regression suite.This patch does not directly address a specific item on the official TODO
list but enhances the extensibility framework.I believe this patch is complete and ready for review. I look forward to
any feedback and am happy to make revisions. I will also add this patch to
the next CommitFest.Thank you,
Siddharth Kothari
Attachments:
0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patchapplication/octet-stream; name=0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patchDownload+16-1
Hi hackers,
Adding Melanie and Tomas to this thread. I noticed you both recently
committed similar changes to handle shared memory instrumentation
separation and retrieval for IndexScan/IndexOnlyScan (dd78e69cfc
<https://github.com/postgres/postgres/commit/dd78e69cfc337f93cfc0303ddf660262d7f1237e>)
and SeqScan (3b1117d6e2
<https://github.com/postgres/postgres/commit/3b1117d6e2e47d86cdbd978b79434c630cb0ef52>
).
My patch implements the exact same pattern for CustomScan states. It adds
an optional RetrieveInstrumentationCustomScan hook so that extensions can
aggregate their worker metrics before the DSM gets unlinked, bringing
custom scans to parity with the recent core scan instrumentation
improvements.
Could you please help review?
The CommitFest entry is also updated here:
https://commitfest.postgresql.org/patch/6524/
Thanks, Siddharth
On Tue, Jun 23, 2026 at 2:38 PM Siddharth Kothari <sidkot@google.com> wrote:
Show quoted text
Hi hackers,
Bumping this thread as this small patch is passing all CF checks but needs
a reviewer.*The Problem:* Right now, if you write a parallel CustomScan extension,
it is impossible to get performance metrics from parallel workers. The data
is destroyed during DSM unlinking before the leader can grab it.*The Fix:* This patch adds an optional RetrieveInstrumentation hook to
CustomExecMethods. It has zero overhead for existing extensions (it's a
simple NULL check) but unlocks metric aggregation for parallel custom scans.Please let me know your thoughts.
Thanks,
Siddharth
On Wed, Apr 8, 2026 at 10:57 AM Siddharth Kothari <sidkot@google.com>
wrote:Hi everyone,
I’m just checking in to see if anyone has had a chance to look at this or
if there’s any further information I should provide to help with the
review. I have also added the patch to PG20-1 CF queue, the link is
https://commitfest.postgresql.org/patch/6524/.Thanks,
SiddharthOn Wed, Feb 18, 2026 at 3:09 PM Siddharth Kothari <sidkot@google.com>
wrote:Dear PostgreSQL Hackers,
This email proposes a patch to enhance the CustomScan provider
interface. The patch file,
0001-Add-RetrieveInstrumentationCustomScan-hook-for-Custo.patch, is
attached.*Problem:*
CustomScan providers currently lack a standard method to aggregate
instrumentation data from parallel workers back to the leader process
before the Dynamic Shared Memory (DSM) segment is unlinked. This makes it
difficult to gather comprehensive performance metrics from parallelized
custom scans.*Solution:*
This patch introduces a new optional hook,
RetrieveInstrumentationCustomScan, to the CustomExecMethods struct.
This hook allows custom scan providers to implement logic to collect and
consolidate instrumentation from shared memory or worker states during the
parallel query cleanup phase. This hook is invoked via the new
ExecCustomScanRetrieveInstrumentation function, called from
ExecParallelRetrieveInstrumentation for T_CustomScanState nodes. Since
the hook is optional (checked for NULL before calling), it maintains full
backward compatibility.*Testing & Compatibility:*
- The patch compiles and passes all core regression tests (make
check-world) on my x86_64 instance.
- The changes are not platform-specific.
- Regression Tests: This patch provides a new *capability* for
custom scan providers. Since the hook's functionality is only realized when
implemented by an extension, specific tests would naturally reside within
that extension rather than in the core regression suite.This patch does not directly address a specific item on the official
TODO list but enhances the extensibility framework.I believe this patch is complete and ready for review. I look forward to
any feedback and am happy to make revisions. I will also add this patch to
the next CommitFest.Thank you,
Siddharth Kothari
Attachments:
v2-0001-Add-RetrieveInstrumentationCustomScan-hook-for-Cu.patchapplication/x-patch; name=v2-0001-Add-RetrieveInstrumentationCustomScan-hook-for-Cu.patchDownload+16-1
Hi Siddharth,
On 7/8/26 18:52, Siddharth Kothari wrote:
Hi hackers,
Adding Melanie and Tomas to this thread. I noticed you both recently
committed similar changes to handle shared memory instrumentation
separation and retrieval for IndexScan/IndexOnlyScan (dd78e69cfc
<https://github.com/postgres/postgres/commit/
dd78e69cfc337f93cfc0303ddf660262d7f1237e>) and SeqScan (3b1117d6e2
<https://github.com/postgres/postgres/
commit/3b1117d6e2e47d86cdbd978b79434c630cb0ef52>).My patch implements the exact same pattern for CustomScan states. It
adds an optional RetrieveInstrumentationCustomScan hook so that
extensions can aggregate their worker metrics before the DSM gets
unlinked, bringing custom scans to parity with the recent core scan
instrumentation improvements.Could you please help review?
The CommitFest entry is also updated here: https://
commitfest.postgresql.org/patch/6524/ <https://
commitfest.postgresql.org/patch/6524/>
Thanks for the patch, and sorry for not responding earlier. The last
couple months were incredibly busy, both because of the last commitfest
and personal reasons.
I think the patch looks generally OK, except for two things:
1) It needs to add the new callback to doc/src/sgml/custom-scan.sgml,
with similar documentation as for the other callbacks.
2) While looking at execParallel.c it occurred to me the CustomScan may
have the same issue as described in [1]/messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.me (and a couple messages after
that). The scan may in the parallel part of a plan, but not necessarily
parallel-aware. But a lot of the initialization is gated by
if (planstate->plan->parallel_aware)
...
All the other scans initialize some of the instrumentation always, in
blocks like this:
/* even when not parallel-aware, for EXPLAIN ANALYZE */
And BHS had this issue until 9c18b47e610, but we didn't do the same
thing for CustomScan. I guess we should.
Of course, it's not the fault of this patch, it's just something I
noticed while looking at the code. OTOH it probably also makes sense for
CustomScan to handle the instrumentation just like the other scans, i.e.
by allocating a separate DSM for the instrumentation. AFAICS that'll
require a couple more optional callbacks in CustomExecMethods:
ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorker
But I think that's OK.
[1]: /messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.me
/messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.me
regards
--
Tomas Vondra
Hi Tomas,
Thanks for the review, and no worries.
I completely agree with both of your suggestions.
I added the new missing callbacks:
ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorker
along with the existing
ExecCustomScanRetrieveInstrumentation
and extracted the custom scan instrumentation tracking out of the
parallel_aware guard block in execParallel.c. The core execution
orchestrator will now correctly invoke these instrumentation callbacks
even when a CustomScan node is part of a parallel chunk but not
strictly parallel_aware itself.
I also updated the Custom scan documentation to properly document the
new callbacks.
I've attached the v3 patch with these changes included. Let me know if
everything looks good or if there is anything else I should adjust.
Regards,
Siddharth
Show quoted text
On Thu, Jul 9, 2026 at 12:10 AM Tomas Vondra <tomas@vondra.me> wrote:
Hi Siddharth,
On 7/8/26 18:52, Siddharth Kothari wrote:
Hi hackers,
Adding Melanie and Tomas to this thread. I noticed you both recently
committed similar changes to handle shared memory instrumentation
separation and retrieval for IndexScan/IndexOnlyScan (dd78e69cfc
<https://github.com/postgres/postgres/commit/
dd78e69cfc337f93cfc0303ddf660262d7f1237e>) and SeqScan (3b1117d6e2
<https://github.com/postgres/postgres/
commit/3b1117d6e2e47d86cdbd978b79434c630cb0ef52>).My patch implements the exact same pattern for CustomScan states. It
adds an optional RetrieveInstrumentationCustomScan hook so that
extensions can aggregate their worker metrics before the DSM gets
unlinked, bringing custom scans to parity with the recent core scan
instrumentation improvements.Could you please help review?
The CommitFest entry is also updated here: https://
commitfest.postgresql.org/patch/6524/ <https://
commitfest.postgresql.org/patch/6524/>Thanks for the patch, and sorry for not responding earlier. The last
couple months were incredibly busy, both because of the last commitfest
and personal reasons.I think the patch looks generally OK, except for two things:
1) It needs to add the new callback to doc/src/sgml/custom-scan.sgml,
with similar documentation as for the other callbacks.2) While looking at execParallel.c it occurred to me the CustomScan may
have the same issue as described in [1] (and a couple messages after
that). The scan may in the parallel part of a plan, but not necessarily
parallel-aware. But a lot of the initialization is gated byif (planstate->plan->parallel_aware)
...All the other scans initialize some of the instrumentation always, in
blocks like this:/* even when not parallel-aware, for EXPLAIN ANALYZE */
And BHS had this issue until 9c18b47e610, but we didn't do the same
thing for CustomScan. I guess we should.Of course, it's not the fault of this patch, it's just something I
noticed while looking at the code. OTOH it probably also makes sense for
CustomScan to handle the instrumentation just like the other scans, i.e.
by allocating a separate DSM for the instrumentation. AFAICS that'll
require a couple more optional callbacks in CustomExecMethods:ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorkerBut I think that's OK.
[1]
/messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.meregards
--
Tomas Vondra
Hi Tomas,
Just sending a gentle ping on this thread. Please let me know if you
get a chance to take a look at the v3 patch, or if there is anything
else you'd like me to address!
Thanks,
Siddharth
Show quoted text
On Thu, Jul 9, 2026 at 1:37 PM Siddharth Kothari <sidkot@google.com> wrote:
Hi Tomas,
Thanks for the review, and no worries.
I completely agree with both of your suggestions.I added the new missing callbacks:
ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorker
along with the existing
ExecCustomScanRetrieveInstrumentationand extracted the custom scan instrumentation tracking out of the
parallel_aware guard block in execParallel.c. The core execution
orchestrator will now correctly invoke these instrumentation callbacks
even when a CustomScan node is part of a parallel chunk but not
strictly parallel_aware itself.I also updated the Custom scan documentation to properly document the
new callbacks.I've attached the v3 patch with these changes included. Let me know if
everything looks good or if there is anything else I should adjust.Regards,
SiddharthOn Thu, Jul 9, 2026 at 12:10 AM Tomas Vondra <tomas@vondra.me> wrote:
Hi Siddharth,
On 7/8/26 18:52, Siddharth Kothari wrote:
Hi hackers,
Adding Melanie and Tomas to this thread. I noticed you both recently
committed similar changes to handle shared memory instrumentation
separation and retrieval for IndexScan/IndexOnlyScan (dd78e69cfc
<https://github.com/postgres/postgres/commit/
dd78e69cfc337f93cfc0303ddf660262d7f1237e>) and SeqScan (3b1117d6e2
<https://github.com/postgres/postgres/
commit/3b1117d6e2e47d86cdbd978b79434c630cb0ef52>).My patch implements the exact same pattern for CustomScan states. It
adds an optional RetrieveInstrumentationCustomScan hook so that
extensions can aggregate their worker metrics before the DSM gets
unlinked, bringing custom scans to parity with the recent core scan
instrumentation improvements.Could you please help review?
The CommitFest entry is also updated here: https://
commitfest.postgresql.org/patch/6524/ <https://
commitfest.postgresql.org/patch/6524/>Thanks for the patch, and sorry for not responding earlier. The last
couple months were incredibly busy, both because of the last commitfest
and personal reasons.I think the patch looks generally OK, except for two things:
1) It needs to add the new callback to doc/src/sgml/custom-scan.sgml,
with similar documentation as for the other callbacks.2) While looking at execParallel.c it occurred to me the CustomScan may
have the same issue as described in [1] (and a couple messages after
that). The scan may in the parallel part of a plan, but not necessarily
parallel-aware. But a lot of the initialization is gated byif (planstate->plan->parallel_aware)
...All the other scans initialize some of the instrumentation always, in
blocks like this:/* even when not parallel-aware, for EXPLAIN ANALYZE */
And BHS had this issue until 9c18b47e610, but we didn't do the same
thing for CustomScan. I guess we should.Of course, it's not the fault of this patch, it's just something I
noticed while looking at the code. OTOH it probably also makes sense for
CustomScan to handle the instrumentation just like the other scans, i.e.
by allocating a separate DSM for the instrumentation. AFAICS that'll
require a couple more optional callbacks in CustomExecMethods:ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorkerBut I think that's OK.
[1]
/messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.meregards
--
Tomas Vondra
Hi folks,
Gentle nudge. Please let me know if you
get a chance to take a look at the v3 patch, or if there is anything
else you'd like me to address!
Thanks,
Siddharth
Show quoted text
On Thu, Jul 16, 2026 at 11:33 AM Siddharth Kothari <sidkot@google.com> wrote:
Hi Tomas,
Just sending a gentle ping on this thread. Please let me know if you
get a chance to take a look at the v3 patch, or if there is anything
else you'd like me to address!Thanks,
SiddharthOn Thu, Jul 9, 2026 at 1:37 PM Siddharth Kothari <sidkot@google.com> wrote:
Hi Tomas,
Thanks for the review, and no worries.
I completely agree with both of your suggestions.I added the new missing callbacks:
ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorker
along with the existing
ExecCustomScanRetrieveInstrumentationand extracted the custom scan instrumentation tracking out of the
parallel_aware guard block in execParallel.c. The core execution
orchestrator will now correctly invoke these instrumentation callbacks
even when a CustomScan node is part of a parallel chunk but not
strictly parallel_aware itself.I also updated the Custom scan documentation to properly document the
new callbacks.I've attached the v3 patch with these changes included. Let me know if
everything looks good or if there is anything else I should adjust.Regards,
SiddharthOn Thu, Jul 9, 2026 at 12:10 AM Tomas Vondra <tomas@vondra.me> wrote:
Hi Siddharth,
On 7/8/26 18:52, Siddharth Kothari wrote:
Hi hackers,
Adding Melanie and Tomas to this thread. I noticed you both recently
committed similar changes to handle shared memory instrumentation
separation and retrieval for IndexScan/IndexOnlyScan (dd78e69cfc
<https://github.com/postgres/postgres/commit/
dd78e69cfc337f93cfc0303ddf660262d7f1237e>) and SeqScan (3b1117d6e2
<https://github.com/postgres/postgres/
commit/3b1117d6e2e47d86cdbd978b79434c630cb0ef52>).My patch implements the exact same pattern for CustomScan states. It
adds an optional RetrieveInstrumentationCustomScan hook so that
extensions can aggregate their worker metrics before the DSM gets
unlinked, bringing custom scans to parity with the recent core scan
instrumentation improvements.Could you please help review?
The CommitFest entry is also updated here: https://
commitfest.postgresql.org/patch/6524/ <https://
commitfest.postgresql.org/patch/6524/>Thanks for the patch, and sorry for not responding earlier. The last
couple months were incredibly busy, both because of the last commitfest
and personal reasons.I think the patch looks generally OK, except for two things:
1) It needs to add the new callback to doc/src/sgml/custom-scan.sgml,
with similar documentation as for the other callbacks.2) While looking at execParallel.c it occurred to me the CustomScan may
have the same issue as described in [1] (and a couple messages after
that). The scan may in the parallel part of a plan, but not necessarily
parallel-aware. But a lot of the initialization is gated byif (planstate->plan->parallel_aware)
...All the other scans initialize some of the instrumentation always, in
blocks like this:/* even when not parallel-aware, for EXPLAIN ANALYZE */
And BHS had this issue until 9c18b47e610, but we didn't do the same
thing for CustomScan. I guess we should.Of course, it's not the fault of this patch, it's just something I
noticed while looking at the code. OTOH it probably also makes sense for
CustomScan to handle the instrumentation just like the other scans, i.e.
by allocating a separate DSM for the instrumentation. AFAICS that'll
require a couple more optional callbacks in CustomExecMethods:ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorkerBut I think that's OK.
[1]
/messages/by-id/3bdbc70d-ad44-494a-8aab-868b5066fe8b@vondra.meregards
--
Tomas Vondra
On Thu, Jul 9, 2026 at 1:37 PM Siddharth Kothari <sidkot@google.com> wrote:
Hi Tomas,
Thanks for the review, and no worries.
I completely agree with both of your suggestions.I added the new missing callbacks:
ExecCustomScanInstrumentEstimate
ExecCustomScanInstrumentInitDSM
ExecCustomScanInstrumentInitWorker
along with the existing
ExecCustomScanRetrieveInstrumentationand extracted the custom scan instrumentation tracking out of the
parallel_aware guard block in execParallel.c. The core execution
orchestrator will now correctly invoke these instrumentation callbacks
even when a CustomScan node is part of a parallel chunk but not
strictly parallel_aware itself.I also updated the Custom scan documentation to properly document the
new callbacks.I've attached the v3 patch with these changes included. Let me know if
everything looks good or if there is anything else I should adjust.
I think the idea looks fine, and the patch looks good to me.
One suggestion, not on the patch itself, but while responding on
pgsql-hackers: avoid top-posting your replies. You can check out the
community etiquette guidelines here:
https://wiki.postgresql.org/wiki/Mailing_Lists
--
Regards,
Dilip Kumar
Google
Hello Siddharth,
On 7/16/26 08:03, Siddharth Kothari wrote:
Hi Tomas,
Just sending a gentle ping on this thread. Please let me know if you
get a chance to take a look at the v3 patch, or if there is anything
else you'd like me to address!Thanks,
Siddharth
Sorry for not responding earlier. The here's usually reduced review
bandwidth over the summer, with people focusing on stabilization of the
new release. This year there's also an influx of AI bug reports, so
people are busy with addressing those.
I expect to take a closer look at the new patch version early September.
regards
--
Tomas Vondra
I think the idea looks fine, and the patch looks good to me.
One suggestion, not on the patch itself, but while responding on
pgsql-hackers: avoid top-posting your replies. You can check out the
community etiquette guidelines here:
https://wiki.postgresql.org/wiki/Mailing_Lists
Thanks for taking a look Dilip! Noted.
On Mon, Aug 17, 2026 at 6:06 PM Tomas Vondra <tomas@vondra.me> wrote:
Sorry for not responding earlier. The here's usually reduced review
bandwidth over the summer, with people focusing on stabilization of the
new release. This year there's also an influx of AI bug reports, so
people are busy with addressing those.I expect to take a closer look at the new patch version early September.
Thanks Tomas. No worries, appreciate your help!
Regards,
Siddharth
Hey Tomas!
Gentle nudge on the patch. I have rebased the changes and attached the
new v4 patch. There are no actual changes from the previous patch; I
have just rebased the changes on top of the latest head.
Thanks,
Siddharth
Hi Siddharth,
Sorry for the long silence. Too much other work :-(
I did a review of the v4 patch today. I haven't found any outright
correctness bug - it modifies the right places, has the right overall
structure, etc. But I think there's a couple loose ends / gaps that need
to be addressed to make this committable.
1) I don't think the new ExecCustomScan callbacks can be this simple.
It's probably right to model this after the parallel query callbacks, so
consider this example:
void
ExecCustomScanInitializeDSM(CustomScanState *node, ...)
{
const CustomExecMethods *methods = node->methods;
if (methods->InitializeDSMCustomScan)
{
int plan_node_id = node->ss.ps.plan->plan_node_id;
void *coordinate;
coordinate = shm_toc_allocate(pcxt->toc, node->pscan_len);
methods->InitializeDSMCustomScan(node, pcxt, coordinate);
shm_toc_insert(pcxt->toc, plan_node_id, coordinate);
}
}
Compare this to the new instrumentation callback doing the same thing:
void
ExecCustomScanInstrumentInitDSM(CustomScanState *node, ...)
{
const CustomExecMethods *methods = node->methods;
if (methods->InitializeDSMCustomScanInstrumentation)
methods->InitializeDSMCustomScanInstrumentation(node, pcxt);
}
Clearly, it does much less. It's up to the custom scan implementation to
do all the shmem setup (shm_toc_alloc, and similarly for the other
callbacks). But why should it be done there? That code is not in a good
position to do that. For example, what key should it use for the new
DSM? It does not really know which keys are already used, e.g. it can't
use the "natural" plan_node_id as that's already used.
A duplicate key likely triggers a crash or some sort of weird errors.
I suggest to make this more like the parallel query callbacks, so that
the core "owns" the allocation, and the CustomScan merely estimates it,
initializes it (after allocation, etc.).
2) AFAIK the callbacks are called even when instrumentation was not
requested, which seems a bit weird. Consider what the existing scan
nodes (like nodeSeqScan.c) do:
if ((estate->es_instrument & INSTRUMENT_IO) == 0)
return;
I suppose the CustomScan should do something similar (possibly in a
slightly different way than SeqScan).
3) I'm not sure if the documentation should have more details about what
the callbacks should do (e.g. that "Retrieve" runs at the end of a
query, not on every rescan, that it needs to copy data out, etc.).
4) There need to be some tests. We actually already have CustomScan
tests in src/test/modules/test_extensible - seems natural to just expand
those to also implement and test the new callbacks. It should also print
the collected instrumentation data in EXPLAIN. This will probably
provide some ideas what needs to be mentioned in the docs.
5) The tests may also reveal that some bits are missing, or are not
possible to implement. For example, the EXPLAIN probably won't be able
to print per-worker stats because ExplainOpenWorker/ExplainCloseWorker
are static in explain.c. I'm not sure if we should export them, or if we
need a new callback in the CustomScan (next to the one we already have
for regular EXPLAIN).
6) I realized the same issue (lack of instrumentation) applies to
ForeignScan too. That's a separate patch, but I wonder if doing them
both would be helpful - it seems like a fairly similar.
7) The naming of the new callbacks in CustomExecMethods seems a bit
inconsistent. Most other callbacks end with "CustomScan" but I realize
that conflicts with the parallel query callbacks, so you added the
"Instrumentation" at the end. But then RetrieveInstrumentationCustomScan
breaks that rule. I guess RetrieveCustomScanInstrumentation would be
more consistent? Or maybe stick with the "CustomScan at the end"
pattern, and put the "Instrumentation" before it in all the callbacks?
That's all I have for now. Thanks for the patch, sorry about the delay.
regards
--
Tomas Vondra
Hi Tomas,
Thanks for the detailed review, and no worries about the delay.
1) I don't think the new ExecCustomScan callbacks can be this simple.
... I suggest to make this more like the parallel query callbacks, so
that the core "owns" the allocation, and the CustomScan merely
estimates it, initializes it (after allocation, etc.).
Makes sense. Updated it accordingly.The wrappers in nodeCustom.c now
mirror ExecCustomScanInitializeDS.
The estimate callback returns a Size, and the executor does
shm_toc_estimate_chunk/keys, shm_toc_allocate,
shm_toc_insert and shm_toc_lookup itself, passing the chunk to the
provider as "coordinate". The size is carried between the two phases in
a new CustomScanState.pinstrument_len, like pscan_len.
For the key I added PARALLEL_KEY_NODE_INSTRUMENTATION(plan_node_id) in
execParallel.h, defined as 0xE100000000000000 + plan_node_id. That keeps
it clear of plan_node_id (used for the parallel-aware chunk) and of the
executor's fixed 0xE0.. keys, and the same macro serves ForeignScan.
It lives in the header rather than in execParallel.c only because
nodeCustom.c and nodeForeignscan.c both need it; happy to move or
rename it if you prefer.
2) AFAIK the callbacks are called even when instrumentation was not
requested, which seems a bit weird.
Done. The wrappers now return early unless the node has instrumentation
and there are workers, i.e. "if (!node->ss.ps.instrument ||
pcxt->nworkers == 0)", which is what nodeSort.c, nodeAgg.c and
nodeMemoize.c do for their shared instrumentation. I went with that
rather than SeqScan's INSTRUMENT_IO test since the executor cannot know
which kind of stats a given provider collects.
3) I'm not sure if the documentation should have more details about
what the callbacks should do
Updated the documentation.
4) There need to be some tests.
Added tests.
5) ... the EXPLAIN probably won't be able to print per-worker stats
because ExplainOpenWorker/ExplainCloseWorker are static in explain.c.
Yep, the test needed them. I exported the two functions
(declared in explain.h since that is where they are defined) rather
than adding another callback; the provider's ExplainCustomScan then
looks exactly like show_sort_info. Output of existing providers that do
not use any of this is unchanged.
6) I realized the same issue (lack of instrumentation) applies to
ForeignScan too.
Took the liberty of adding a patch for ForeignScan as well (0002). It
is the same change
for FdwRoutine, sharing the key macro and the gating.
I started on a test for it and realized it needs a bit more thought.
There is no test FDW that is parallel-safe. file_fdw is, so a test
would mean implementing the callbacks there and printing per-worker
counts in its EXPLAIN output, which is a small user-visible feature for
file_fdw rather than just a test. The alternative is a dummy
parallel-safe FDW under src/test/modules - which would need a non
trivial amount of scaffolding. I'm happy to do either as a
follow-up once we agree which one; in the meantime the executor side is
identical to the CustomScan paths that test_extensible covers.
7) The naming of the new callbacks in CustomExecMethods seems a bit
inconsistent.
Done. All four now end in "CustomScan":
EstimateDSMInstrumentationCustomScan,
InitializeDSMInstrumentationCustomScan,
InitializeWorkerInstrumentationCustomScan,
RetrieveInstrumentationCustomScan.
Have attached two v5 patches, one for CustomScan and another for ForeignScan
that pass make check and the test_extensible suite.
Thanks,
Siddharth
Attachments:
t53369_14v5-0001-Add-parallel-instrumentation-hooks-for-CustomScan.patchapplication/x-patch; name=v5-0001-Add-parallel-instrumentation-hooks-for-CustomScan.patchDownload+365-11
v5-0002-Add-parallel-instrumentation-hooks-for-ForeignSca.patchapplication/x-patch; name=v5-0002-Add-parallel-instrumentation-hooks-for-ForeignSca.patchDownload+158-5
On 11/13/26 15:58, Siddharth Kothari wrote:
Hi Tomas,
Thanks for the detailed review, and no worries about the delay.1) I don't think the new ExecCustomScan callbacks can be this simple.
... I suggest to make this more like the parallel query callbacks, so
that the core "owns" the allocation, and the CustomScan merely
estimates it, initializes it (after allocation, etc.).Makes sense. Updated it accordingly.The wrappers in nodeCustom.c now
mirror ExecCustomScanInitializeDS.
The estimate callback returns a Size, and the executor does
shm_toc_estimate_chunk/keys, shm_toc_allocate,
shm_toc_insert and shm_toc_lookup itself, passing the chunk to the
provider as "coordinate". The size is carried between the two phases in
a new CustomScanState.pinstrument_len, like pscan_len.
I didn't do a proper review, but seems OK.
For the key I added PARALLEL_KEY_NODE_INSTRUMENTATION(plan_node_id) in
execParallel.h, defined as 0xE100000000000000 + plan_node_id. That keeps
it clear of plan_node_id (used for the parallel-aware chunk) and of the
executor's fixed 0xE0.. keys, and the same macro serves ForeignScan.
It lives in the header rather than in execParallel.c only because
nodeCustom.c and nodeForeignscan.c both need it; happy to move or
rename it if you prefer.
Why not to simply copy what the other scan nodes do?
shm_toc_insert(pcxt->toc,
node->ss.ps.plan->plan_node_id +
PARALLEL_KEY_SCAN_INSTRUMENT_OFFSET,
node->sinstrument);
2) AFAIK the callbacks are called even when instrumentation was not
requested, which seems a bit weird.Done. The wrappers now return early unless the node has instrumentation
and there are workers, i.e. "if (!node->ss.ps.instrument ||
pcxt->nworkers == 0)", which is what nodeSort.c, nodeAgg.c and
nodeMemoize.c do for their shared instrumentation. I went with that
rather than SeqScan's INSTRUMENT_IO test since the executor cannot know
which kind of stats a given provider collects.
Yeah. I'd probably keep the checks separate, to make it look more like
the other scans. Something like
if (!node->ss.ps.instrument || pcxt->nworkers == 0 ||
node->pinstrument_len == 0)
return;
if (!methods->InitializeDSMInstrumentationCustomScan)
return;
instead of one fused check. But that's a detail. BTW, why do we need the
pinstrument_len check?
3) I'm not sure if the documentation should have more details about
what the callbacks should doUpdated the documentation.
Haven't looked yet.
4) There need to be some tests.
Added tests.
Haven't looked yet.
5) ... the EXPLAIN probably won't be able to print per-worker stats
because ExplainOpenWorker/ExplainCloseWorker are static in explain.c.Yep, the test needed them. I exported the two functions
(declared in explain.h since that is where they are defined) rather
than adding another callback; the provider's ExplainCustomScan then
looks exactly like show_sort_info. Output of existing providers that do
not use any of this is unchanged.
OK. I wonder if exporting the functions is the right solution. Maybe we
should instead provide explain the information in a structured way, so
that explain.c can do the rest. But I guess explain.c does not know what
the statistics are.
6) I realized the same issue (lack of instrumentation) applies to
ForeignScan too.Took the liberty of adding a patch for ForeignScan as well (0002). It
is the same change
for FdwRoutine, sharing the key macro and the gating.I started on a test for it and realized it needs a bit more thought.
There is no test FDW that is parallel-safe. file_fdw is, so a test
would mean implementing the callbacks there and printing per-worker
counts in its EXPLAIN output, which is a small user-visible feature for
file_fdw rather than just a test. The alternative is a dummy
parallel-safe FDW under src/test/modules - which would need a non
trivial amount of scaffolding. I'm happy to do either as a
follow-up once we agree which one; in the meantime the executor side is
identical to the CustomScan paths that test_extensible covers.
OK. I was not really expecting / suggesting you do the work, but I won't
complain. Haven't looked yet.
7) The naming of the new callbacks in CustomExecMethods seems a bit
inconsistent.Done. All four now end in "CustomScan":
EstimateDSMInstrumentationCustomScan,
InitializeDSMInstrumentationCustomScan,
InitializeWorkerInstrumentationCustomScan,
RetrieveInstrumentationCustomScan.
OK. The naming is somewhat "unnatural", but I don't have a better
option, and the naming convention was mostly set by the existing functions.
Have attached two v5 patches, one for CustomScan and another for ForeignScan
that pass make check and the test_extensible suite.
Thank. I'll try to take a closer look sometime next week.
regards
--
Tomas Vondra