BUG #17233: Incorrect behavior of DELETE command with bad subquery in WHERE clause

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: lxndrkrlv(at)gmail(dot)com
Subject: BUG #17233: Incorrect behavior of DELETE command with bad subquery in WHERE clause
Date: 2021-10-16 19:41:52
Message-ID: 17233-afb9d806aaa64b17@postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 17233
Logged by: Alexander Korolev
Email address: lxndrkrlv(at)gmail(dot)com
PostgreSQL version: 14.0
Operating system: Windows
Description:

This SELECT command fails as expected:
SELECT CTID FROM tmp1 INNER JOIN tmp2 ON tmp1.id = tmp2.id FOR UPDATE;
-- ERROR: column "ctid" does not exist.

But if I use same SELECT in WHERE clause of DELETE command
DELETE FROM tmp1 WHERE CTID in (
SELECT CTID FROM tmp1 INNER JOIN tmp2 ON tmp1.id = tmp2.id FOR
UPDATE);
this command is executed without errors.

Repro
--------------------------------------------------------------------------------------------
DROP TABLE IF EXISTS pg_temp.tmp1;
DROP TABLE IF EXISTS pg_temp.tmp2;

CREATE TEMPORARY TABLE tmp1 (id int NOT NULL, name text);
CREATE TEMPORARY TABLE tmp2 (id int NOT NULL, name text);

INSERT INTO tmp1 (id, name) VALUES (1, 'aaa'), (2, 'bbb');
INSERT INTO tmp2 (id, name) VALUES (1, 'aaa'), (2, 'bbb'), (3, 'eee');

SELECT ctid, * from tmp1;
SELECT ctid, * from tmp2;

/* Works as expected: ERROR: column "ctid" does not exist
SELECT CTID
FROM tmp1
INNER JOIN tmp2 ON tmp1.id = tmp2.id FOR UPDATE;
*/

// Executed without errors !!!
DELETE FROM tmp1
WHERE CTID in (
SELECT CTID
FROM tmp1
INNER JOIN tmp2 ON tmp1.id = tmp2.id FOR UPDATE
);

SELECT * FROM tmp1;
SELECT * FROM tmp2;
--------------------------------------------------------------------------------------------

* in some cases, if tables is big, server process is hang. I can't make
small reproducible example.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2021-10-17 14:15:48 Re: BUG #17233: Incorrect behavior of DELETE command with bad subquery in WHERE clause
Previous Message Alvaro Herrera 2021-10-16 14:59:15 Re: BUG #17232: DISTINCT ON does not allow AS