8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.215 2003/09/19 19:57:42 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.216 2003/09/23 01:51:09 inoue Exp $
12
12
*
13
13
*
14
14
* INTERFACE ROUTINES
@@ -76,6 +76,7 @@ static void UpdateIndexRelation(Oid indexoid, Oid heapoid,
76
76
Oid * classOids ,
77
77
bool primary );
78
78
static Oid IndexGetRelation (Oid indexId );
79
+ static bool activate_index (Oid indexId , bool activate , bool inplace );
79
80
80
81
81
82
static bool reindexing = false;
@@ -1689,8 +1690,23 @@ IndexGetRelation(Oid indexId)
1689
1690
return result ;
1690
1691
}
1691
1692
1692
- /*
1693
+ /* ---------------------------------
1694
+ * activate_index -- activate/deactivate the specified index.
1695
+ * Note that currently PostgreSQL doesn't hold the
1696
+ * status per index
1697
+ * ---------------------------------
1698
+ */
1699
+ static bool
1700
+ activate_index (Oid indexId , bool activate , bool inplace )
1701
+ {
1702
+ if (!activate ) /* Currently does nothing */
1703
+ return true;
1704
+ return reindex_index (indexId , false, inplace );
1705
+ }
1706
+
1707
+ /* --------------------------------
1693
1708
* reindex_index - This routine is used to recreate an index
1709
+ * --------------------------------
1694
1710
*/
1695
1711
bool
1696
1712
reindex_index (Oid indexId , bool force , bool inplace )
@@ -1729,26 +1745,26 @@ reindex_index(Oid indexId, bool force, bool inplace)
1729
1745
* the relcache can't cope with changing its relfilenode.
1730
1746
*
1731
1747
* In either of these cases, we are definitely processing a system index,
1732
- * so we'd better be ignoring system indexes. (These checks are just
1733
- * for paranoia's sake --- upstream code should have disallowed reindex
1734
- * in such cases already.)
1748
+ * so we'd better be ignoring system indexes.
1735
1749
*/
1736
1750
if (iRel -> rd_rel -> relisshared )
1737
1751
{
1738
1752
if (!IsIgnoringSystemIndexes ())
1739
- elog (ERROR ,
1740
- "must be ignoring system indexes to reindex shared index %u" ,
1741
- indexId );
1753
+ ereport (ERROR ,
1754
+ ( errcode ( ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ) ,
1755
+ errmsg ( "the target relation %u is shared" , indexId )) );
1742
1756
inplace = true;
1743
1757
}
1758
+ #ifndef ENABLE_REINDEX_NAILED_RELATIONS
1744
1759
if (iRel -> rd_isnailed )
1745
1760
{
1746
1761
if (!IsIgnoringSystemIndexes ())
1747
- elog (ERROR ,
1748
- "must be ignoring system indexes to reindex nailed index %u" ,
1749
- indexId );
1762
+ ereport (ERROR ,
1763
+ ( errcode ( ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ) ,
1764
+ errmsg ( "the target relation %u is nailed" , indexId )) );
1750
1765
inplace = true;
1751
1766
}
1767
+ #endif /* ENABLE_REINDEX_NAILED_RELATIONS */
1752
1768
1753
1769
/* Fetch info needed for index_build */
1754
1770
indexInfo = BuildIndexInfo (iRel );
@@ -1787,12 +1803,13 @@ reindex_index(Oid indexId, bool force, bool inplace)
1787
1803
return true;
1788
1804
}
1789
1805
1790
- #ifdef NOT_USED
1791
1806
/*
1807
+ * ----------------------------
1792
1808
* activate_indexes_of_a_table
1793
1809
* activate/deactivate indexes of the specified table.
1794
1810
*
1795
1811
* Caller must already hold exclusive lock on the table.
1812
+ * ----------------------------
1796
1813
*/
1797
1814
bool
1798
1815
activate_indexes_of_a_table (Relation heaprel , bool activate )
@@ -1814,11 +1831,11 @@ activate_indexes_of_a_table(Relation heaprel, bool activate)
1814
1831
}
1815
1832
return true;
1816
1833
}
1817
- #endif /* NOT_USED */
1818
1834
1819
- /*
1820
- * reindex_relation - This routine is used to recreate all indexes
1835
+ /* --------------------------------
1836
+ * reindex_relation - This routine is used to recreate indexes
1821
1837
* of a relation.
1838
+ * --------------------------------
1822
1839
*/
1823
1840
bool
1824
1841
reindex_relation (Oid relid , bool force )
@@ -1829,10 +1846,11 @@ reindex_relation(Oid relid, bool force)
1829
1846
HeapTuple indexTuple ;
1830
1847
bool old ,
1831
1848
reindexed ;
1832
- bool overwrite ;
1849
+ bool deactivate_needed ,
1850
+ overwrite ;
1833
1851
Relation rel ;
1834
1852
1835
- overwrite = false;
1853
+ overwrite = deactivate_needed = false;
1836
1854
1837
1855
/*
1838
1856
* Ensure to hold an exclusive lock throughout the transaction. The
@@ -1842,50 +1860,62 @@ reindex_relation(Oid relid, bool force)
1842
1860
rel = heap_open (relid , AccessExclusiveLock );
1843
1861
1844
1862
/*
1845
- * Should be ignoring system indexes if we are reindexing a system table.
1846
- * (This is elog not ereport because caller should have caught it.)
1863
+ * ignore the indexes of the target system relation while processing
1864
+ * reindex.
1847
1865
*/
1848
1866
if (!IsIgnoringSystemIndexes () &&
1849
1867
IsSystemRelation (rel ) && !IsToastRelation (rel ))
1850
- elog (ERROR ,
1851
- "must be ignoring system indexes to reindex system table %u" ,
1852
- relid );
1868
+ deactivate_needed = true;
1853
1869
1854
1870
/*
1855
1871
* Shared system indexes must be overwritten because it's impossible
1856
1872
* to update pg_class tuples of all databases.
1857
1873
*/
1858
1874
if (rel -> rd_rel -> relisshared )
1859
1875
{
1860
- if (!IsIgnoringSystemIndexes ()) /* shouldn't happen */
1861
- elog (ERROR ,
1862
- "must be ignoring system indexes to reindex shared table %u" ,
1863
- relid );
1864
- overwrite = true;
1876
+ if (IsIgnoringSystemIndexes ())
1877
+ {
1878
+ overwrite = true;
1879
+ deactivate_needed = true;
1880
+ }
1881
+ else
1882
+ ereport (ERROR ,
1883
+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1884
+ errmsg ("the target relation %u is shared" , relid )));
1865
1885
}
1866
1886
1867
1887
old = SetReindexProcessing (true);
1868
1888
1889
+ if (deactivate_needed )
1890
+ {
1891
+ if (IndexesAreActive (rel ))
1892
+ {
1893
+ if (!force )
1894
+ {
1895
+ SetReindexProcessing (old );
1896
+ heap_close (rel , NoLock );
1897
+ return false;
1898
+ }
1899
+ activate_indexes_of_a_table (rel , false);
1900
+ CommandCounterIncrement ();
1901
+ }
1902
+ }
1903
+
1869
1904
/*
1870
1905
* Continue to hold the lock.
1871
1906
*/
1872
1907
heap_close (rel , NoLock );
1873
1908
1874
- /*
1875
- * Find table's indexes by looking in pg_index (not trusting indexes...)
1876
- */
1877
1909
indexRelation = heap_openr (IndexRelationName , AccessShareLock );
1878
- ScanKeyEntryInitialize (& entry , 0 ,
1879
- Anum_pg_index_indrelid ,
1880
- F_OIDEQ ,
1881
- ObjectIdGetDatum (relid ));
1910
+ ScanKeyEntryInitialize (& entry , 0 , Anum_pg_index_indrelid ,
1911
+ F_OIDEQ , ObjectIdGetDatum (relid ));
1882
1912
scan = heap_beginscan (indexRelation , SnapshotNow , 1 , & entry );
1883
1913
reindexed = false;
1884
1914
while ((indexTuple = heap_getnext (scan , ForwardScanDirection )) != NULL )
1885
1915
{
1886
1916
Form_pg_index index = (Form_pg_index ) GETSTRUCT (indexTuple );
1887
1917
1888
- if (reindex_index (index -> indexrelid , false , overwrite ))
1918
+ if (activate_index (index -> indexrelid , true , overwrite ))
1889
1919
reindexed = true;
1890
1920
else
1891
1921
{
@@ -1895,7 +1925,31 @@ reindex_relation(Oid relid, bool force)
1895
1925
}
1896
1926
heap_endscan (scan );
1897
1927
heap_close (indexRelation , AccessShareLock );
1928
+ if (reindexed )
1929
+ {
1930
+ /*
1931
+ * Ok,we could use the reindexed indexes of the target system
1932
+ * relation now.
1933
+ */
1934
+ if (deactivate_needed )
1935
+ {
1936
+ if (!overwrite && relid == RelOid_pg_class )
1937
+ {
1938
+ /*
1939
+ * For pg_class, relhasindex should be set to true here in
1940
+ * place.
1941
+ */
1942
+ setRelhasindex (relid , true, false, InvalidOid );
1943
+ CommandCounterIncrement ();
1898
1944
1945
+ /*
1946
+ * However the following setRelhasindex() is needed to
1947
+ * keep consistency with WAL.
1948
+ */
1949
+ }
1950
+ setRelhasindex (relid , true, false, InvalidOid );
1951
+ }
1952
+ }
1899
1953
SetReindexProcessing (old );
1900
1954
1901
1955
return reindexed ;
0 commit comments