Re: CTE subquery referencing phantom records

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Dave Bothwell <dbothwell(at)primepoint(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: CTE subquery referencing phantom records
Date: 2023-03-28 19:31:42
Message-ID: 3655676.1680031902@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Dave Bothwell <dbothwell(at)primepoint(dot)com> writes:
> Common Table Expression issue in PostgreSQL 15
> The final WHERE statement in our CTE appears to be referencing more
> records, then the first subquery of our CTE is returning. It works when the
> first subquery is filtered by the primary key (We left a commented out
> example in the WHERE statement). It fails when the first subquery is
> filtered by a string. We have provided a complete example of the problem
> below (The following example works in PostgreSQL 11):

I don't think this is a Postgres bug: your query is making unwarranted
assumptions about the order in which different WHERE clauses will be
evaluated. You could return to the PG 11 behavior by marking the
CTE as "materialized":

with
phase_in_date as materialized ( ...

but it'd be better to make the data structure more normalized so that
you don't have hazards like applying date() to fields for which it
would fail on some rows.

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2023-03-28 20:26:57 BUG #17875: Postgres common repo is missing gdal32-libs-3.2.3-6.rhel8.x86_64/proj80-8.0.1-1.rhel8.x86_64
Previous Message Tom Lane 2023-03-28 19:18:30 Re: BUG #17872: Dropping an attribute of a composite type breaks indexes over the type silently