Skip to content

Commit 604f54c

Browse files
committed
Some minor tweaks of REINDEX processing: grab exclusive lock a little
earlier, make error checks more uniform.
1 parent fb72628 commit 604f54c

File tree

3 files changed

+48
-20
lines changed

3 files changed

+48
-20
lines changed

src/backend/catalog/index.c

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.169 2001/11/05 17:46:24 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.170 2001/11/20 02:46:13 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1874,10 +1874,27 @@ reindex_index(Oid indexId, bool force, bool inplace)
18741874
* REINDEX within a transaction block is dangerous, because if the
18751875
* transaction is later rolled back we have no way to undo truncation
18761876
* of the index's physical file. Disallow it.
1877+
*
1878+
* XXX if we're not doing an inplace rebuild, wouldn't this be okay?
18771879
*/
18781880
if (IsTransactionBlock())
18791881
elog(ERROR, "REINDEX cannot run inside a transaction block");
18801882

1883+
/*
1884+
* Open our index relation and get an exclusive lock on it.
1885+
*
1886+
* Note: doing this before opening the parent heap relation means
1887+
* there's a possibility for deadlock failure against another xact
1888+
* that is doing normal accesses to the heap and index. However,
1889+
* it's not real clear why you'd be needing to do REINDEX on a table
1890+
* that's in active use, so I'd rather have the protection of making
1891+
* sure the index is locked down.
1892+
*/
1893+
iRel = index_open(indexId);
1894+
if (iRel == NULL)
1895+
elog(ERROR, "reindex_index: can't open index relation");
1896+
LockRelation(iRel, AccessExclusiveLock);
1897+
18811898
old = SetReindexProcessing(true);
18821899

18831900
/* Scan pg_index to find the index's pg_index entry */
@@ -1898,22 +1915,17 @@ reindex_index(Oid indexId, bool force, bool inplace)
18981915
heap_endscan(scan);
18991916
heap_close(indexRelation, AccessShareLock);
19001917

1901-
/* Open our index relation */
1918+
/* Open the parent heap relation */
19021919
heapRelation = heap_open(heapId, ExclusiveLock);
19031920
if (heapRelation == NULL)
19041921
elog(ERROR, "reindex_index: can't open heap relation");
1905-
iRel = index_open(indexId);
1906-
if (iRel == NULL)
1907-
elog(ERROR, "reindex_index: can't open index relation");
19081922

1909-
if (!inplace)
1910-
{
1911-
inplace = iRel->rd_rel->relisshared;
1912-
if (!inplace)
1913-
setNewRelfilenode(iRel);
1914-
}
1915-
/* Obtain exclusive lock on it, just to be sure */
1916-
LockRelation(iRel, AccessExclusiveLock);
1923+
/*
1924+
* Force inplace processing if it's a shared index. Necessary because
1925+
* we have no way to update relfilenode in other databases.
1926+
*/
1927+
if (iRel->rd_rel->relisshared)
1928+
inplace = true;
19171929

19181930
if (inplace)
19191931
{
@@ -1928,6 +1940,13 @@ reindex_index(Oid indexId, bool force, bool inplace)
19281940
iRel->rd_nblocks = 0;
19291941
iRel->rd_targblock = InvalidBlockNumber;
19301942
}
1943+
else
1944+
{
1945+
/*
1946+
* We'll build a new physical relation for the index.
1947+
*/
1948+
setNewRelfilenode(iRel);
1949+
}
19311950

19321951
/* Initialize the index and rebuild */
19331952
index_build(heapRelation, iRel, indexInfo);
@@ -1982,11 +2001,9 @@ reindex_relation(Oid relid, bool force)
19822001
HeapTuple indexTuple;
19832002
bool old,
19842003
reindexed;
1985-
19862004
bool deactivate_needed,
19872005
overwrite,
19882006
upd_pg_class_inplace;
1989-
19902007
Relation rel;
19912008

19922009
overwrite = upd_pg_class_inplace = deactivate_needed = false;

src/backend/commands/indexcmds.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.60 2001/10/25 05:49:25 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/indexcmds.c,v 1.61 2001/11/20 02:46:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -637,6 +637,9 @@ ReindexDatabase(const char *dbname, bool force, bool all)
637637
ALLOCSET_DEFAULT_INITSIZE,
638638
ALLOCSET_DEFAULT_MAXSIZE);
639639

640+
/*
641+
* Scan pg_class to build a list of the relations we need to reindex.
642+
*/
640643
relationRelation = heap_openr(RelationRelationName, AccessShareLock);
641644
scan = heap_beginscan(relationRelation, false, SnapshotNow, 0, NULL);
642645
relcnt = relalc = 0;
@@ -646,8 +649,6 @@ ReindexDatabase(const char *dbname, bool force, bool all)
646649
{
647650
if (!IsSystemRelationName(NameStr(((Form_pg_class) GETSTRUCT(tuple))->relname)))
648651
continue;
649-
if (((Form_pg_class) GETSTRUCT(tuple))->relhasrules)
650-
continue;
651652
}
652653
if (((Form_pg_class) GETSTRUCT(tuple))->relkind == RELKIND_RELATION)
653654
{
@@ -670,6 +671,7 @@ ReindexDatabase(const char *dbname, bool force, bool all)
670671
heap_endscan(scan);
671672
heap_close(relationRelation, AccessShareLock);
672673

674+
/* Now reindex each rel in a separate transaction */
673675
CommitTransactionCommand();
674676
for (i = 0; i < relcnt; i++)
675677
{

src/backend/tcop/utility.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.122 2001/10/25 05:49:43 momjian Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.123 2001/11/20 02:46:13 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -865,7 +865,7 @@ ProcessUtility(Node *parsetree,
865865
relname = (char *) stmt->name;
866866
if (IsSystemRelationName(relname))
867867
{
868-
if (!allowSystemTableMods && IsSystemRelationName(relname))
868+
if (!allowSystemTableMods)
869869
elog(ERROR, "\"%s\" is a system index. call REINDEX under standalone postgres with -O -P options",
870870
relname);
871871
if (!IsIgnoringSystemIndexes())
@@ -878,6 +878,15 @@ ProcessUtility(Node *parsetree,
878878
break;
879879
case TABLE:
880880
relname = (char *) stmt->name;
881+
if (IsSystemRelationName(relname))
882+
{
883+
if (!allowSystemTableMods)
884+
elog(ERROR, "\"%s\" is a system table. call REINDEX under standalone postgres with -O -P options",
885+
relname);
886+
if (!IsIgnoringSystemIndexes())
887+
elog(ERROR, "\"%s\" is a system table. call REINDEX under standalone postgres with -P -O options",
888+
relname);
889+
}
881890
if (!pg_ownercheck(GetUserId(), relname, RELNAME))
882891
elog(ERROR, "%s: %s", relname, aclcheck_error_strings[ACLCHECK_NOT_OWNER]);
883892
ReindexTable(relname, stmt->force);

0 commit comments

Comments
 (0)