Skip to content

Commit 24614a9

Browse files
committed
Upgrade ALTER TABLE DROP COLUMN so that it can drop an OID column, and
remove separate implementation of ALTER TABLE SET WITHOUT OIDS in favor of doing a regular DROP. Also, cause CREATE TABLE to account completely correctly for the inheritance status of the OID column. This fixes problems with dropping OID columns that have dependencies, as noted by Christopher Kings-Lynne, as well as making sure that you can't drop an OID column that was inherited from a parent.
1 parent 446b547 commit 24614a9

File tree

11 files changed

+151
-167
lines changed

11 files changed

+151
-167
lines changed

doc/src/sgml/ref/alter_table.sgml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.66 2004/03/09 16:57:47 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.67 2004/03/23 19:35:15 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -150,12 +150,11 @@ ALTER TABLE <replaceable class="PARAMETER">name</replaceable>
150150
<term><literal>SET WITHOUT OIDS</literal></term>
151151
<listitem>
152152
<para>
153-
This form removes the <literal>oid</literal> column from the
154-
table. Removing OIDs from a table does not occur immediately.
155-
The space that the OID uses will be reclaimed when the row is
156-
updated. Without updating the row, both the space and the value
157-
of the OID are kept indefinitely. This is semantically similar
158-
to the <literal>DROP COLUMN</literal> process.
153+
This form removes the <literal>oid</literal> system column from the
154+
table. This is exactly equivalent to
155+
<literal>DROP COLUMN oid RESTRICT</literal>,
156+
except that it will not complain if there is already no
157+
<literal>oid</literal> column.
159158
</para>
160159

161160
<para>

src/backend/bootstrap/bootparse.y

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.64 2004/01/07 18:56:25 neilc Exp $
12+
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.65 2004/03/23 19:35:16 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -197,6 +197,8 @@ Boot_CreateStmt:
197197
tupdesc,
198198
RELKIND_RELATION,
199199
$3,
200+
true,
201+
0,
200202
ONCOMMIT_NOOP,
201203
true);
202204
elog(DEBUG4, "relation created with oid %u", id);

