Fix incorrect tests for SRFs in relation_can_be_sorted_early().
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 3 Aug 2022 21:33:42 +0000 (17:33 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 3 Aug 2022 21:33:42 +0000 (17:33 -0400)
commitda4ed7588132db44ab11bf52bad0472eb0c868e4
tree76df3c58abc9b6f2dc1675a55555d9a789269121
parentb2694aebe33c8a664cb7fcaacc5a3fe30d120a09
Fix incorrect tests for SRFs in relation_can_be_sorted_early().

Commit fac1b470a thought we could check for set-returning functions
by testing only the top-level node in an expression tree.  This is
wrong in itself, and to make matters worse it encouraged others
to make the same mistake, by exporting tlist.c's special-purpose
IS_SRF_CALL() as a widely-visible macro.  I can't find any evidence
that anyone's taken the bait, but it was only a matter of time.

Use expression_returns_set() instead, and stuff the IS_SRF_CALL()
genie back in its bottle, this time with a warning label.  I also
added a couple of cross-reference comments.

After a fair amount of fooling around, I've despaired of making
a robust test case that exposes the bug reliably, so no test case
here.  (Note that the test case added by fac1b470a is itself
broken, in that it doesn't notice if you remove the code change.
The repro given by the bug submitter currently doesn't fail either
in v15 or HEAD, though I suspect that may indicate an unrelated bug.)

Per bug #17564 from Martijn van Oosterhout.  Back-patch to v13,
as the faulty patch was.

Discussion: https://postgr.es/m/17564-c7472c2f90ef2da3@postgresql.org
src/backend/nodes/nodeFuncs.c
src/backend/optimizer/path/equivclass.c
src/backend/optimizer/util/tlist.c
src/include/optimizer/optimizer.h