RE: Index Skip Scan

From: Floris Van Nee <florisvannee(at)Optiver(dot)com>
To: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>
Cc: Peter Geoghegan <pg(at)bowt(dot)ie>, Jesper Pedersen <jesper(dot)pedersen(at)redhat(dot)com>, David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>, Tomas Vondra <tomas(dot)vondra(at)2ndquadrant(dot)com>, Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>, Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>, "Thomas Munro" <thomas(dot)munro(at)gmail(dot)com>, James Coleman <jtc331(at)gmail(dot)com>, Rafia Sabih <rafia(dot)pghackers(at)gmail(dot)com>, Jeff Janes <jeff(dot)janes(at)gmail(dot)com>, "Bhushan Uparkar" <bhushan(dot)uparkar(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Alexander Korotkov <a(dot)korotkov(at)postgrespro(dot)ru>
Subject: RE: Index Skip Scan
Date: 2020-01-27 14:00:39
Message-ID: e198edfa18804c13b6b383b62fbfd94a@opammb0561.comp.optiver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Dmitry,

Thanks for the new patch! I tested it and managed to find a case that causes some issues. Here's how to reproduce:

drop table if exists t;
create table t as select a,b,b%2 as c,10 as d from generate_series(1,5) a, generate_series(1,1000) b;
create index on t (a,b,c,d);

-- correct
postgres=# begin; declare c scroll cursor for select distinct on (a) a,b,c,d from t order by a desc, b desc; fetch forward all from c; fetch backward all from c; commit;
BEGIN
DECLARE CURSOR
a | b | c | d
---+------+---+----
5 | 1000 | 0 | 10
4 | 1000 | 0 | 10
3 | 1000 | 0 | 10
2 | 1000 | 0 | 10
1 | 1000 | 0 | 10
(5 rows)

a | b | c | d
---+------+---+----
1 | 1000 | 0 | 10
2 | 1000 | 0 | 10
3 | 1000 | 0 | 10
4 | 1000 | 0 | 10
5 | 1000 | 0 | 10
(5 rows)

-- now delete some rows
postgres=# delete from t where a=3;
DELETE 1000

-- and rerun: error is thrown
postgres=# begin; declare c scroll cursor for select distinct on (a) a,b,c,d from t order by a desc, b desc; fetch forward all from c; fetch backward all from c; commit;
BEGIN
DECLARE CURSOR
a | b | c | d
---+------+---+----
5 | 1000 | 0 | 10
4 | 1000 | 0 | 10
2 | 1000 | 0 | 10
1 | 1000 | 0 | 10
(4 rows)

ERROR: lock buffer_content is not held
ROLLBACK

A slightly different situation arises when executing the cursor with an ORDER BY a, b instead of the ORDER BY a DESC, b DESC:
-- recreate table again and execute the delete as above

postgres=# begin; declare c scroll cursor for select distinct on (a) a,b,c,d from t order by a, b; fetch forward all from c; fetch backward all from c; commit;
BEGIN
DECLARE CURSOR
a | b | c | d
---+---+---+----
1 | 1 | 1 | 10
2 | 1 | 1 | 10
4 | 1 | 1 | 10
5 | 1 | 1 | 10
(4 rows)

a | b | c | d
---+-----+---+----
5 | 1 | 1 | 10
4 | 1 | 1 | 10
2 | 827 | 1 | 10
1 | 1 | 1 | 10
(4 rows)

COMMIT

And lastly, you'll also get incorrect results if you do the delete slightly differently:
-- leave one row where a=3 and b=1000
postgres=# delete from t where a=3 and b<=999;
-- the cursor query above won't show any of the a=3 rows even though they should

-Floris

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kohei KaiGai 2020-01-27 14:08:36 Re: TRUNCATE on foreign tables
Previous Message Ranier Vilela 2020-01-27 13:48:18 Re: [PATCH] /src/backend/access/transam/xlog.c, tiny improvements