8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
@@ -1874,10 +1874,27 @@ reindex_index(Oid indexId, bool force, bool inplace)
1874
1874
* REINDEX within a transaction block is dangerous, because if the
1875
1875
* transaction is later rolled back we have no way to undo truncation
1876
1876
* of the index's physical file. Disallow it.
1877
+ *
1878
+ * XXX if we're not doing an inplace rebuild, wouldn't this be okay?
1877
1879
*/
1878
1880
if (IsTransactionBlock ())
1879
1881
elog (ERROR , "REINDEX cannot run inside a transaction block" );
1880
1882
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
+
1881
1898
old = SetReindexProcessing (true);
1882
1899
1883
1900
/* Scan pg_index to find the index's pg_index entry */
@@ -1898,22 +1915,17 @@ reindex_index(Oid indexId, bool force, bool inplace)
1898
1915
heap_endscan (scan );
1899
1916
heap_close (indexRelation , AccessShareLock );
1900
1917
1901
- /* Open our index relation */
1918
+ /* Open the parent heap relation */
1902
1919
heapRelation = heap_open (heapId , ExclusiveLock );
1903
1920
if (heapRelation == NULL )
1904
1921
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" );
1908
1922
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;
1917
1929
1918
1930
if (inplace )
1919
1931
{
@@ -1928,6 +1940,13 @@ reindex_index(Oid indexId, bool force, bool inplace)
1928
1940
iRel -> rd_nblocks = 0 ;
1929
1941
iRel -> rd_targblock = InvalidBlockNumber ;
1930
1942
}
1943
+ else
1944
+ {
1945
+ /*
1946
+ * We'll build a new physical relation for the index.
1947
+ */
1948
+ setNewRelfilenode (iRel );
1949
+ }
1931
1950
1932
1951
/* Initialize the index and rebuild */
1933
1952
index_build (heapRelation , iRel , indexInfo );
@@ -1982,11 +2001,9 @@ reindex_relation(Oid relid, bool force)
1982
2001
HeapTuple indexTuple ;
1983
2002
bool old ,
1984
2003
reindexed ;
1985
-
1986
2004
bool deactivate_needed ,
1987
2005
overwrite ,
1988
2006
upd_pg_class_inplace ;
1989
-
1990
2007
Relation rel ;
1991
2008
1992
2009
overwrite = upd_pg_class_inplace = deactivate_needed = false;
0 commit comments