Skip to content

Commit 90c35a9

Browse files
committed
Code cleanup for REINDEX DATABASE/SCHEMA/SYSTEM.
Fix some minor infelicities. Some of these things were introduced in commit fe263d1, and some are older.
1 parent ac09142 commit 90c35a9

File tree

3 files changed

+34
-56
lines changed

3 files changed

+34
-56
lines changed

src/backend/commands/indexcmds.c

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,37 +1780,37 @@ ReindexTable(RangeVar *relation)
17801780
}
17811781

17821782
/*
1783-
* ReindexObject
1784-
* Recreate indexes of object whose type is defined by objectKind.
1783+
* ReindexMultipleTables
1784+
* Recreate indexes of tables selected by objectName/objectKind.
17851785
*
17861786
* To reduce the probability of deadlocks, each table is reindexed in a
17871787
* separate transaction, so we can release the lock on it right away.
17881788
* That means this must not be called within a user transaction block!
17891789
*/
1790-
Oid
1791-
ReindexObject(const char *objectName, ReindexObjectType objectKind)
1790+
void
1791+
ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind)
17921792
{
17931793
Oid objectOid;
17941794
Relation relationRelation;
17951795
HeapScanDesc scan;
1796-
ScanKeyData *scan_keys = NULL;
1796+
ScanKeyData scan_keys[1];
17971797
HeapTuple tuple;
17981798
MemoryContext private_context;
17991799
MemoryContext old;
18001800
List *relids = NIL;
18011801
ListCell *l;
1802-
int num_keys;
1802+
int num_keys;
18031803

18041804
AssertArg(objectName);
18051805
Assert(objectKind == REINDEX_OBJECT_SCHEMA ||
18061806
objectKind == REINDEX_OBJECT_SYSTEM ||
18071807
objectKind == REINDEX_OBJECT_DATABASE);
18081808

18091809
/*
1810-
* Get OID of object to reindex, being the database currently being
1811-
* used by session for a database or for system catalogs, or the schema
1812-
* defined by caller. At the same time do permission checks that need
1813-
* different processing depending on the object type.
1810+
* Get OID of object to reindex, being the database currently being used
1811+
* by session for a database or for system catalogs, or the schema defined
1812+
* by caller. At the same time do permission checks that need different
1813+
* processing depending on the object type.
18141814
*/
18151815
if (objectKind == REINDEX_OBJECT_SCHEMA)
18161816
{
@@ -1824,11 +1824,11 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
18241824
{
18251825
objectOid = MyDatabaseId;
18261826

1827-
if (strcmp(objectName, get_database_name(MyDatabaseId)) != 0)
1827+
if (strcmp(objectName, get_database_name(objectOid)) != 0)
18281828
ereport(ERROR,
18291829
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
18301830
errmsg("can only reindex the currently open database")));
1831-
if (!pg_database_ownercheck(MyDatabaseId, GetUserId()))
1831+
if (!pg_database_ownercheck(objectOid, GetUserId()))
18321832
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE,
18331833
objectName);
18341834
}
@@ -1840,42 +1840,19 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
18401840
* abort cleanup logic.
18411841
*/
18421842
private_context = AllocSetContextCreate(PortalContext,
1843-
(objectKind == REINDEX_OBJECT_SCHEMA) ?
1844-
"ReindexSchema" : "ReindexDatabase",
1843+
"ReindexMultipleTables",
18451844
ALLOCSET_DEFAULT_MINSIZE,
18461845
ALLOCSET_DEFAULT_INITSIZE,
18471846
ALLOCSET_DEFAULT_MAXSIZE);
18481847

