Tighten asserts on ParallelWorkerNumber a little bit

Started by Tomas Vondra9 months ago3 messageshackers
Jump to latest
#1Tomas Vondra
tomas@vondra.me

Hi,

While hacking on something I happened to notice a couple asserts on
ParallelWorkerNumber when collecting shared instrumentation:

Assert(ParallelWorkerNumber <= node->shared_info->num_workers);

This is not quire right, because num_workers is used to size arrays
indexed by ParallelWorkerNumber. And the comment in parallel.c also
claims (ParallelWorkerNumber < num_workers).

So AFAICS the assert(s) should be

Assert(ParallelWorkerNumber < node->shared_info->num_workers);

I don't think we had issues with this not catching a bug. But it may be
a bit misleading, so worth fixing and (probably) backpatching.

regards

--
Tomas Vondra

Attachments:

0001-Tighten-asserts-on-ParallelWorkerNumber.patchtext/x-patch; charset=UTF-8; name=0001-Tighten-asserts-on-ParallelWorkerNumber.patchDownload+8-9
#2Bertrand Drouvot
bertranddrouvot.pg@gmail.com
In reply to: Tomas Vondra (#1)
Re: Tighten asserts on ParallelWorkerNumber a little bit

Hi,

On Thu, Mar 12, 2026 at 07:59:43PM +0100, Tomas Vondra wrote:

Hi,

While hacking on something I happened to notice a couple asserts on
ParallelWorkerNumber when collecting shared instrumentation:

Assert(ParallelWorkerNumber <= node->shared_info->num_workers);

This is not quire right, because num_workers is used to size arrays
indexed by ParallelWorkerNumber. And the comment in parallel.c also
claims (ParallelWorkerNumber < num_workers).

So AFAICS the assert(s) should be

Assert(ParallelWorkerNumber < node->shared_info->num_workers);

I think that you're right. It looks like the first one has been introduced
by bf11e7ee2e3 and then the others are probably copy/paste.

I don't think we had issues with this not catching a bug. But it may be
a bit misleading, so worth fixing and (probably) backpatching.

I do agree.

With that patch applied, the remaining one:

$ git grep "<=.*num_workers"
src/backend/executor/nodeGatherMerge.c: Assert(nreaders <= castNode(GatherMerge, gm_state->ps.plan)->num_workers);

does not need to be fixed, so I think that your patch does not miss any and
LGTM.

Regards,

--
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com

#3Tomas Vondra
tomas@vondra.me
In reply to: Bertrand Drouvot (#2)
Re: Tighten asserts on ParallelWorkerNumber a little bit

On 3/13/26 11:53, Bertrand Drouvot wrote:

...

So AFAICS the assert(s) should be

Assert(ParallelWorkerNumber < node->shared_info->num_workers);

I think that you're right. It looks like the first one has been introduced
by bf11e7ee2e3 and then the others are probably copy/paste.

I don't think we had issues with this not catching a bug. But it may be
a bit misleading, so worth fixing and (probably) backpatching.

I do agree.

With that patch applied, the remaining one:

$ git grep "<=.*num_workers"
src/backend/executor/nodeGatherMerge.c: Assert(nreaders <= castNode(GatherMerge, gm_state->ps.plan)->num_workers);

does not need to be fixed, so I think that your patch does not miss any and
LGTM.

Thanks. Pushed and backpatched.

--
Tomas Vondra