Subject: Unclear code - please elaborate

From: Dmitry Nikitin <[email-masked]>
Date: Fri, 29 Nov 2024 20:11:34 +0300

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:[email-masked]

---

From: Junwang Zhao <[email-masked]>
Date: Sat, 30 Nov 2024 19:54:35 +0800

> 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.
*/

-- 
Regards
Junwang Zhao

---

From: Tom Lane <[email-masked]>
Date: Sat, 30 Nov 2024 11:12:01 -0500

> 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