pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #16520: Deleting from non-existent column in CTE removes all rows
3+ messages / 3 participants
[nested] [flat]

* BUG #16520: Deleting from non-existent column in CTE removes all rows
@ 2020-06-30 17:30 PG Bug reporting form <noreply@postgresql.org>
  2020-06-30 18:34 ` Re: BUG #16520: Deleting from non-existent column in CTE removes all rows Tom Lane <tgl@sss.pgh.pa.us>
  2020-06-30 19:01 ` Re: BUG #16520: Deleting from non-existent column in CTE removes all rows David G. Johnston <david.g.johnston@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: PG Bug reporting form @ 2020-06-30 17:30 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: jesse007@ymail.com

The following bug has been logged on the website:

Bug reference:      16520
Logged by:          Jesse Lieberg
Email address:      jesse007@ymail.com
PostgreSQL version: 12.3
Operating system:   debian:buster-slim
Description:        

Using the `postgres:12` docker image and given the following:
```
DROP TABLE IF EXISTS test;
CREATE TABLE test (
  id SERIAL,
  username varchar(32)
);
INSERT INTO test (username)
VALUES ('Jesse'), ('Jesse'), ('Scott'), ('Scott'), ('John');
```

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;
```

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
);
```

More information: https://stackoverflow.com/q/62661721/3903479



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

* Re: BUG #16520: Deleting from non-existent column in CTE removes all rows
  2020-06-30 17:30 BUG #16520: Deleting from non-existent column in CTE removes all rows PG Bug reporting form <noreply@postgresql.org>
@ 2020-06-30 18:34 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2020-06-30 18:34 UTC (permalink / raw)
  To: jesse007@ymail.com; +Cc: pgsql-bugs@lists.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





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

* Re: BUG #16520: Deleting from non-existent column in CTE removes all rows
  2020-06-30 17:30 BUG #16520: Deleting from non-existent column in CTE removes all rows PG Bug reporting form <noreply@postgresql.org>
@ 2020-06-30 19:01 ` David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: David G. Johnston @ 2020-06-30 19:01 UTC (permalink / raw)
  To: jesse007@ymail.com <jesse007@ymail.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>

On Tuesday, June 30, 2020, PG Bug reporting form <noreply@postgresql.org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference:      16520
> Logged by:          Jesse Lieberg
> Email address:      jesse007@ymail.com
> PostgreSQL version: 12.3
> Operating system:   debian:buster-slim
> Description:
>
> 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
> );
>

Yes, because the column id does exist - you just omitted the table
reference which ends up making the subquery query equivalent to:  “select
test.id from to_delete” which is mandatory, and generally useful, sql
syntax.

David J.

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


end of thread, other threads:[~2020-06-30 19:01 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-06-30 17:30 BUG #16520: Deleting from non-existent column in CTE removes all rows PG Bug reporting form <noreply@postgresql.org>
2020-06-30 18:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2020-06-30 19:01 ` David G. Johnston <david.g.johnston@gmail.com>

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