8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.114 2000/12/22 23:12:05 tgl Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.115 2001/01/07 00:05:22 tgl Exp $
12
12
*
13
13
* NOTES
14
14
* The PerformAddAttribute() code, like most of the relation
57
57
58
58
59
59
static bool needs_toast_table (Relation rel );
60
- static bool is_viewr (char * relname );
61
- static bool is_view (Relation rel );
60
+ static bool is_relation (char * name );
62
61
63
62
64
63
/* --------------------------------
@@ -302,6 +301,11 @@ AlterTableAddColumn(const char *relationName,
302
301
* release until end of transaction.
303
302
*/
304
303
rel = heap_openr (relationName , AccessExclusiveLock );
304
+
305
+ if (rel -> rd_rel -> relkind != RELKIND_RELATION )
306
+ elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
307
+ relationName );
308
+
305
309
myrelid = RelationGetRelid (rel );
306
310
heap_close (rel , NoLock ); /* close rel but keep lock! */
307
311
@@ -367,13 +371,6 @@ AlterTableAddColumn(const char *relationName,
367
371
elog (ERROR , "ALTER TABLE: relation \"%s\" not found" ,
368
372
relationName );
369
373
370
- /*
371
- * XXX is the following check sufficient?
372
- */
373
- if (((Form_pg_class ) GETSTRUCT (reltup ))-> relkind != RELKIND_RELATION )
374
- elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
375
- relationName );
376
-
377
374
minattnum = ((Form_pg_class ) GETSTRUCT (reltup ))-> relnatts ;
378
375
maxatts = minattnum + 1 ;
379
376
if (maxatts > MaxHeapAttributeNumber )
@@ -517,8 +514,9 @@ AlterTableAlterColumn(const char *relationName,
517
514
#endif
518
515
519
516
rel = heap_openr (relationName , AccessExclusiveLock );
520
- if ( rel -> rd_rel -> relkind == RELKIND_VIEW )
521
- elog (ERROR , "ALTER TABLE: %s is a view" , relationName );
517
+ if (rel -> rd_rel -> relkind != RELKIND_RELATION )
518
+ elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
519
+ relationName );
522
520
myrelid = RelationGetRelid (rel );
523
521
heap_close (rel , NoLock );
524
522
@@ -936,6 +934,11 @@ AlterTableDropColumn(const char *relationName,
936
934
* release until end of transaction.
937
935
*/
938
936
rel = heap_openr (relationName , AccessExclusiveLock );
937
+
938
+ if (rel -> rd_rel -> relkind != RELKIND_RELATION )
939
+ elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
940
+ relationName );
941
+
939
942
myrelid = RelationGetRelid (rel );
940
943
heap_close (rel , NoLock ); /* close rel but keep lock! */
941
944
@@ -969,15 +972,6 @@ AlterTableDropColumn(const char *relationName,
969
972
reltup = heap_copytuple (& classtuple );
970
973
ReleaseBuffer (buffer );
971
974
972
- /*
973
- * XXX is the following check sufficient?
974
- */
975
- if (((Form_pg_class ) GETSTRUCT (reltup ))-> relkind != RELKIND_RELATION )
976
- {
977
- elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
978
- relationName );
979
- }
980
-
981
975
attrdesc = heap_openr (AttributeRelationName , RowExclusiveLock );
982
976
983
977
/*
@@ -1090,9 +1084,10 @@ AlterTableAddConstraint(char *relationName,
1090
1084
elog (ERROR , "ALTER TABLE: permission denied" );
1091
1085
#endif
1092
1086
1093
- /* check to see if the table to be constrained is a view. */
1094
- if (is_viewr (relationName ))
1095
- elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1087
+ /* Disallow ADD CONSTRAINT on views, indexes, sequences, etc */
1088
+ if (! is_relation (relationName ))
1089
+ elog (ERROR , "ALTER TABLE ADD CONSTRAINT: %s is not a table" ,
1090
+ relationName );
1096
1091
1097
1092
switch (nodeTag (newConstraint ))
1098
1093
{
@@ -1482,9 +1477,18 @@ AlterTableOwner(const char *relationName, const char *newOwnerName)
1482
1477
elog (ERROR , "ALTER TABLE: relation \"%s\" not found" ,
1483
1478
relationName );
1484
1479
1485
- if (((Form_pg_class ) GETSTRUCT (tuple ))-> relkind != RELKIND_RELATION )
1486
- elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
1487
- relationName );
1480
+ switch (((Form_pg_class ) GETSTRUCT (tuple ))-> relkind )
1481
+ {
1482
+ case RELKIND_RELATION :
1483
+ case RELKIND_INDEX :
1484
+ case RELKIND_VIEW :
1485
+ case RELKIND_SEQUENCE :
1486
+ /* ok to change owner */
1487
+ break ;
1488
+ default :
1489
+ elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table, index, view, or sequence" ,
1490
+ relationName );
1491
+ }
1488
1492
1489
1493
/*
1490
1494
* modify the table's entry and write to the heap
@@ -1541,6 +1545,11 @@ AlterTableCreateToastTable(const char *relationName, bool silent)
1541
1545
* release until end of transaction.
1542
1546
*/
1543
1547
rel = heap_openr (relationName , AccessExclusiveLock );
1548
+
1549
+ if (rel -> rd_rel -> relkind != RELKIND_RELATION )
1550
+ elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
1551
+ relationName );
1552
+
1544
1553
myrelid = RelationGetRelid (rel );
1545
1554
1546
1555
/*
@@ -1569,19 +1578,8 @@ AlterTableCreateToastTable(const char *relationName, bool silent)
1569
1578
ReleaseBuffer (buffer );
1570
1579
1571
1580
/*
1572
- * XXX is the following check sufficient? At least it would
1573
- * allow to create TOAST tables for views. But why not - someone
1574
- * can insert into a view, so it shouldn't be impossible to hide
1575
- * huge data there :-)
1576
- *
1577
- * Not any more.
1581
+ * Is it already toasted?
1578
1582
*/
1579
- if (((Form_pg_class ) GETSTRUCT (reltup ))-> relkind != RELKIND_RELATION )
1580
- {
1581
- elog (ERROR , "ALTER TABLE: relation \"%s\" is not a table" ,
1582
- relationName );
1583
- }
1584
-
1585
1583
if (((Form_pg_class ) GETSTRUCT (reltup ))-> reltoastrelid != InvalidOid )
1586
1584
{
1587
1585
if (silent )
@@ -1781,9 +1779,6 @@ LockTableCommand(LockStmt *lockstmt)
1781
1779
if (rel -> rd_rel -> relkind != RELKIND_RELATION )
1782
1780
elog (ERROR , "LOCK TABLE: %s is not a table" , lockstmt -> relname );
1783
1781
1784
- if (is_view (rel ))
1785
- elog (ERROR , "LOCK TABLE: cannot lock a view" );
1786
-
1787
1782
if (lockstmt -> mode == AccessShareLock )
1788
1783
aclresult = pg_aclcheck (lockstmt -> relname , GetUserId (), ACL_RD );
1789
1784
else
@@ -1799,60 +1794,13 @@ LockTableCommand(LockStmt *lockstmt)
1799
1794
1800
1795
1801
1796
static bool
1802
- is_viewr (char * name )
1797
+ is_relation (char * name )
1803
1798
{
1804
1799
Relation rel = heap_openr (name , NoLock );
1805
1800
1806
- bool retval = is_view (rel );
1801
+ bool retval = (rel -> rd_rel -> relkind == RELKIND_RELATION );
1807
1802
1808
1803
heap_close (rel , NoLock );
1809
1804
1810
1805
return retval ;
1811
1806
}
1812
-
1813
- static bool
1814
- is_view (Relation rel )
1815
- {
1816
- Relation RewriteRelation ;
1817
- HeapScanDesc scanDesc ;
1818
- ScanKeyData scanKeyData ;
1819
- HeapTuple tuple ;
1820
- Form_pg_rewrite data ;
1821
- bool retval = false;
1822
-
1823
- /*
1824
- * Open the pg_rewrite relation.
1825
- */
1826
- RewriteRelation = heap_openr (RewriteRelationName , RowExclusiveLock );
1827
-
1828
- /*
1829
- * Scan the RuleRelation ('pg_rewrite') for all the tuples that has
1830
- * the same ev_class as the relation being checked.
1831
- */
1832
- ScanKeyEntryInitialize (& scanKeyData ,
1833
- 0 ,
1834
- Anum_pg_rewrite_ev_class ,
1835
- F_OIDEQ ,
1836
- ObjectIdGetDatum (rel -> rd_id ));
1837
- scanDesc = heap_beginscan (RewriteRelation ,
1838
- 0 , SnapshotNow , 1 , & scanKeyData );
1839
-
1840
- while (HeapTupleIsValid (tuple = heap_getnext (scanDesc , 0 )))
1841
- {
1842
- if (tuple -> t_data != NULL )
1843
- {
1844
- data = (Form_pg_rewrite ) GETSTRUCT (tuple );
1845
- if (data -> ev_type == '1' )
1846
- {
1847
- retval = true;
1848
- break ;
1849
- }
1850
- }
1851
-
1852
- }
1853
-
1854
- heap_endscan (scanDesc );
1855
- heap_close (RewriteRelation , RowExclusiveLock );
1856
-
1857
- return retval ;
1858
- }
0 commit comments