From: Tom Lane <tgl@sss.pgh.pa.us>
To: jesse007@ymail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16520: Deleting from non-existent column in CTE removes all rows
Date: Tue, 30 Jun 2020 14:34:05 -0400
Message-ID: <494165.1593542045@sss.pgh.pa.us> (raw)
In-Reply-To: <16520-e4e105305f3d0c4a@postgresql.org>
References: <16520-e4e105305f3d0c4a@postgresql.org>
PG Bug reporting form <noreply@postgresql.org> writes:
> This will throw an error that the column does not exist:
> ```
> WITH to_delete AS (
> SELECT MIN(id), username
> FROM test
> GROUP BY username
> HAVING count(*) > 1
> )
> SELECT id
> FROM to_delete;
> ```
Sure, because the columns exposed by to_delete are named "min" and
"username", not "id".
> However, the this will not return an error and instead deletes all rows:
> ```
> WITH to_delete AS (
> SELECT MIN(id), username
> FROM test
> GROUP BY username
> HAVING count(*) > 1
> )
> DELETE FROM test
> WHERE id IN (
> SELECT id
> FROM to_delete
> );
> ```
You've been bit by the standard SQL newbie trap that sub-selects
allow outer references. That IN clause devolves to constant true
(at least for non-null id values, and with to_delete known not
empty), because it's just "id = id".
regards, tom lane
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, jesse007@ymail.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #16520: Deleting from non-existent column in CTE removes all rows
In-Reply-To: <494165.1593542045@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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