Thread: Unclear code - please elaborate

Unclear code - please elaborate

From
Dmitry Nikitin
Date:
Hello ,


https://github.com/postgres/postgres/blob/5d39becf8ba0080c98fee4b63575552f6800b012/src/backend/optimizer/prep/prepjointree.c#L3856
bms_next_member() is allowed to return the zero as a valid value. Subsequent rt_fetch() offsets that
zero to -1 which leads to the assertion down the code. Nothing wrong here? Either zero is simply not
possible after that bms_next_member() because of some factors behind the code?


-- 
Best regards,
 Dmitry                          mailto:pgsql-hackers@dima.nikitin.name




Re: Unclear code - please elaborate

From
Junwang Zhao
Date:
On Sat, Nov 30, 2024 at 4:15 PM Dmitry Nikitin
<pgsql-hackers@dima.nikitin.name> wrote:
>
> Hello ,
>
>
https://github.com/postgres/postgres/blob/5d39becf8ba0080c98fee4b63575552f6800b012/src/backend/optimizer/prep/prepjointree.c#L3856
> bms_next_member() is allowed to return the zero as a valid value. Subsequent rt_fetch() offsets that
> zero to -1 which leads to the assertion down the code. Nothing wrong here? Either zero is simply not
> possible after that bms_next_member() because of some factors behind the code?
>

rtindex 0 is not used, see the logic of setup_simple_rel_arrays and the comments
of PlannerInfo.simple_rel_array.

/*
* simple_rel_array holds pointers to "base rels" and "other rels" (see
* comments for RelOptInfo for more info). It is indexed by rangetable
* index (so entry 0 is always wasted). Entries can be NULL when an RTE
* does not correspond to a base relation, such as a join RTE or an
* unreferenced view RTE; or if the RelOptInfo hasn't been made yet.
*/

>
> --
> Best regards,
>  Dmitry                          mailto:pgsql-hackers@dima.nikitin.name
>
>
>


--
Regards
Junwang Zhao



Re: Unclear code - please elaborate

From
Tom Lane
Date:
Dmitry Nikitin <pgsql-hackers@dima.nikitin.name> writes:
>
https://github.com/postgres/postgres/blob/5d39becf8ba0080c98fee4b63575552f6800b012/src/backend/optimizer/prep/prepjointree.c#L3856
> bms_next_member() is allowed to return the zero as a valid value. Subsequent rt_fetch() offsets that
> zero to -1 which leads to the assertion down the code. Nothing wrong here? Either zero is simply not
> possible after that bms_next_member() because of some factors behind the code?

Zero isn't a valid relid.  If we were to find such a value in that
bitmapset, an assertion would be a fine outcome.

            regards, tom lane