Remove inadequate assertion check in CTE inlining.
authorTom Lane
Thu, 21 Apr 2022 21:58:52 +0000 (17:58 -0400)
committerTom Lane
Thu, 21 Apr 2022 21:58:52 +0000 (17:58 -0400)
commitda22ef388a2469c3d7d11a8c97a3c41cc0016f4a
tree9543132b3d15603c3cc61c8aa2f4b58d493c1b6a
parentb235d41d9646c531864ecc680fd9ec5da9217051
Remove inadequate assertion check in CTE inlining.

inline_cte() expected to find exactly as many references to the
target CTE as its cterefcount indicates.  While that should be
accurate for the tree as emitted by the parser, there are some
optimizations that occur upstream of here that could falsify it,
notably removal of unused subquery output expressions.

Trying to make the accounting 100% accurate seems expensive and
doomed to future breakage.  It's not really worth it, because
all this code is protecting is downstream assumptions that every
referenced CTE has a plan.  Let's convert those assertions to
regular test-and-elog just in case there's some actual problem,
and then drop the failing assertion.

Per report from Tomas Vondra (thanks also to Richard Guo for
analysis).  Back-patch to v12 where the faulty code came in.

Discussion: https://postgr.es/m/29196a1e-ed47-c7ca-9be2-b1c636816183@enterprisedb.com
src/backend/optimizer/path/allpaths.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/plan/subselect.c
src/include/nodes/pathnodes.h
src/test/regress/expected/with.out
src/test/regress/sql/with.sql