src/backend/catalog/heap.c

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.260 2004/02/15 21:01:39 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/catalog/heap.c,v 1.261 2004/03/23 19:35:16 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -458,7 +458,9 @@ CheckAttributeType(const char *attname, Oid atttypid)
458458
static void
459459
AddNewAttributeTuples(Oid new_rel_oid,
460460
TupleDesc tupdesc,
461-
char relkind)
461+
char relkind,
462+
bool oidislocal,
463+
int oidinhcount)
462464
{
463465
Form_pg_attribute *dpp;
464466
int i;
@@ -531,11 +533,18 @@ AddNewAttributeTuples(Oid new_rel_oid,
531533
false,
532534
ATTRIBUTE_TUPLE_SIZE,
533535
(void *) *dpp);
536+
attStruct = (Form_pg_attribute) GETSTRUCT(tup);
534537

535538
/* Fill in the correct relation OID in the copied tuple */
536-
attStruct = (Form_pg_attribute) GETSTRUCT(tup);
537539
attStruct->attrelid = new_rel_oid;
538540

541+
/* Fill in correct inheritance info for the OID column */
542+
if (attStruct->attnum == ObjectIdAttributeNumber)
543+
{
544+
attStruct->attislocal = oidislocal;
545+
attStruct->attinhcount = oidinhcount;
546+
}
547+
539548
/*
540549
* Unneeded since they should be OK in the constant data
541550
* anyway
@@ -713,6 +722,8 @@ heap_create_with_catalog(const char *relname,
713722
TupleDesc tupdesc,
714723
char relkind,
715724
bool shared_relation,
725+
bool oidislocal,
726+
int oidinhcount,
716727
OnCommitAction oncommit,
717728
bool allow_system_table_mods)
718729
{
@@ -786,7 +797,8 @@ heap_create_with_catalog(const char *relname,
786797
* now add tuples to pg_attribute for the attributes in our new
787798
* relation.
788799
*/
789-
AddNewAttributeTuples(new_rel_oid, new_rel_desc->rd_att, relkind);
800+
AddNewAttributeTuples(new_rel_oid, new_rel_desc->rd_att, relkind,
801+
oidislocal, oidinhcount);
790802

791803
/*
792804
* make a dependency link to force the relation to be deleted if its
@@ -973,35 +985,46 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
973985
attnum, relid);
974986
attStruct = (Form_pg_attribute) GETSTRUCT(tuple);
975987

976-
/* Mark the attribute as dropped */
977-
attStruct->attisdropped = true;
988+
if (attnum < 0)
989+
{
990+
/* System attribute (probably OID) ... just delete the row */
978991

979-
/*
980-
* Set the type OID to invalid. A dropped attribute's type link
981-
* cannot be relied on (once the attribute is dropped, the type might
982-
* be too). Fortunately we do not need the type row --- the only
983-
* really essential information is the type's typlen and typalign,
984-
* which are preserved in the attribute's attlen and attalign. We set
985-
* atttypid to zero here as a means of catching code that incorrectly
986-
* expects it to be valid.
987-
*/
988-
attStruct->atttypid = InvalidOid;
992+
simple_heap_delete(attr_rel, &tuple->t_self);
993+
}
994+
else
995+
{
996+
/* Dropping user attributes is lots harder */
989997

990-
/* Remove any NOT NULL constraint the column may have */
991-
attStruct->attnotnull = false;
998+
/* Mark the attribute as dropped */
999+
attStruct->attisdropped = true;
9921000

993-
/* We don't want to keep stats for it anymore */
994-
attStruct->attstattarget = 0;
1001+
/*
1002+
* Set the type OID to invalid. A dropped attribute's type link
1003+
* cannot be relied on (once the attribute is dropped, the type might
1004+
* be too). Fortunately we do not need the type row --- the only
1005+
* really essential information is the type's typlen and typalign,
1006+
* which are preserved in the attribute's attlen and attalign. We set
1007+
* atttypid to zero here as a means of catching code that incorrectly
1008+
* expects it to be valid.
1009+
*/
1010+
attStruct->atttypid = InvalidOid;
9951011

996-
/* Change the column name to something that isn't likely to conflict */
997-
snprintf(newattname, sizeof(newattname),
998-
"........pg.dropped.%d........", attnum);
999-
namestrcpy(&(attStruct->attname), newattname);
1012+
/* Remove any NOT NULL constraint the column may have */
1013+
attStruct->attnotnull = false;
10001014

1001-
simple_heap_update(attr_rel, &tuple->t_self, tuple);
1015+
/* We don't want to keep stats for it anymore */
1016+
attStruct->attstattarget = 0;
10021017

1003-
/* keep the system catalog indexes current */
1004-
CatalogUpdateIndexes(attr_rel, tuple);
1018+
/* Change the column name to something that isn't likely to conflict */
1019+
snprintf(newattname, sizeof(newattname),
1020+
"........pg.dropped.%d........", attnum);
1021+
namestrcpy(&(attStruct->attname), newattname);
1022+
1023+
simple_heap_update(attr_rel, &tuple->t_self, tuple);
1024+
1025+
/* keep the system catalog indexes current */
1026+
CatalogUpdateIndexes(attr_rel, tuple);
1027+
}
10051028

10061029
/*
10071030
* Because updating the pg_attribute row will trigger a relcache flush
@@ -1011,7 +1034,8 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
10111034

10121035
heap_close(attr_rel, RowExclusiveLock);
10131036

1014-
RemoveStatistics(rel, attnum);
1037+
if (attnum > 0)
1038+
RemoveStatistics(rel, attnum);
10151039

10161040
relation_close(rel, NoLock);
10171041
}

src/backend/commands/cluster.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.119 2003/11/29 19:51:47 pgsql Exp $
14+
* $PostgreSQL: pgsql/src/backend/commands/cluster.c,v 1.120 2004/03/23 19:35:16 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -501,6 +501,8 @@ make_new_heap(Oid OIDOldHeap, const char *NewName)
501501
tupdesc,
502502
OldHeap->rd_rel->relkind,
503503
OldHeap->rd_rel->relisshared,
504+
true,
505+
0,
504506
ONCOMMIT_NOOP,
505507
allowSystemTableMods);
506508

0 commit comments

Comments
 (0)