Fix incautious CTE matching in rewriteSearchAndCycle().
authorTom Lane <[email protected]>
Sat, 23 Apr 2022 16:16:12 +0000 (12:16 -0400)
committerTom Lane <[email protected]>
Sat, 23 Apr 2022 16:16:12 +0000 (12:16 -0400)
commitf819020d400f1dbd96ee1a9fd44a1f6f44932b4d
tree312fb630148146855cd0659e1e55f0113f9c8f57
parentc1da0acbb06e9175044b436d14c51cef03339109
Fix incautious CTE matching in rewriteSearchAndCycle().

This function looks for a reference to the recursive WITH CTE,
but it checked only the CTE name not ctelevelsup, so that it could
seize on a lower CTE that happened to have the same name.  This
would result in planner failures later, either weird errors such as
"could not find attribute 2 in subquery targetlist", or crashes
or assertion failures.  The code also merely Assert'ed that it found
a matching entry, which is not guaranteed at all by the parser.

Per bugs #17320 and #17318 from Zhiyong Wu.
Thanks to Kyotaro Horiguchi for investigation.

Discussion: https://postgr.es/m/17320-70e37868182512ab@postgresql.org
Discussion: https://postgr.es/m/17318-2eb65a3a611d2368@postgresql.org
src/backend/rewrite/rewriteSearchCycle.c
src/test/regress/expected/with.out
src/test/regress/sql/with.sql