pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Frédéric Yhuel <frederic.yhuel@dalibo.com>
To: pgsql-hackers@lists.postgresql.org
Subject: REINDEX blocks virtually any queries but some prepared queries.
Date: Wed, 6 Apr 2022 16:48:57 +0200
Message-ID: <65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com> (raw)

Hello,

 From the documentation 
(https://www.postgresql.org/docs/current/sql-reindex.html#id-1.9.3.162.7), 
it sounds like REINDEX won't block read queries that don't need the 
index. But it seems like the planner wants to take an ACCESS SHARE lock 
on every indexes, regardless of the query, and so REINDEX actually 
blocks any queries but some prepared queries whose plan have been cached.

I wonder if it is a bug, or if the documentation should be updated. What 
do you think?

Here is a simple demo (tested with postgres 10 and master):

Session #1
===========================================================

srcpg@postgres=# CREATE TABLE flights (id INT generated always as 
identity, takeoff DATE);
CREATE TABLE

srcpg@postgres=# INSERT INTO flights (takeoff) SELECT date '2022-03-01' 
+ interval '1 day' * i FROM generate_series(1,1000) i;
INSERT 0 1000

srcpg@postgres=# CREATE INDEX ON flights(takeoff);
CREATE INDEX

srcpg@postgres=# BEGIN;
BEGIN
srcpg@postgres=# REINDEX INDEX flights_takeoff_idx ;
REINDEX



Session #2
===========================================================

srcpg@postgres=# SELECT pg_backend_pid();
  pg_backend_pid
----------------
         4114695

srcpg@postgres=# EXPLAIN SELECT id FROM flights;
--> it blocks


Session #3
===========================================================

srcpg@postgres=# SELECT locktype, relname, mode, granted FROM pg_locks 
LEFT JOIN pg_class ON (oid = relation) WHERE pid = 4114695;
   locktype  |       relname       |      mode       | granted
------------+---------------------+-----------------+---------
  virtualxid | ∅                   | ExclusiveLock   | t
  relation   | flights_takeoff_idx | AccessShareLock | f
  relation   | flights             | AccessShareLock | t





view thread (10+ messages)  latest in thread

Message-ID: <65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com>
Permalink:  ../65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com/
Also on:    postgresql.org/message-id/65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: frederic.yhuel@dalibo.com
  Subject: Re: REINDEX blocks virtually any queries but some prepared queries.
  In-Reply-To: <65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com>
  References: <65d08718-6f11-978a-4b5a-72b807d4c663@dalibo.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox