agora inbox for pgsql-hackers@postgresql.org
help / color / mirror / Atom feed[PATCH v13 06/18] More refactoring
157+ messages / 2 participants
[nested] [flat]
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v12 5/5] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 183 +++++++++------------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 83 insertions(+), 104 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 08d44a1999..4586942960 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- int options, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, int options,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2625,8 +2625,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params->options, CurrentMemoryContext);
}
else
@@ -2961,12 +2961,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_partitions(List *inhoids, int options, List **heaprels)
+leaf_partitions(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -2998,7 +2997,6 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3010,13 +3008,11 @@ leaf_partitions(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3070,7 +3066,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_partitions(inhoids, params->options, &heaprels);
+ partitions = leaf_partitions(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3083,7 +3079,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_partitions(partindexes, params->options, &heaprels);
+ partindexes = leaf_partitions(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3092,10 +3088,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3110,7 +3105,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params->options, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params->options, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3262,7 +3257,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3315,14 +3309,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3335,7 +3321,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3386,14 +3372,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3434,70 +3412,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3521,10 +3435,8 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
return false;
}
- Assert(heapRelationIds != NIL);
-
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params->options, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params->options, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3566,9 +3478,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
+ReindexIndexesConcurrently(List *indexIds, int options,
MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3586,6 +3499,72 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds, int options,
};
int64 progress_vals[4];
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 4a03ab2abb..fc6afab58a 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--MfFXiAuoTsnnDAfZ--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v13 06/18] More refactoring
@ 2020-11-01 19:46 Justin Pryzby <pryzbyj@telsasoft.com>
0 siblings, 0 replies; 157+ messages in thread
From: Justin Pryzby @ 2020-11-01 19:46 UTC (permalink / raw)
---
src/backend/commands/indexcmds.c | 201 ++++++++++-----------
src/test/regress/expected/create_index.out | 4 +-
2 files changed, 93 insertions(+), 112 deletions(-)
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index f5fea14ff4..5c5596cd28 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -102,8 +102,8 @@ static void ReindexMultipleInternal(List *relids,
ReindexParams *params);
static bool ReindexRelationConcurrently(Oid relationOid,
ReindexParams *params);
-static List *ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context);
+static List *ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context);
static void update_relispartition(Oid relationId, bool newval);
static inline void set_indexsafe_procflags(void);
@@ -2652,8 +2652,8 @@ ReindexIndex(RangeVar *indexRelation, ReindexParams *params, bool isTopLevel)
.indexId = indOid,
/* other fields set later */
};
+
ReindexIndexesConcurrently(list_make1(&idxinfo),
- list_make1_oid(IndexGetRelation(indOid, false)),
params, CurrentMemoryContext);
}
else
@@ -3023,12 +3023,11 @@ reindex_error_callback(void *arg)
/*
- * Given a list of index oids, return a list of leaf partitions by removing
- * any intermediate parents. heaprels is populated with the corresponding
- * tables.
+ * Given a list of index oids, return a new list of leaf partitions by
+ * excluding any intermediate parents.
*/
static List *
-leaf_indexes(List *inhoids, int options, List **heaprels)
+leaf_indexes(List *inhoids, int options)
{
List *partitions = NIL;
ListCell *lc;
@@ -3060,7 +3059,6 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
/* Save partition OID in current MemoryContext */
partitions = lappend_oid(partitions, partoid);
- *heaprels = lappend_oid(*heaprels, tableoid);
}
return partitions;
@@ -3072,13 +3070,11 @@ leaf_indexes(List *inhoids, int options, List **heaprels)
*
* Reindex a set of partitions, per the partitioned index or table given
* by the caller.
- * XXX: should be further refactored with logic from ReindexRelationConcurrently
*/
static void
ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
{
- List *partitions = NIL,
- *heaprels = NIL;
+ List *partitions = NIL;
char relkind = get_rel_relkind(relid);
char *relname = get_rel_name(relid);
char *relnamespace = get_namespace_name(get_rel_namespace(relid));
@@ -3132,7 +3128,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
if (relkind == RELKIND_PARTITIONED_INDEX)
{
old_context = MemoryContextSwitchTo(reindex_context);
- partitions = leaf_indexes(inhoids, params->options, &heaprels);
+ partitions = leaf_indexes(inhoids, params->options);
MemoryContextSwitchTo(old_context);
} else {
/* Loop over parent tables */
@@ -3145,7 +3141,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
parttable = table_open(partoid, ShareLock);
old_context = MemoryContextSwitchTo(reindex_context);
partindexes = RelationGetIndexList(parttable);
- partindexes = leaf_indexes(partindexes, params->options, &heaprels);
+ partindexes = leaf_indexes(partindexes, params->options);
partitions = list_concat(partitions, partindexes);
MemoryContextSwitchTo(old_context);
@@ -3154,10 +3150,9 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
}
if ((params->options & REINDEXOPT_CONCURRENTLY) != 0 &&
- relkind == RELKIND_PARTITIONED_INDEX &&
get_rel_persistence(relid) != RELPERSISTENCE_TEMP)
{
- List *idxinfos = NIL;
+ List *idxinfos = NIL;
ReindexIndexInfo *idxinfo;
old_context = MemoryContextSwitchTo(reindex_context);
@@ -3172,7 +3167,7 @@ ReindexPartitions(Oid relid, ReindexParams *params, bool isTopLevel)
MemoryContextSwitchTo(old_context);
/* Process all indexes in a single loop */
- ReindexIndexesConcurrently(idxinfos, heaprels, params, reindex_context);
+ ReindexIndexesConcurrently(idxinfos, params, reindex_context);
} else {
/*
* Process each partition listed in a separate transaction. Note that
@@ -3342,7 +3337,6 @@ ReindexMultipleInternal(List *relids, ReindexParams *params)
static bool
ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
{
- List *heapRelationIds = NIL;
List *indexIds = NIL;
List *newIndexIds = NIL;
ListCell *lc,
@@ -3395,14 +3389,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
*/
Relation heapRelation;
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, relationOid);
-
- MemoryContextSwitchTo(oldcontext);
-
if (IsCatalogRelationOid(relationOid))
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -3415,7 +3401,7 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
ShareUpdateExclusiveLock);
/* leave if relation does not exist */
if (!heapRelation)
- break;
+ break; // XXX: lremove
}
else
heapRelation = table_open(relationOid,
@@ -3473,14 +3459,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
Relation toastRelation = table_open(toastOid,
ShareUpdateExclusiveLock);
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track this relation for session locks */
- heapRelationIds = lappend_oid(heapRelationIds, toastOid);
-
- MemoryContextSwitchTo(oldcontext);
-
foreach(lc2, RelationGetIndexList(toastRelation))
{
Oid cellOid = lfirst_oid(lc2);
@@ -3521,78 +3499,6 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
break;
}
case RELKIND_INDEX:
- {
- Oid heapId = IndexGetRelation(relationOid,
- (params->options & REINDEXOPT_MISSING_OK) != 0);
- Relation heapRelation;
- ReindexIndexInfo *idx;
-
- /* if relation is missing, leave */
- if (!OidIsValid(heapId))
- break;
-
- if (IsCatalogRelationOid(heapId))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex system catalogs concurrently")));
-
- /*
- * Don't allow reindex for an invalid index on TOAST table, as
- * if rebuilt it would not be possible to drop it. Match
- * error message in reindex_index().
- */
- if (IsToastNamespace(get_rel_namespace(relationOid)) &&
- !get_index_isvalid(relationOid))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot reindex invalid index on TOAST table")));
-
- /*
- * Check if parent relation can be locked and if it exists,
- * this needs to be done at this stage as the list of indexes
- * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
- * should not be used once all the session locks are taken.
- */
- if ((params->options & REINDEXOPT_MISSING_OK) != 0)
- {
- heapRelation = try_table_open(heapId,
- ShareUpdateExclusiveLock);
- /* leave if relation does not exist */
- if (!heapRelation)
- break;
- }
- else
- heapRelation = table_open(heapId,
- ShareUpdateExclusiveLock);
-
- if (OidIsValid(params->tablespaceOid) &&
- IsSystemRelation(heapRelation))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot move system relation \"%s\"",
- get_rel_name(relationOid))));
-
- table_close(heapRelation, NoLock);
-
- /* Save the list of relation OIDs in private context */
- oldcontext = MemoryContextSwitchTo(private_context);
-
- /* Track the heap relation of this index for session locks */
- heapRelationIds = list_make1_oid(heapId);
-
- /*
- * Save the list of relation OIDs in private context. Note
- * that invalid indexes are allowed here.
- */
- idx = palloc(sizeof(ReindexIndexInfo));
- idx->indexId = relationOid;
- indexIds = lappend(indexIds, idx);
- /* other fields set later */
-
- MemoryContextSwitchTo(oldcontext);
- break;
- }
-
case RELKIND_PARTITIONED_TABLE:
case RELKIND_PARTITIONED_INDEX:
default:
@@ -3623,10 +3529,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
errmsg("cannot move non-shared relation to tablespace \"%s\"",
get_tablespace_name(params->tablespaceOid))));
- Assert(heapRelationIds != NIL);
+ // Assert(heapRelationIds != NIL);
/* Do the work */
- newIndexIds = ReindexIndexesConcurrently(indexIds, heapRelationIds, params, private_context);
+ newIndexIds = ReindexIndexesConcurrently(indexIds, params, private_context);
/* Log what we did */
if ((params->options & REINDEXOPT_VERBOSE) != 0)
@@ -3668,9 +3574,10 @@ ReindexRelationConcurrently(Oid relationOid, ReindexParams *params)
* This is called by ReindexRelationConcurrently and
*/
static List *
-ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
- ReindexParams *params, MemoryContext private_context)
+ReindexIndexesConcurrently(List *indexIds, ReindexParams *params,
+ MemoryContext private_context)
{
+ List *heapRelationIds = NIL;
List *newIndexIds = NIL;
List *relationLocks = NIL;
List *lockTags = NIL;
@@ -3688,6 +3595,80 @@ ReindexIndexesConcurrently(List *indexIds, List *heapRelationIds,
};
int64 progress_vals[4];
+ /* It's not a shared catalog, so refuse to move it to shared tablespace */
+ if (params->tablespaceOid == GLOBALTABLESPACE_OID && false)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move non-shared relation to tablespace \"%s\"",
+ get_tablespace_name(params->tablespaceOid))));
+
+ foreach(lc, indexIds)
+ {
+ ReindexIndexInfo *idx = lfirst(lc);
+ Oid indexrelid = idx->indexId;
+ Oid heapId = IndexGetRelation(indexrelid,
+ (params->options & REINDEXOPT_MISSING_OK) != 0);
+ Relation heapRelation;
+
+ /* if relation is missing, leave */
+ if (!OidIsValid(heapId))
+ break; // XXX: ldelete?
+
+ if (IsCatalogRelationOid(heapId))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex system catalogs concurrently")));
+
+ /*
+ * Don't allow reindex for an invalid index on TOAST table, as
+ * if rebuilt it would not be possible to drop it. Match
+ * error message in reindex_index().
+ */
+ if (IsToastNamespace(get_rel_namespace(indexrelid)) &&
+ !get_index_isvalid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot reindex invalid index on TOAST table")));
+
+ if (OidIsValid(params->tablespaceOid) &&
+ IsCatalogRelationOid(indexrelid))
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("cannot move system relation \"%s\"",
+ get_rel_name(indexrelid))));
+
+ /*
+ * Check if parent relation can be locked and if it exists,
+ * this needs to be done at this stage as the list of indexes
+ * to rebuild is not complete yet, and REINDEXOPT_MISSING_OK
+ * should not be used once all the session locks are taken.
+ */
+ if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+ {
+ heapRelation = try_table_open(heapId,
+ ShareUpdateExclusiveLock);
+ /* leave if relation does not exist */
+ if (!heapRelation)
+ break; // ldelete
+ }
+ else
+ heapRelation = table_open(heapId,
+ ShareUpdateExclusiveLock);
+ table_close(heapRelation, NoLock);
+
+ /* Save the list of relation OIDs in private context */
+ oldcontext = MemoryContextSwitchTo(private_context);
+
+ /* Track the heap relation of this index for session locks */
+ heapRelationIds = lappend_oid(heapRelationIds, heapId);
+ // heapRelationIds = list_make1_oid(heapId);
+
+ /* Note that invalid indexes are allowed here. */
+
+ MemoryContextSwitchTo(oldcontext);
+ // break;
+ }
+
/*-----
* Now we have all the indexes we want to process in indexIds.
*
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index 6f41adf736..830fdddf24 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2470,12 +2470,12 @@ COMMIT;
REINDEX TABLE CONCURRENTLY pg_class; -- no catalog relation
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_class_oid_index; -- no catalog index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
-- These are the toast table and index of pg_authid.
REINDEX TABLE CONCURRENTLY pg_toast.pg_toast_1260; -- no catalog toast table
ERROR: cannot reindex system catalogs concurrently
REINDEX INDEX CONCURRENTLY pg_toast.pg_toast_1260_index; -- no catalog toast index
-ERROR: concurrent index creation on system catalog tables is not supported
+ERROR: cannot reindex system catalogs concurrently
REINDEX SYSTEM CONCURRENTLY postgres; -- not allowed for SYSTEM
ERROR: cannot reindex system catalogs concurrently
-- Warns about catalog relations
--
2.17.0
--QTprm0S8XgL7H0Dt--
^ permalink raw reply [nested|flat] 157+ messages in thread
* [PATCH v1] Use open file description locks for data directory lockfile
@ 2025-12-18 17:21 Dmitrii Dolgov <9erthalion6@gmail.com>
0 siblings, 0 replies; 157+ messages in thread
From: Dmitrii Dolgov @ 2025-12-18 17:21 UTC (permalink / raw)
When starting up, postmaster checks for an existing data directory lockfile. If
this file contains current process PID, it's assumed to be stale. Turns out
there is another possibility: we might be running in a PID namespace, and there
is another postgres running inside another PID namespace using the same data
directory. The result is that we don't see another process due to namespace
isolation and start concurrently with the other.
To prevent such situations, at startup use fcntl to get an exclusive open file
description lock for data directory lockfile. Since such locks are associated
with open file descriptors, meaning they're not affected by PID namespace
isolation. It's a "best effort" locking, intended to work with already existing
mechanism, not replace it.
This approach was discussed multiple times in the past, and usually was
rejected as the main work horse for the data directory lockfile due to:
* Portability issues. Open file description lock was a non-POSIX extension in
Linux and similar flock is from BSD standard. But looks like everybody agrees
that such locks make more sense than a typical advisory locks, and
F_OFD_SETLK made its way into POSIX.1 2024 [1].
* Issues with NFS. The current state of things here looks like this:
- NFSv3 doesn't implement open file description locks, they're converted to
advisory locks instead. Advisory locks are subject to namespace isolation,
meaning that processes in different PID namespaces will not see each other
advisory lock, and it's still possible to run multiple postgres
instances on the same data directory.
- NFSv4 uses a lease system for locking, I haven't found any mention of
conversion to advisory locks neither in the man page nor in RFC [2].
To summarize, the approach is now considered POSIX and should fix the described
problem everywhere, except NFSv3.
[1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/fcntl.html
[2]: https://www.rfc-editor.org/rfc/rfc7530
---
configure | 14 ++++
configure.ac | 3 +
meson.build | 1 +
src/backend/utils/init/miscinit.c | 107 +++++++++++++++++++++++++-----
src/include/pg_config.h.in | 4 ++
5 files changed, 111 insertions(+), 18 deletions(-)
diff --git a/configure b/configure
index 14ad0a5006f..b176ac39799 100755
--- a/configure
+++ b/configure
@@ -16177,6 +16177,20 @@ cat >>confdefs.h <<_ACEOF
_ACEOF
+# Linux open file descriptor locks
+ac_fn_c_check_decl "$LINENO" "F_OFD_SETLK" "ac_cv_have_decl_F_OFD_SETLK" "#include <fcntl.h>
+"
+if test "x$ac_cv_have_decl_F_OFD_SETLK" = xyes; then :
+ ac_have_decl=1
+else
+ ac_have_decl=0
+fi
+
+cat >>confdefs.h <<_ACEOF
+#define HAVE_DECL_F_OFD_SETLK $ac_have_decl
+_ACEOF
+
+
ac_fn_c_check_func "$LINENO" "explicit_bzero" "ac_cv_func_explicit_bzero"
if test "x$ac_cv_func_explicit_bzero" = xyes; then :
$as_echo "#define HAVE_EXPLICIT_BZERO 1" >>confdefs.h
diff --git a/configure.ac b/configure.ac
index 01b3bbc1be8..d6cf1f27771 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1838,6 +1838,9 @@ AC_CHECK_DECLS([memset_s], [], [], [#define __STDC_WANT_LIB_EXT1__ 1
# This is probably only present on macOS, but may as well check always
AC_CHECK_DECLS(F_FULLFSYNC, [], [], [#include <fcntl.h>])
+# Linux open file descriptor locks
+AC_CHECK_DECLS([F_OFD_SETLK], [], [], [#include <fcntl.h>])
+
AC_REPLACE_FUNCS(m4_normalize([
explicit_bzero
getopt
diff --git a/meson.build b/meson.build
index d7c5193d4ce..ad9ecad829f 100644
--- a/meson.build
+++ b/meson.build
@@ -2667,6 +2667,7 @@ decl_checks = [
['strnlen', 'string.h'],
['strsep', 'string.h'],
['timingsafe_bcmp', 'string.h'],
+ ['F_OFD_SETLK', 'fcntl.h'],
]
# Need to check for function declarations for these functions, because
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index fec79992c8d..78bb7df543e 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -68,6 +68,11 @@ static List *lock_files = NIL;
static Latch LocalLatchData;
+#ifdef HAVE_DECL_F_OFD_SETLK
+/* File descriptor for data directory lock file. */
+static int DataDirLockFD;
+#endif
+
/* ----------------------------------------------------------------
* ignoring system indexes support stuff
*
@@ -1117,6 +1122,45 @@ RestoreClientConnectionInfo(char *conninfo)
*-------------------------------------------------------------------------
*/
+/*
+ * Flock the data directory lockfile.
+ *
+ * Lock the data directory lockfile with an open file description lock. If the
+ * lock is already taken, it's a hard stop. It's only a best effort test, and
+ * any other errors are ignored. On succes the file descriptor is duplicated,
+ * to make sure there will be at least one open copy of it to keep the lock.
+ *
+ * filename is used only for reporting purposes.
+ */
+static void
+FlockDataDirLockFile(int fd, const char *filename)
+{
+
+#ifdef HAVE_DECL_F_OFD_SETLK
+ struct flock lock;
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 0;
+ lock.l_pid = 0;
+
+ if (fcntl(fd, F_OFD_SETLK, &lock) == -1)
+ {
+ if (errno == EAGAIN)
+ ereport(FATAL,
+ (errcode(ERRCODE_LOCK_FILE_EXISTS),
+ errmsg("cannot lock the lock file \"%s\"", filename),
+ errhint("Another server is starting.")));
+ else
+ elog(WARNING, "Failed locking file \"%s\", %m", filename);
+ }
+ else
+ DataDirLockFD = dup(fd);
+#endif
+
+}
+
/*
* proc_exit callback to remove lockfiles.
*/
@@ -1125,6 +1169,11 @@ UnlinkLockFiles(int status, Datum arg)
{
ListCell *l;
+#ifdef HAVE_DECL_F_OFD_SETLK
+ /* Close the file descriptor, which keeps the open file description lock */
+ close(DataDirLockFD);
+#endif
+
foreach(l, lock_files)
{
char *curfile = (char *) lfirst(l);
@@ -1171,22 +1220,32 @@ CreateLockFile(const char *filename, bool amPostmaster,
const char *envvar;
/*
- * If the PID in the lockfile is our own PID or our parent's or
- * grandparent's PID, then the file must be stale (probably left over from
- * a previous system boot cycle). We need to check this because of the
- * likelihood that a reboot will assign exactly the same PID as we had in
- * the previous reboot, or one that's only one or two counts larger and
- * hence the lockfile's PID now refers to an ancestor shell process. We
- * allow pg_ctl to pass down its parent shell PID (our grandparent PID)
- * via the environment variable PG_GRANDPARENT_PID; this is so that
- * launching the postmaster via pg_ctl can be just as reliable as
- * launching it directly. There is no provision for detecting
- * further-removed ancestor processes, but if the init script is written
- * carefully then all but the immediate parent shell will be root-owned
- * processes and so the kill test will fail with EPERM. Note that we
- * cannot get a false negative this way, because an existing postmaster
- * would surely never launch a competing postmaster or pg_ctl process
- * directly.
+ * If we find an already existing lockfile containing our own PID,
+ * there are few options:
+ *
+ * - There is another process, that we don't see due to PID namespace
+ * isolation, which is already running in this data directory.
+ *
+ * To prevent two concurrent processes working with the same data
+ * directory, we first try to lock the lockfile exclusively.
+ *
+ * - The file must be stale, probably left over from a previous system boot
+ * cycle. The same if the lockfile contains our parent's or grandparent's
+ * PID.
+ *
+ * We need to check this because of the likelihood that a reboot will
+ * assign exactly the same PID as we had in the previous reboot, or one
+ * that's only one or two counts larger and hence the lockfile's PID now
+ * refers to an ancestor shell process. We allow pg_ctl to pass down its
+ * parent shell PID (our grandparent PID) via the environment variable
+ * PG_GRANDPARENT_PID; this is so that launching the postmaster via
+ * pg_ctl can be just as reliable as launching it directly. There is no
+ * provision for detecting further-removed ancestor processes, but if the
+ * init script is written carefully then all but the immediate parent
+ * shell will be root-owned processes and so the kill test will fail with
+ * EPERM. Note that we cannot get a false negative this way, because an
+ * existing postmaster would surely never launch a competing postmaster
+ * or pg_ctl process directly.
*/
my_pid = getpid();
@@ -1222,7 +1281,11 @@ CreateLockFile(const char *filename, bool amPostmaster,
*/
fd = open(filename, O_RDWR | O_CREAT | O_EXCL, pg_file_create_mode);
if (fd >= 0)
- break; /* Success; exit the retry loop */
+ {
+ /* Success; lock and exit the retry loop */
+ FlockDataDirLockFile(fd, filename);
+ break;
+ }
/*
* Couldn't create the pid file. Probably it already exists.
@@ -1236,8 +1299,12 @@ CreateLockFile(const char *filename, bool amPostmaster,
/*
* Read the file to get the old owner's PID. Note race condition
* here: file might have been deleted since we tried to create it.
+ *
+ * We're going to use the same fd for flock, and want to create a write
+ * lock for the latter one. Since both fd and the lock have to be of
+ * the same type, open the file for read and write.
*/
- fd = open(filename, O_RDONLY, pg_file_create_mode);
+ fd = open(filename, O_RDWR, pg_file_create_mode);
if (fd < 0)
{
if (errno == ENOENT)
@@ -1247,6 +1314,10 @@ CreateLockFile(const char *filename, bool amPostmaster,
errmsg("could not open lock file \"%s\": %m",
filename)));
}
+
+ /* Try to lock the file. We stop here, if it's already locked. */
+ FlockDataDirLockFile(fd, filename);
+
pgstat_report_wait_start(WAIT_EVENT_LOCK_FILE_CREATE_READ);
if ((len = read(fd, buffer, sizeof(buffer) - 1)) < 0)
ereport(FATAL,
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 92fcc5f3063..c19a50f108e 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -83,6 +83,10 @@
don't. */
#undef HAVE_DECL_F_FULLFSYNC
+/* Define to 1 if you have the declaration of `F_OFD_SETLK', and to 0 if you
+ don't. */
+#undef HAVE_DECL_F_OFD_SETLK
+
/* Define to 1 if you have the declaration of
`LLVMCreateGDBRegistrationListener', and to 0 if you don't. */
#undef HAVE_DECL_LLVMCREATEGDBREGISTRATIONLISTENER
base-commit: 44f49511b7940adf3be4337d4feb2de38fe92297
--
2.49.0
--n6t6hie3zne7u6vd--
^ permalink raw reply [nested|flat] 157+ messages in thread
end of thread, other threads:[~2025-12-18 17:21 UTC | newest]
Thread overview: 157+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v13 06/18] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2020-11-01 19:46 [PATCH v12 5/5] More refactoring Justin Pryzby <pryzbyj@telsasoft.com>
2025-12-18 17:21 [PATCH v1] Use open file description locks for data directory lockfile Dmitrii Dolgov <9erthalion6@gmail.com>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox