更新:問題已解決!SELECT FOR UPDATE返回零行
這是PostgreSQL中的bug。 Tom Lane將其固定在這個 commit。
爲什麼SELECT FOR UPDATE在下面的場景中返回0行?但如果我只是從第二個事務執行sql查詢,它總是返回1行。
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
實施例:
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);
難道你不能提供一個最小的完整例子嗎?即在必要時包含您的'create table'語句和一些'insert'語句。 – redneb
@redneb: CREATE TABLE t1(_pk serial,t1c1 integer,t1c2 integer,t1c3 text); CREATE TABLE t2(_pk serial,t2c1 text,t2c2 integer); 插入t1(t1c1,t1c2,t1c3)的值(123456789,100,'string_value_1'); 插入到t2(t2c1,t2c2)值('string_value_2',100); –