From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Michael Paquier <michael@paquier.xyz>
Cc: Andres Freund <andres@anarazel.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Manuel Rigger <rigger.manuel@gmail.com>
Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: REINDEX CONCURRENTLY unexpectedly fails
Date: Fri, 17 Jan 2020 14:53:11 +0200
Message-ID: <39c1e415-96e6-9b91-79a8-aa1cbd25e801@iki.fi> (raw)
In-Reply-To: <20200115013824.GA2243@paquier.xyz>
References: <20191115080706.GI1849@paquier.xyz>
<20191120013649.2xfr2dnznyjgluch@alap3.anarazel.de>
<20191120035408.GA4243@paquier.xyz>
<20191212213709.neopqccvdo724eha@alap3.anarazel.de>
<20191213034536.GD1942@paquier.xyz>
<20191223015956.GB34339@paquier.xyz>
<8f0bde7b-2d96-fb5c-cd0e-553694c1d376@iki.fi>
<20200108081930.GF3413@paquier.xyz>
<20200109030619.GC2251@paquier.xyz>
<d62659d5-8860-075f-1e86-f17834f26495@iki.fi>
<20200115013824.GA2243@paquier.xyz>
On 15/01/2020 03:39, Michael Paquier wrote:
> Thanks for taking the time to share your opinion. That was as well my
> feeling with the peanut and the sledgehammer. I liked the peanuts,
> but not the hammer part.
Heh, yeah :-).
> There are still some parts I liked about v4 (doc wording, tweaks about
> the shape of RelationSupportsConcurrentIndexing and its use in
> assertions, setting up the concurrent flag in RemoveRelation and use an
> assert in index_drop is also cleaner), so I kept a good portion of
> v4. Attached is an updated patch, v5, that removes the parts
> enforcing the lock when looking at the relation OID based on its
> RangeVar.
>
> Any thoughts?
Some comments below:
> diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
> index 3e59e647e5..4139232b51 100644
> --- a/src/backend/catalog/index.c
> +++ b/src/backend/catalog/index.c
> @@ -2016,6 +2016,13 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
> LOCKTAG heaplocktag;
> LOCKMODE lockmode;
>
> + /*
> + * A relation not supporting concurrent indexing should never do
> + * a concurrent index drop or try to use a concurrent lock mode.
> + */
> + Assert(RelationSupportsConcurrentIndexing(indexId) ||
> + (!concurrent && !concurrent_lock_mode));
> +
> /*
> * To drop an index safely, we must grab exclusive lock on its parent
> * table. Exclusive lock on the index alone is insufficient because
Or maybe decide to do non-current drop within index_drop() itself,
instead of requiring the caller to set 'concurrent' differently for
temporary tables?
> @@ -2490,6 +2500,30 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
> return true;
> }
>
> +/*
> + * RelationSupportsConcurrentIndexing
> + *
> + * Check if a relation supports concurrent builds or not. This is used
> + * before processing CREATE INDEX, DROP INDEX or REINDEX when using
> + * CONCURRENTLY to decide if the operation is supported.
> + */
> +bool
> +RelationSupportsConcurrentIndexing(Oid relid)
> +{
> + /*
> + * Build indexes non-concurrently for temporary relations. Such
> + * relations only work with the session assigned to them, so they are
> + * not subject to concurrent concerns, and a concurrent build would
> + * cause issues with ON COMMIT actions triggered by the transactions
> + * of the concurrent build. A non-concurrent reindex is also more
> + * efficient in this case.
> + */
> + if (get_rel_persistence(relid) == RELPERSISTENCE_TEMP)
> + return false;
> +
> + return true;
> +}
> +
Sorry to beat a dead hore, but I still don't like this function:
* Does it take a table OID or index OID? (Answer: both)
* There's a hidden assumption that if
RelationSupportsConcurrentIndexing() returns false, then it's OK to
upgrade the lock. That's true today, but if we added other conditions
when RelationSupportsConcurrentIndexing() returned false, there would be
trouble. It seems like a bad abstraction.
This would be better if the function was renamed to something like "Is
it OK to upgrade a CONCURRENTLY build to non-CONCURRENTLY?", but meh. I
understand that it's nice to have a place for this comment, so that it
doesn't need to be repeated in so many places. But I feel that a little
bit of repetition is better in this case. The reasoning isn't exactly
the same for CREATE INDEX, DROP INDEX, and REINDEX anyway.
> diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
> index 52ce02f898..d63a885638 100644
> --- a/src/backend/commands/indexcmds.c
> +++ b/src/backend/commands/indexcmds.c
> @@ -485,6 +485,13 @@ DefineIndex(Oid relationId,
> GUC_ACTION_SAVE, true, 0, false);
> }
>
> + /*
> + * Enforce non-concurrent build if the relation does not support this
> + * option. Do this before any use of the concurrent option is done.
> + */
> + if (!RelationSupportsConcurrentIndexing(relationId))
> + stmt->concurrent = false;
> +
Is it OK to scribble on the original 'stmt' here? Doesn't seem kosher,
although it probably works fine in practice.
> @@ -2769,6 +2778,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
> /* Open relation to get its indexes */
> heapRelation = table_open(relationOid, ShareUpdateExclusiveLock);
>
> + /* Relation had better support concurrent indexing */
> + Assert(RelationSupportsConcurrentIndexing(relationOid));
> +
> /* Add all the valid indexes of relation to list */
> foreach(lc, RelationGetIndexList(heapRelation))
> {
Do we care whether the *table* supports concurrent indexing, rather than
individual indexes? I guess that's academic, since you can't have
temporary indexes on a permanent table, or vice versa.
> @@ -2937,6 +2952,13 @@ ReindexRelationConcurrently(Oid relationOid, int options)
> heapRel = table_open(indexRel->rd_index->indrelid,
> ShareUpdateExclusiveLock);
>
> + /*
> + * Also check for active uses of the relation in the current
> + * transaction, including open scans and pending AFTER trigger
> + * events.
> + */
> + CheckTableNotInUse(indexRel, "REINDEX");
> +
> pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
> RelationGetRelid(heapRel));
> pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
I don't understand why this is required for this patch. It seems like a
good thing to check, I think otherwise you get an error from the
CheckTableNotInUse() call in index_drop(), in phase 6 where the old
indexes are dropped. But it seems unrelated to the rest of the patch.
Maybe commit it as a separate patch?
I came up with the attached version. It seems a bit more clear to me.
I'm not 100% wedded to this, though, so if you want to proceed based on
your version instead, feel free. The docs and the tests are unchanged.
- Heikki
Attachments:reindex-conc-temp-v5-heikki.patch (text/x-patch, 17.7K) downloaddiff --git a/doc/src/sgml/ref/create_index.sgml b/doc/src/sgml/ref/create_index.sgmlindex 629a31ef79..ab362a0dc5 100644--- a/doc/src/sgml/ref/create_index.sgml+++ b/doc/src/sgml/ref/create_index.sgml@@ -129,6 +129,11 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] <replaceable class=
— see <xref linkend="sql-createindex-concurrently"
endterm="sql-createindex-concurrently-title"/>.
</para>
+ <para>+ For temporary tables, <command>CREATE INDEX</command> is always+ non-concurrent, as no other session can access them, and+ non-concurrent index creation is cheaper.+ </para>
</listitem>
</varlistentry>
diff --git a/doc/src/sgml/ref/drop_index.sgml b/doc/src/sgml/ref/drop_index.sgmlindex 2a8ca5bf68..0aedd71bd6 100644--- a/doc/src/sgml/ref/drop_index.sgml+++ b/doc/src/sgml/ref/drop_index.sgml@@ -58,6 +58,11 @@ DROP INDEX [ CONCURRENTLY ] [ IF EXISTS ] <replaceable class="parameter">name</r
performed within a transaction block, but
<command>DROP INDEX CONCURRENTLY</command> cannot.
</para>
+ <para>+ For temporary tables, <command>DROP INDEX</command> is always+ non-concurrent, as no other session can access them, and+ non-concurrent index drop is cheaper.+ </para>
</listitem>
</varlistentry>
diff --git a/doc/src/sgml/ref/reindex.sgml b/doc/src/sgml/ref/reindex.sgmlindex 5aa59d3b75..0cc19b86ee 100644--- a/doc/src/sgml/ref/reindex.sgml+++ b/doc/src/sgml/ref/reindex.sgml@@ -166,6 +166,11 @@ REINDEX [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] { IN
— see <xref linkend="sql-reindex-concurrently"
endterm="sql-reindex-concurrently-title"/>.
</para>
+ <para>+ For temporary tables, <command>REINDEX</command> is always+ non-concurrent, as no other session can access them, and+ non-concurrent index creation is cheaper.+ </para>
</listitem>
</varlistentry>
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.cindex 3e59e647e5..031504e157 100644--- a/src/backend/catalog/index.c+++ b/src/backend/catalog/index.c@@ -2033,12 +2033,30 @@ index_drop(Oid indexId, bool concurrent, bool concurrent_lock_mode)
* doing CREATE/DROP INDEX CONCURRENTLY on the same index. (We will get
* AccessExclusiveLock on the index below, once we're sure nobody else is
* using it.)
+ *+ * If it's a temporary relation, however, we force non-concurrent drop+ * even if CONCURRENTLY was requested. Other backends can't access a+ * temporary relation, so there's no harm in grabbing a stronger lock, and+ * a non-concurrent DROP is more efficient.
*/
heapId = IndexGetRelation(indexId, false);
lockmode = (concurrent || concurrent_lock_mode) ? ShareUpdateExclusiveLock : AccessExclusiveLock;
userHeapRelation = table_open(heapId, lockmode);
userIndexRelation = index_open(indexId, lockmode);
+ if (concurrent && userIndexRelation->rd_rel->relpersistence == RELPERSISTENCE_TEMP)+ {+ /*+ * If it's a temporary relation, and we acquired a weaker lock because+ * CONCURRENTLY was specificed, upgrade it. There's no deadlock+ * hazard from upgrading the lock, because a backend can't access+ * another session's temporary relations.+ */+ index_close(userIndexRelation, NoLock);+ userIndexRelation = index_open(indexId, AccessExclusiveLock);+ concurrent = false;+ }+
/*
* We might still have open queries using it in our own session, which the
* above locking won't prevent, so test explicitly.
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.cindex 52ce02f898..28cba72bca 100644--- a/src/backend/commands/indexcmds.c+++ b/src/backend/commands/indexcmds.c@@ -438,6 +438,7 @@ DefineIndex(Oid relationId,
bool skip_build,
bool quiet)
{
+ bool concurrent;
char *indexRelationName;
char *accessMethodName;
Oid *typeObjectId;
@@ -485,6 +486,18 @@ DefineIndex(Oid relationId,
GUC_ACTION_SAVE, true, 0, false);
}
+ /*+ * Force non-concurrent build on temporary relations, even if CONCURRENTLY+ * was requested. Other backends can't access a temporary relation, so+ * there's no harm in grabbing a stronger lock, and a non-concurrent DROP+ * is more efficient. Do this before any use of the concurrent option is+ * done.+ */+ if (stmt->concurrent && get_rel_persistence(relationId) != RELPERSISTENCE_TEMP)+ concurrent = true;+ else+ concurrent = false;+
/*
* Start progress report. If we're building a partition, this was already
* done.
@@ -494,7 +507,7 @@ DefineIndex(Oid relationId,
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
relationId);
pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
- stmt->concurrent ?+ concurrent ?
PROGRESS_CREATEIDX_COMMAND_CREATE_CONCURRENTLY :
PROGRESS_CREATEIDX_COMMAND_CREATE);
}
@@ -590,6 +603,12 @@ DefineIndex(Oid relationId,
partitioned = rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE;
if (partitioned)
{
+ /*+ * Note: we check 'stmt->concurrent' rather than 'concurrent', so that+ * the error is thrown also for temporary tables. Seems better to be+ * consistent, even though we could do it on temporary table because+ * we're not actually doing it concurrently.+ */
if (stmt->concurrent)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -781,8 +800,8 @@ DefineIndex(Oid relationId,
NIL, /* expressions, NIL for now */
make_ands_implicit((Expr *) stmt->whereClause),
stmt->unique,
- !stmt->concurrent,- stmt->concurrent);+ !concurrent,+ concurrent);
typeObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
collationObjectId = (Oid *) palloc(numberOfAttributes * sizeof(Oid));
@@ -943,8 +962,11 @@ DefineIndex(Oid relationId,
/*
* A valid stmt->oldNode implies that we already have a built form of the
* index. The caller should also decline any index build.
+ *+ * FIXME: should this check 'concurrent' or 'stmt->concurrent'? I don't+ * quite understand the conditions here.
*/
- Assert(!OidIsValid(stmt->oldNode) || (skip_build && !stmt->concurrent));+ Assert(!OidIsValid(stmt->oldNode) || (skip_build && !concurrent));
/*
* Make the catalog entries for the index, including constraints. This
@@ -955,11 +977,11 @@ DefineIndex(Oid relationId,
flags = constr_flags = 0;
if (stmt->isconstraint)
flags |= INDEX_CREATE_ADD_CONSTRAINT;
- if (skip_build || stmt->concurrent || partitioned)+ if (skip_build || concurrent || partitioned)
flags |= INDEX_CREATE_SKIP_BUILD;
if (stmt->if_not_exists)
flags |= INDEX_CREATE_IF_NOT_EXISTS;
- if (stmt->concurrent)+ if (concurrent)
flags |= INDEX_CREATE_CONCURRENT;
if (partitioned)
flags |= INDEX_CREATE_PARTITIONED;
@@ -1253,7 +1275,7 @@ DefineIndex(Oid relationId,
return address;
}
- if (!stmt->concurrent)+ if (!concurrent)
{
/* Close the heap and we're done, in the non-concurrent case */
table_close(rel, NoLock);
@@ -2323,6 +2345,11 @@ 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().
+ *+ * If it's a temporary index, we will perform a non-concurrent reindex,+ * even if CONCURRENTLY was requested. In that case, reindex_index() will+ * upgrade the lock, but that's OK, because other sessions can't hold+ * locks on our temporary table.
*/
state.concurrent = concurrent;
state.locked_table_oid = InvalidOid;
@@ -2347,7 +2374,7 @@ ReindexIndex(RangeVar *indexRelation, int options, bool concurrent)
persistence = irel->rd_rel->relpersistence;
index_close(irel, NoLock);
- if (concurrent)+ if (concurrent && persistence != RELPERSISTENCE_TEMP)
ReindexRelationConcurrently(indOid, options);
else
reindex_index(indOid, false, persistence,
@@ -2432,15 +2459,32 @@ Oid
ReindexTable(RangeVar *relation, int options, bool concurrent)
{
Oid heapOid;
+ Relation heapRel;+ char persistence;
bool result;
- /* The lock level used here should match reindex_relation(). */+ /*+ * The lock level used here should match reindex_relation().+ *+ * If it's a temporary table, we will perform a non-concurrent reindex,+ * even if CONCURRENTLY was requested. In that case, reindex_relation()+ * will upgrade the lock, but that's OK, because other sessions can't hold+ * locks on our temporary table.+ */
heapOid = RangeVarGetRelidExtended(relation,
concurrent ? ShareUpdateExclusiveLock : ShareLock,
0,
RangeVarCallbackOwnsTable, NULL);
- if (concurrent)+ /*+ * Obtain the current persistence of the existing table. We already hold+ * lock on it.+ */+ heapRel = table_open(heapOid, NoLock);+ persistence = heapRel->rd_rel->relpersistence;+ table_close(heapRel, NoLock);++ if (concurrent && persistence != RELPERSISTENCE_TEMP)
{
result = ReindexRelationConcurrently(heapOid, options);
@@ -2646,7 +2690,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
/* functions in indexes may want a snapshot set */
PushActiveSnapshot(GetTransactionSnapshot());
- if (concurrent)+ if (concurrent && get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
(void) ReindexRelationConcurrently(relid, options);
/* ReindexRelationConcurrently() does the verbose output */
@@ -2694,6 +2738,12 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
*
* Returns true if any indexes have been rebuilt (including toast table's
* indexes, when relevant), otherwise returns false.
+ *+ * NOTE: This cannot be used on temporary relations. A concurrent build would+ * cause issues with ON COMMIT actions triggered by the transactions of the+ * concurrent build. Temporary relations are not subject to concurrent+ * concerns, so there's no need for the more complicated concurrent build,+ * anyway, and a non-concurrent reindex is more efficient.
*/
static bool
ReindexRelationConcurrently(Oid relationOid, int options)
@@ -2937,6 +2987,16 @@ ReindexRelationConcurrently(Oid relationOid, int options)
heapRel = table_open(indexRel->rd_index->indrelid,
ShareUpdateExclusiveLock);
+ /* This function shouldn't be called for temporary relations. */+ if (indexRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP)+ elog(ERROR, "cannot reindex a temporary table concurrently");++ /*+ * Also check for active uses of the relation in the current+ * transaction, including open scans and pending AFTER trigger events.+ */+ CheckTableNotInUse(indexRel, "REINDEX");+
pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX,
RelationGetRelid(heapRel));
pgstat_progress_update_param(PROGRESS_CREATEIDX_COMMAND,
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.outindex 6446907a65..cf1a0ca2f2 100644--- a/src/test/regress/expected/create_index.out+++ b/src/test/regress/expected/create_index.out@@ -1435,6 +1435,31 @@ Indexes:
"concur_index5" btree (f2) WHERE f1 = 'x'::text
"std_index" btree (f2)
+-- Temporary tables with concurrent builds and on-commit actions+-- CONCURRENTLY used with CREATE INDEX and DROP INDEX is ignored.+-- PRESERVE ROWS, the default.+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+DROP INDEX CONCURRENTLY concur_temp_ind;+DROP TABLE concur_temp;+-- ON COMMIT DROP+BEGIN;+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT DROP;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+-- Fails when running in a transaction.+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block+COMMIT;+-- ON COMMIT DELETE ROWS+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT DELETE ROWS;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+DROP INDEX CONCURRENTLY concur_temp_ind;+DROP TABLE concur_temp;
--
-- Try some concurrent index drops
--
@@ -2418,6 +2443,55 @@ SELECT pg_get_indexdef('concur_exprs_index_pred_2'::regclass);
(1 row)
DROP TABLE concur_exprs_tab;
+-- Temporary tables and on-commit actions, where CONCURRENTLY is ignored.+-- ON COMMIT PRESERVE ROWS, the default.+CREATE TEMP TABLE concur_temp_tab_1 (c1 int, c2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp_tab_1 VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX concur_temp_ind_1 ON concur_temp_tab_1(c2);+REINDEX TABLE CONCURRENTLY concur_temp_tab_1;+REINDEX INDEX CONCURRENTLY concur_temp_ind_1;+-- Still fails in transaction blocks+BEGIN;+REINDEX INDEX CONCURRENTLY concur_temp_ind_1;+ERROR: REINDEX CONCURRENTLY cannot run inside a transaction block+COMMIT;+-- ON COMMIT DELETE ROWS+CREATE TEMP TABLE concur_temp_tab_2 (c1 int, c2 text)+ ON COMMIT DELETE ROWS;+CREATE INDEX concur_temp_ind_2 ON concur_temp_tab_2(c2);+REINDEX TABLE CONCURRENTLY concur_temp_tab_2;+REINDEX INDEX CONCURRENTLY concur_temp_ind_2;+-- ON COMMIT DROP+BEGIN;+CREATE TEMP TABLE concur_temp_tab_3 (c1 int, c2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp_tab_3 VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX concur_temp_ind_3 ON concur_temp_tab_3(c2);+-- Fails when running in a transaction+REINDEX INDEX CONCURRENTLY concur_temp_ind_3;+ERROR: REINDEX CONCURRENTLY cannot run inside a transaction block+COMMIT;+-- REINDEX SCHEMA processes all temporary relations+CREATE TABLE reindex_temp_before AS+SELECT oid, relname, relfilenode, relkind, reltoastrelid+ FROM pg_class+ WHERE relname IN ('concur_temp_ind_1', 'concur_temp_ind_2');+SELECT pg_my_temp_schema()::regnamespace as temp_schema_name \gset+REINDEX SCHEMA :temp_schema_name;+SELECT b.relname,+ b.relkind,+ CASE WHEN a.relfilenode = b.relfilenode THEN 'relfilenode is unchanged'+ ELSE 'relfilenode has changed' END+ FROM reindex_temp_before b JOIN pg_class a ON b.oid = a.oid+ ORDER BY 1;+ relname | relkind | case +-------------------+---------+-------------------------+ concur_temp_ind_1 | i | relfilenode has changed+ concur_temp_ind_2 | i | relfilenode has changed+(2 rows)++DROP TABLE concur_temp_tab_1, concur_temp_tab_2, reindex_temp_before;
--
-- REINDEX SCHEMA
--
diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sqlindex 3c0c1cdc5e..5bc9da440e 100644--- a/src/test/regress/sql/create_index.sql+++ b/src/test/regress/sql/create_index.sql@@ -501,6 +501,31 @@ VACUUM FULL concur_heap;
REINDEX TABLE concur_heap;
\d concur_heap
+-- Temporary tables with concurrent builds and on-commit actions+-- CONCURRENTLY used with CREATE INDEX and DROP INDEX is ignored.+-- PRESERVE ROWS, the default.+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+DROP INDEX CONCURRENTLY concur_temp_ind;+DROP TABLE concur_temp;+-- ON COMMIT DROP+BEGIN;+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT DROP;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+-- Fails when running in a transaction.+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+COMMIT;+-- ON COMMIT DELETE ROWS+CREATE TEMP TABLE concur_temp (f1 int, f2 text)+ ON COMMIT DELETE ROWS;+INSERT INTO concur_temp VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX CONCURRENTLY concur_temp_ind ON concur_temp(f1);+DROP INDEX CONCURRENTLY concur_temp_ind;+DROP TABLE concur_temp;+
--
-- Try some concurrent index drops
--
@@ -972,6 +997,48 @@ SELECT pg_get_indexdef('concur_exprs_index_pred'::regclass);
SELECT pg_get_indexdef('concur_exprs_index_pred_2'::regclass);
DROP TABLE concur_exprs_tab;
+-- Temporary tables and on-commit actions, where CONCURRENTLY is ignored.+-- ON COMMIT PRESERVE ROWS, the default.+CREATE TEMP TABLE concur_temp_tab_1 (c1 int, c2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp_tab_1 VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX concur_temp_ind_1 ON concur_temp_tab_1(c2);+REINDEX TABLE CONCURRENTLY concur_temp_tab_1;+REINDEX INDEX CONCURRENTLY concur_temp_ind_1;+-- Still fails in transaction blocks+BEGIN;+REINDEX INDEX CONCURRENTLY concur_temp_ind_1;+COMMIT;+-- ON COMMIT DELETE ROWS+CREATE TEMP TABLE concur_temp_tab_2 (c1 int, c2 text)+ ON COMMIT DELETE ROWS;+CREATE INDEX concur_temp_ind_2 ON concur_temp_tab_2(c2);+REINDEX TABLE CONCURRENTLY concur_temp_tab_2;+REINDEX INDEX CONCURRENTLY concur_temp_ind_2;+-- ON COMMIT DROP+BEGIN;+CREATE TEMP TABLE concur_temp_tab_3 (c1 int, c2 text)+ ON COMMIT PRESERVE ROWS;+INSERT INTO concur_temp_tab_3 VALUES (1, 'foo'), (2, 'bar');+CREATE INDEX concur_temp_ind_3 ON concur_temp_tab_3(c2);+-- Fails when running in a transaction+REINDEX INDEX CONCURRENTLY concur_temp_ind_3;+COMMIT;+-- REINDEX SCHEMA processes all temporary relations+CREATE TABLE reindex_temp_before AS+SELECT oid, relname, relfilenode, relkind, reltoastrelid+ FROM pg_class+ WHERE relname IN ('concur_temp_ind_1', 'concur_temp_ind_2');+SELECT pg_my_temp_schema()::regnamespace as temp_schema_name \gset+REINDEX SCHEMA :temp_schema_name;+SELECT b.relname,+ b.relkind,+ CASE WHEN a.relfilenode = b.relfilenode THEN 'relfilenode is unchanged'+ ELSE 'relfilenode has changed' END+ FROM reindex_temp_before b JOIN pg_class a ON b.oid = a.oid+ ORDER BY 1;+DROP TABLE concur_temp_tab_1, concur_temp_tab_2, reindex_temp_before;+
--
-- REINDEX SCHEMA
--
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-bugs@postgresql.org
Cc: hlinnaka@iki.fi, michael@paquier.xyz, andres@anarazel.de, tgl@sss.pgh.pa.us, rigger.manuel@gmail.com
Subject: Re: REINDEX CONCURRENTLY unexpectedly fails
In-Reply-To: <39c1e415-96e6-9b91-79a8-aa1cbd25e801@iki.fi>
References: <20191115080706.GI1849@paquier.xyz> <20191120013649.2xfr2dnznyjgluch@alap3.anarazel.de> <20191120035408.GA4243@paquier.xyz> <20191212213709.neopqccvdo724eha@alap3.anarazel.de> <20191213034536.GD1942@paquier.xyz> <20191223015956.GB34339@paquier.xyz> <8f0bde7b-2d96-fb5c-cd0e-553694c1d376@iki.fi> <20200108081930.GF3413@paquier.xyz> <20200109030619.GC2251@paquier.xyz> <d62659d5-8860-075f-1e86-f17834f26495@iki.fi> <20200115013824.GA2243@paquier.xyz> <39c1e415-96e6-9b91-79a8-aa1cbd25e801@iki.fi>
* 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