pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
12+ messages / 1 participants
[nested] [flat]

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/5844022a272c468014ca816c53223b9623233897

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0e7dabc852d40d33d1341a4c227b1c26460f1a81

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/0f8e93fab91ea2efe1410b52e31edbefefb074c8

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d27418375d64663c920d81b0814d389a33d621db

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e22173e29b719e5e8705feba9073a5956fa0cada

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns.
@ 2026-11-20 16:52  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 16:52 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term has resjunk columns.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist.  If that targetlist had
resjunk entries, for example a window function's ORDER BY column or a
GROUP BY column that is not selected, the new columns landed after
them.  That breaks the expectation that resjunk entries come last, and
the column names added to the wrapping subquery then pointed at the
wrong entries.  Depending on the query, this caused errors such as
"could not find replacement targetlist entry" or "attribute N of type
record has wrong type", an assertion failure in assert-enabled builds,
or silently wrong results.

Fix by detaching any resjunk entries before adding the new columns and
re-attaching them afterwards with renumbered resnos.  Sort/group
clauses reference targetlist entries via ressortgroupref, not resno,
so renumbering them is safe.

The non-recursive term is not affected, since it is wrapped in a new
subquery without modifying its targetlist.

Bug: #19922
Reported-by: Robins Tharakan <tharakan@gmail.com>
Diagnosed-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Nitin Motiani <nitinmotiani@google.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19922-a0d338245d361a4c@postgresql.org
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/fba1717d5a6537c54070f5f8eafba5ea6e3a1cbd

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c | 42 ++++++++++++++++++++++++++++++--
src/test/regress/expected/with.out       | 40 ++++++++++++++++++++++++++++++
src/test/regress/sql/with.sql            | 23 +++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/65ebed1f5e2f16d56234b5a27b8d508d18c3854d

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/ecebc02f69a20b539513f4c253eac24221a901e5

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/206714d0ddc4c6c9ed8c00ae9afbbb634ff8c676

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
REL_17_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/8205b3129f9bd12d79fd9b4b7443f6e2665c55bd

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
REL_16_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/6b6faf1216a2f5d27e6cfe0aafeb5d709d3028c6

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.
@ 2026-11-20 19:53  Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 12+ messages in thread

From: Tom Lane @ 2026-11-20 19:53 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT.

rewriteSearchAndCycle() appended the extra SEARCH/CYCLE columns to
the end of the recursive term's targetlist, but did not consider
the interaction of that with SELECT DISTINCT.  Since we don't (and
shouldn't) mark them resjunk, they should logically be included in
the unique-ification action of SELECT DISTINCT, ie get added to the
distinctClause.  We didn't do that, which confuses recent planner
versions into assert'ing or building an incorrect plan.

The simplest fix is to mark the query as hasDistinctOn, so that
it looks like it was originally written with a DISTINCT ON list
containing just the original columns.  Aside from being a trivial
change, this restores the behavior as it was pre-v18.

In principle, it should not matter whether we add the extra columns
to the distinctClause or not, as they should be immutable functions
of the original columns and thus couldn't add extra rows to the
de-duplicated output.  Per the discussion thread, it's possible to
construct artificial cases where the <cycle mark column> is only
a stable function, and maybe even a volatile function if weirdly-
behaving casts are involved.  But even then it seems better if the
implementation arbitrarily selects one mark value rather than
duplicating original rows.

Although no bad behavior currently manifests pre-v18, that seems
like luck rather than something to count on: we might back-patch some
fix that requires consistency of distinctClause and hasDistinctOn.
So back-patch to all supported branches.

Bug: #19923
Reported-by: Robins Tharakan <tharakan@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19923-24cef1e958fa2370@postgresql.org
Backpatch-through: 15

Branch
------
REL_15_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/e9d3d12c79c968b3357bfbf88d2f95212bb2cf20

Modified Files
--------------
src/backend/rewrite/rewriteSearchCycle.c |  9 +++++++++
src/test/regress/expected/with.out       | 13 +++++++++++++
src/test/regress/sql/with.sql            |  8 ++++++++
3 files changed, 30 insertions(+)



^ permalink  raw  reply  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2026-11-20 19:53 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 16:52 pgsql: Fix SEARCH/CYCLE when the recursive term has resjunk columns. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>
2026-11-20 19:53 pgsql: Fix SEARCH/CYCLE when the recursive term is SELECT DISTINCT. Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox