pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: REINDEX INDEX results in a crash for an index of pg_class since 9.6
Date: Tue, 30 Apr 2019 00:05:52 -0700
Message-ID: <20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de> (raw)
In-Reply-To: <26528.1556599820@sss.pgh.pa.us>
References: <20190425210309.cnofpjnwsrhdnahd@alap3.anarazel.de>
	<31933.1556226753@sss.pgh.pa.us>
	<20190426020248.iyev66bkpf2abxqd@alap3.anarazel.de>
	<7882.1556290296@sss.pgh.pa.us>
	<12189.1556575627@sss.pgh.pa.us>
	<20190429220924.lfptluzkujb26y2n@alap3.anarazel.de>
	<20190430030342.jmbgnih4q3cdoufz@alap3.anarazel.de>
	<26051.1556599053@sss.pgh.pa.us>
	<BC406D10-146C-4495-AC5E-492CADBFA144@anarazel.de>
	<26528.1556599820@sss.pgh.pa.us>

Hi,

On 2019-04-30 00:50:20 -0400, Tom Lane wrote:
> Andres Freund <andres@anarazel.de> writes:
> > On April 29, 2019 9:37:33 PM PDT, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> >> Seems like putting reindexes of pg_class into a test script that runs
> >> in parallel with other DDL wasn't a hot idea.
> 
> > Saw that. Will try to reproduce (and if necessary either run separately or revert). But isn't that somewhat broken? They're not run in a transaction, so the locking shouldn't be deadlock prone.
> 
> Hm?  REINDEX INDEX is deadlock-prone by definition, because it starts
> by opening/locking the index and then it has to open/lock the index's
> table.  Every other operation locks tables before their indexes.

We claim to have solved that:

/*
 * ReindexIndex
 *		Recreate a specific index.
 */
void
ReindexIndex(RangeVar *indexRelation, int options, bool concurrent)


	/*
	 * Find and lock index, and check permissions on table; use callback to
	 * obtain lock on table first, to avoid deadlock hazard.  The lock level
	 * used here must match the index lock obtained in reindex_index().
	 */
	indOid = RangeVarGetRelidExtended(indexRelation,
									  concurrent ? ShareUpdateExclusiveLock : AccessExclusiveLock,
									  0,
									  RangeVarCallbackForReindexIndex,
									  (void *) &heapOid);

and I don't see an obvious hole in the general implementation. Minus the
comment that code exists back to 9.4.

I suspect the problem isn't REINDEX INDEX in general, it's REINDEX INDEX
over catalog tables modified during reindex. The callback acquires a
ShareLock lock on the index's table, but *also* during the reindex needs
a RowExclusiveLock on pg_class, etc. E.g. in RelationSetNewRelfilenode()
on pg_class, and on pg_index in index_build(). Which means there's a
lock-upgrade hazard (Share to RowExclusive - well, that's more a
side-grade, but still deadlock prone).

I can think of ways to fix that (e.g. if reindex is on pg_class or
index, use SHARE ROW EXCLUSIVE, rather than SHARE), but we'd probably
not want to backpatch that.

I'll try to reproduce tomorrow.

Greetings,

Andres Freund





view thread (82+ messages)  latest in thread

Message-ID: <20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de>
Permalink:  ../20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de/
Also on:    postgresql.org/message-id/20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de

 · 

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: andres@anarazel.de, tgl@sss.pgh.pa.us, michael@paquier.xyz
  Subject: Re: REINDEX INDEX results in a crash for an index of pg_class since 9.6
  In-Reply-To: <20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de>
  References: <20190425210309.cnofpjnwsrhdnahd@alap3.anarazel.de> <31933.1556226753@sss.pgh.pa.us> <20190426020248.iyev66bkpf2abxqd@alap3.anarazel.de> <7882.1556290296@sss.pgh.pa.us> <12189.1556575627@sss.pgh.pa.us> <20190429220924.lfptluzkujb26y2n@alap3.anarazel.de> <20190430030342.jmbgnih4q3cdoufz@alap3.anarazel.de> <26051.1556599053@sss.pgh.pa.us> <BC406D10-146C-4495-AC5E-492CADBFA144@anarazel.de> <26528.1556599820@sss.pgh.pa.us> <20190430070552.jzqgcy4ihalx7nur@alap3.anarazel.de>

* 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