Re: Unexpected serialization error

From: Andres Freund <andres(at)anarazel(dot)de>
To: Luka Žitnik <luka(dot)zitnik(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Unexpected serialization error
Date: 2021-02-22 01:28:50
Message-ID: 20210222012850.n3pvwispasl5yugq@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

On 2021-02-12 19:14:17 +0100, Luka Žitnik wrote:
> I had no luck over at general slack channel. So I'm beginning to treat this
> as a bug. Here's a test case that unexpectedly fails at last insert. I
> expect it not to fail because the rows that the two transactions act on are
> unrelated to one another.
>
> CREATE TABLE t1 (
> class integer NOT NULL
> );
>
> CREATE INDEX ON t1 (class);
>
> CREATE TABLE t2 (
> class integer NOT NULL
> );
>
> CREATE INDEX ON t2 (class);
>
> ALTER SYSTEM SET enable_seqscan TO off;
>
> -- Session 1
> BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
> SELECT * FROM t2 WHERE class=1;
>
> -- Session 2
> BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE;
> SELECT * FROM t1 WHERE class=2;
> INSERT INTO t2 VALUES(2);
> COMMIT;
>
> -- Session 1
> INSERT INTO t1 VALUES(1);
> COMMIT;

I think you're hitting multiple issues here... For one, initially your
table is empty, in which case we mark the entire index to be a conflict
during for the SELECT * FROM t2 WHERE class=1;

Second, even if the indexes weren't empty, the granularity of the index
predicate locking is a page - which means that S1's predicate lock for
SELECT * FROM t1 WHERE class=2; will conflict with S1's INSERT INTO t1
VALUES(1).

If the SELECTs actual hit rows (and the inserts were updates), you'd
presumably have the sequence suceed. Same with an index where the
inserted values weren't on the same page as the looked up values.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2021-02-22 02:56:44 BUG #16881: error log:Npgsql.PostgresException (0x80004005): XX001: 无法读取文件"base/16394/25021"的块157:只读取了8192字节的0
Previous Message Euler Taveira 2021-02-22 00:06:27 Re: BUG #16879: Delayed standby does not connect to primary on startup