18491848
/*
1850-
* We always want to reindex pg_class first when reindexing system
1851-
* catalogs or a database. This ensures that if there is any corruption
1852-
* in pg_class' indexes, they will be fixed before we process any other
1853-
* tables. This is critical because reindexing itself will try to
1854-
* update pg_class.
1855-
*/
1856-
if (objectKind == REINDEX_OBJECT_DATABASE ||
1857-
objectKind == REINDEX_OBJECT_SYSTEM ||
1858-
(objectKind == REINDEX_OBJECT_SCHEMA &&
1859-
IsSystemNamespace(objectOid)))
1860-
{
1861-
old = MemoryContextSwitchTo(private_context);
1862-
relids = lappend_oid(relids, RelationRelationId);
1863-
MemoryContextSwitchTo(old);
1864-
}
1865-
1866-
/*
1867-
* Define the search keys to find the objects to reindex. For a schema,
1868-
* we search target relations using relnamespace and relkind, something
1869-
* not necessary for a database-wide operation.
1849+
* Define the search keys to find the objects to reindex. For a schema, we
1850+
* select target relations using relnamespace, something not necessary for
1851+
* a database-wide operation.
18701852
*/
18711853
if (objectKind == REINDEX_OBJECT_SCHEMA)
18721854
{
1873-
/*
1874-
* Return all objects in schema. We filter out
1875-
* inappropriate objects as we walk through results.
1876-
*/
18771855
num_keys = 1;
1878-
scan_keys = palloc(sizeof(ScanKeyData));
18791856
ScanKeyInit(&scan_keys[0],
18801857
Anum_pg_class_relnamespace,
18811858
BTEqualStrategyNumber, F_OIDEQ,
@@ -1898,8 +1875,8 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
18981875
Oid relid = HeapTupleGetOid(tuple);
18991876

19001877
/*
1901-
* Only regular tables and matviews can have indexes,
1902-
* so filter out any other kind of object.
1878+
* Only regular tables and matviews can have indexes, so ignore any
1879+
* other kind of relation.
19031880
*/
19041881
if (classtuple->relkind != RELKIND_RELATION &&
19051882
classtuple->relkind != RELKIND_MATVIEW)
@@ -1911,20 +1888,25 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
19111888
continue;
19121889

19131890
/* Check user/system classification, and optionally skip */
1914-
if (!IsSystemClass(relid, classtuple) &&
1915-
objectKind == REINDEX_OBJECT_SYSTEM)
1891+
if (objectKind == REINDEX_OBJECT_SYSTEM &&
1892+
!IsSystemClass(relid, classtuple))
19161893
continue;
19171894

1895+
/* Save the list of relation OIDs in private context */
1896+
old = MemoryContextSwitchTo(private_context);
1897+
19181898
/*
1919-
* Already have it in the case of system catalogs being all
1920-
* reindexed, of a database or of a system catalog being reindexed
1921-
* as a schema.
1899+
* We always want to reindex pg_class first if it's selected to be
1900+
* reindexed. This ensures that if there is any corruption in
1901+
* pg_class' indexes, they will be fixed before we process any other
1902+
* tables. This is critical because reindexing itself will try to
1903+
* update pg_class.
19221904
*/
1923-
if (HeapTupleGetOid(tuple) == RelationRelationId)
1924-
continue;
1905+
if (relid == RelationRelationId)
1906+
relids = lcons_oid(relid, relids);
1907+
else
1908+
relids = lappend_oid(relids, relid);
19251909

1926-
old = MemoryContextSwitchTo(private_context);
1927-
relids = lappend_oid(relids, relid);
19281910
MemoryContextSwitchTo(old);
19291911
}
19301912
heap_endscan(scan);
@@ -1953,8 +1935,4 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
19531935
StartTransactionCommand();
19541936

19551937
MemoryContextDelete(private_context);
1956-
if (scan_keys)
1957-
pfree(scan_keys);
1958-
1959-
return objectOid;
19601938
}

src/backend/tcop/utility.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ standard_ProcessUtility(Node *parsetree,
756756
(stmt->kind == REINDEX_OBJECT_SCHEMA) ? "REINDEX SCHEMA" :
757757
(stmt->kind == REINDEX_OBJECT_SYSTEM) ? "REINDEX SYSTEM" :
758758
"REINDEX DATABASE");
759-
ReindexObject(stmt->name, stmt->kind);
759+
ReindexMultipleTables(stmt->name, stmt->kind);
760760
break;
761761
default:
762762
elog(ERROR, "unrecognized object type: %d",

src/include/commands/defrem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern ObjectAddress DefineIndex(Oid relationId,
3131
bool quiet);
3232
extern Oid ReindexIndex(RangeVar *indexRelation);
3333
extern Oid ReindexTable(RangeVar *relation);
34-
extern Oid ReindexObject(const char *databaseName, ReindexObjectType kind);
34+
extern void ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind);
3535
extern char *makeObjectName(const char *name1, const char *name2,
3636
const char *label);
3737
extern char *ChooseRelationName(const char *name1, const char *name2,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy