77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.110 1999/11/24 00:44:29 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/backend/catalog/heap.c,v 1.111 1999/11/28 02:03:04 tgl Exp $
1111 *
1212 *
1313 * INTERFACE ROUTINES
4444#include "catalog/pg_ipl.h"
4545#include "catalog/pg_proc.h"
4646#include "catalog/pg_relcheck.h"
47+ #include "catalog/pg_statistic.h"
4748#include "catalog/pg_type.h"
4849#include "commands/comment.h"
4950#include "commands/trigger.h"
@@ -82,6 +83,7 @@ static void StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin,
8283static void StoreRelCheck (Relation rel ,char * ccname ,char * ccbin );
8384static void StoreConstraints (Relation rel );
8485static void RemoveConstraints (Relation rel );
86+ static void RemoveStatistics (Relation rel );
8587
8688
8789/* ----------------------------------------------------------------
@@ -1232,7 +1234,7 @@ heap_truncate(char *relname)
12321234/* Open relation for processing, and grab exclusive access on it. */
12331235
12341236rel = heap_openr (relname ,AccessExclusiveLock );
1235- rid = rel -> rd_id ;
1237+ rid = RelationGetRelid ( rel ) ;
12361238
12371239/* ----------------
12381240 *TRUNCATE TABLE within a transaction block is dangerous, because
@@ -1455,7 +1457,7 @@ heap_destroy_with_catalog(char *relname)
14551457 * ----------------
14561458 */
14571459rel = heap_openr (relname ,AccessExclusiveLock );
1458- rid = rel -> rd_id ;
1460+ rid = RelationGetRelid ( rel ) ;
14591461
14601462/* ----------------
14611463 *prevent deletion of system relations
@@ -1512,12 +1514,17 @@ heap_destroy_with_catalog(char *relname)
15121514DeleteAttributeTuples (rel );
15131515
15141516/* ----------------
1515- * delete comments
1516- * ----------------
1517- */
1518-
1517+ *delete comments
1518+ * ----------------
1519+ */
15191520DeleteComments (RelationGetRelid (rel ));
15201521
1522+ /* ----------------
1523+ *delete statistics
1524+ * ----------------
1525+ */
1526+ RemoveStatistics (rel );
1527+
15211528/* ----------------
15221529 *delete type tuple.here we want to see the effects
15231530 *of the deletions we just did, so we use setheapoverride().
@@ -1731,7 +1738,7 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin,
17311738rte -> skipAcl = false;
17321739adsrc = deparse_expression (expr ,lcons (lcons (rte ,NIL ),NIL ), false);
17331740
1734- values [Anum_pg_attrdef_adrelid - 1 ]= rel -> rd_id ;
1741+ values [Anum_pg_attrdef_adrelid - 1 ]= RelationGetRelid ( rel ) ;
17351742values [Anum_pg_attrdef_adnum - 1 ]= attnum ;
17361743values [Anum_pg_attrdef_adbin - 1 ]= PointerGetDatum (textin (adbin ));
17371744values [Anum_pg_attrdef_adsrc - 1 ]= PointerGetDatum (textin (adsrc ));
@@ -1754,11 +1761,11 @@ StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin,
17541761
17551762attrrel = heap_openr (AttributeRelationName ,RowExclusiveLock );
17561763atttup = SearchSysCacheTupleCopy (ATTNUM ,
1757- ObjectIdGetDatum (rel -> rd_id ),
1764+ ObjectIdGetDatum (RelationGetRelid ( rel ) ),
17581765 (Datum )attnum ,0 ,0 );
17591766if (!HeapTupleIsValid (atttup ))
17601767elog (ERROR ,"cache lookup of attribute %d in relation %u failed" ,
1761- attnum ,rel -> rd_id );
1768+ attnum ,RelationGetRelid ( rel ) );
17621769attStruct = (Form_pg_attribute )GETSTRUCT (atttup );
17631770if (!attStruct -> atthasdef )
17641771{
@@ -1810,7 +1817,7 @@ StoreRelCheck(Relation rel, char *ccname, char *ccbin)
18101817rte -> skipAcl = false;
18111818ccsrc = deparse_expression (expr ,lcons (lcons (rte ,NIL ),NIL ), false);
18121819
1813- values [Anum_pg_relcheck_rcrelid - 1 ]= rel -> rd_id ;
1820+ values [Anum_pg_relcheck_rcrelid - 1 ]= RelationGetRelid ( rel ) ;
18141821values [Anum_pg_relcheck_rcname - 1 ]= PointerGetDatum (namein (ccname ));
18151822values [Anum_pg_relcheck_rcbin - 1 ]= PointerGetDatum (textin (ccbin ));
18161823values [Anum_pg_relcheck_rcsrc - 1 ]= PointerGetDatum (textin (ccsrc ));
@@ -2077,10 +2084,10 @@ AddRelationRawConstraints(Relation rel,
20772084 */
20782085relrel = heap_openr (RelationRelationName ,RowExclusiveLock );
20792086reltup = SearchSysCacheTupleCopy (RELOID ,
2080- ObjectIdGetDatum (rel -> rd_id ),
2087+ ObjectIdGetDatum (RelationGetRelid ( rel ) ),
208120880 ,0 ,0 );
20822089if (!HeapTupleIsValid (reltup ))
2083- elog (ERROR ,"cache lookup of relation %u failed" ,rel -> rd_id );
2090+ elog (ERROR ,"cache lookup of relation %u failed" ,RelationGetRelid ( rel ) );
20842091relStruct = (Form_pg_class )GETSTRUCT (reltup );
20852092
20862093relStruct -> relchecks = numchecks ;
@@ -2120,7 +2127,7 @@ RemoveAttrDefault(Relation rel)
21202127adrel = heap_openr (AttrDefaultRelationName ,RowExclusiveLock );
21212128
21222129ScanKeyEntryInitialize (& key ,0 ,Anum_pg_attrdef_adrelid ,
2123- F_OIDEQ ,rel -> rd_id );
2130+ F_OIDEQ ,RelationGetRelid ( rel ) );
21242131
21252132adscan = heap_beginscan (adrel ,0 ,SnapshotNow ,1 ,& key );
21262133
@@ -2142,7 +2149,7 @@ RemoveRelCheck(Relation rel)
21422149rcrel = heap_openr (RelCheckRelationName ,RowExclusiveLock );
21432150
21442151ScanKeyEntryInitialize (& key ,0 ,Anum_pg_relcheck_rcrelid ,
2145- F_OIDEQ ,rel -> rd_id );
2152+ F_OIDEQ ,RelationGetRelid ( rel ) );
21462153
21472154rcscan = heap_beginscan (rcrel ,0 ,SnapshotNow ,1 ,& key );
21482155
@@ -2167,3 +2174,25 @@ RemoveConstraints(Relation rel)
21672174if (constr -> num_check > 0 )
21682175RemoveRelCheck (rel );
21692176}
2177+
2178+ static void
2179+ RemoveStatistics (Relation rel )
2180+ {
2181+ Relation pgstatistic ;
2182+ HeapScanDesc scan ;
2183+ ScanKeyData key ;
2184+ HeapTuple tuple ;
2185+
2186+ pgstatistic = heap_openr (StatisticRelationName ,RowExclusiveLock );
2187+
2188+ ScanKeyEntryInitialize (& key ,0x0 ,Anum_pg_statistic_starelid ,
2189+ F_OIDEQ ,
2190+ ObjectIdGetDatum (RelationGetRelid (rel )));
2191+ scan = heap_beginscan (pgstatistic , false,SnapshotNow ,1 ,& key );
2192+
2193+ while (HeapTupleIsValid (tuple = heap_getnext (scan ,0 )))
2194+ heap_delete (pgstatistic ,& tuple -> t_self ,NULL );
2195+
2196+ heap_endscan (scan );
2197+ heap_close (pgstatistic ,RowExclusiveLock );
2198+ }