postgresql/src
Tom Lane 35b5c62c3a Don't allow CTEs to determine semantic levels of aggregates.
The fix for bug #19055 (commit b0cc0a71e) allowed CTE references in
sub-selects within aggregate functions to affect the semantic levels
assigned to such aggregates.  It turns out this broke some related
cases, leading to assertion failures or strange planner errors such
as "unexpected outer reference in CTE query".  After experimenting
with some alternative rules for assigning the semantic level in
such cases, we've come to the conclusion that changing the level
is more likely to break things than be helpful.

Therefore, this patch undoes what b0cc0a71e changed, and instead
installs logic to throw an error if there is any reference to a
CTE that's below the semantic level that standard SQL rules would
assign to the aggregate based on its contained Var and Aggref nodes.
(The SQL standard disallows sub-selects within aggregate functions,
so it can't reach the troublesome case and hence has no rule for
what to do.)

Perhaps someone will come along with a legitimate query that this
logic rejects, and if so probably the example will help us craft
a level-adjustment rule that works better than what b0cc0a71e did.
I'm not holding my breath for that though, because the previous
logic had been there for a very long time before bug #19055 without
complaints, and that bug report sure looks to have originated from
fuzzing not from real usage.

Like b0cc0a71e, back-patch to all supported branches, though
sadly that no longer includes v13.

Bug: #19106
Reported-by: Kamil Monicz <kamil@monicz.dev>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19106-9dd3668a0734cd72@postgresql.org
Backpatch-through: 14
2025-11-18 12:56:55 -05:00
..
backend Don't allow CTEs to determine semantic levels of aggregates. 2025-11-18 12:56:55 -05:00
bin Log a note at program start when running in dry-run mode 2025-11-18 16:13:29 +01:00
common Document that pg_getaddrinfo_all does not accept null hints 2025-11-13 16:35:07 +01:00
fe_utils Add \pset options for boolean value display 2025-11-03 17:40:39 +01:00
include Optimize shared memory usage for WaitLSNProcInfo 2025-11-18 09:50:12 +02:00
interfaces Change coding pattern for CURL_IGNORE_DEPRECATION() 2025-11-12 12:35:14 +01:00
makefiles Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
pl Avoid memory leak in validation of a PL/Python trigger function. 2025-10-23 14:23:26 -04:00
port Fix pg_crc32c_armv8_choose.c to build with ancient glibc releases. 2025-11-17 15:24:34 -05:00
template Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
test Don't allow CTEs to determine semantic levels of aggregates. 2025-11-18 12:56:55 -05:00
timezone Generate GUC tables from .dat file 2025-09-03 09:45:17 +02:00
tools Replace off_t by pgoff_t in I/O routines 2025-11-13 12:41:40 +09:00
tutorial Fix compile of src/tutorial/funcs.c 2025-10-07 10:45:57 +02:00
.gitignore
DEVELOPERS
Makefile Remove distprep 2023-11-06 15:18:04 +01:00
Makefile.global.in Remove traces of support for Sun Studio compiler 2025-09-12 07:39:05 +02:00
Makefile.shlib Use exported symbols list on macOS for loadable modules as well 2025-06-10 07:04:43 +02:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
nls-global.mk Fix update-po for the PGXS case 2025-10-16 20:21:05 +02:00