BUG #14328: SELECT FOR UPDATE doesn't return existing row

Started by Dima Pavlovover 9 years ago3 messagesbugs
Jump to latest
#1Dima Pavlov
imyfess@gmail.com

The following bug has been logged on the website:

Bug reference: 14328
Logged by: Dima Pavlov
Email address: imyfess@gmail.com
PostgreSQL version: 9.4.4
Operating system: Windows 10
Description:

SELECT FOR UPDATE returns 0 rows in scenario below. But if I just execute
sql query from second transaction it always returns 1 row.

ENVIRONMENT:
-------------
CREATE TABLE t1 (_pk serial, t1c1 integer, t1c2 integer, t1c3 text);
CREATE TABLE t2 (_pk serial, t2c1 text, t2c2 integer);
insert into t1 (t1c1, t1c2, t1c3) values(123456789, 100,
'string_value_1');
insert into t2 (t2c1, t2c2) values('string_value_2', 100);

TRANSACTION 1:
--------------
BEGIN;
-- This query updates t1c1 to its current value, it doesn't change
anything
UPDATE t1 SET t1c3 = 'string_value_1' WHERE t1c1 = 123456789;

-- Query returned successfully: one row affected, 51 msec execution time.

TRANSACTION 2:
--------------
WITH
cte1 AS (
SELECT t2c2 FROM t2 WHERE t2c1 = 'string_value_2'
),

cte2 AS (
SELECT * FROM t1
WHERE
t1c1 = 123456789
AND t1c2 = (SELECT t2c2 FROM cte1)
FOR UPDATE
)

SELECT * FROM cte2

-- Waiting

TRANSACTION 1:
--------------
COMMIT;

-- Query returned successfully with no result in 41 msec.

TRANSACTION 2:
--------------
-- Returned 0 rows

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

#2Andomar
andomar@aule.net
In reply to: Dima Pavlov (#1)
Re: BUG #14328: SELECT FOR UPDATE doesn't return existing row

I can reproduce this on 9.4 on Debian Linux.

This relates to a question on Stack Overflow: http://stackoverflow.com/q/39550510/50552

The row seems to be dropped from the result set after the query waits on the lock.

Cheers,
Andomar

Show quoted text

On 19 Sep 2016, at 18:01, imyfess@gmail.com wrote:

14328

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Dima Pavlov (#1)
Re: BUG #14328: SELECT FOR UPDATE doesn't return existing row

imyfess@gmail.com writes:

SELECT FOR UPDATE returns 0 rows in scenario below. But if I just execute
sql query from second transaction it always returns 1 row.

I've pushed a patch for this. Thanks for the report!

regards, tom lane

--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs