Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8195f8f

Browse files
committed
Code for WITHOUT OIDS.
On Wed, 2003-01-08 at 21:59, Christopher Kings-Lynne wrote: > I agree. I want to remove OIDs from heaps of our tables when we go to 7.3. > I'd rather not have to do it in the dump due to down time. Rod Taylor <rbt@rbt.ca>
1 parent 8add2e1 commit 8195f8f

File tree

8 files changed

+246
-11
lines changed

8 files changed

+246
-11
lines changed

doc/src/sgml/ref/alter_table.sgml

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.54 2003/01/19 00:13:29 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/alter_table.sgml,v 1.55 2003/02/13 05:19:59 momjian Exp $
33
PostgreSQL documentation
44
-->
55

@@ -33,6 +33,8 @@ ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
3333
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STATISTICS <replaceable class="PARAMETER">integer</replaceable>
3434
ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
3535
ALTER [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
36+
ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
37+
SET WITHOUT OIDS
3638
ALTER TABLE [ ONLY ] <replaceable class="PARAMETER">table</replaceable> [ * ]
3739
RENAME [ COLUMN ] <replaceable class="PARAMETER">column</replaceable> TO <replaceable
3840
class="PARAMETER">new_column</replaceable>
@@ -286,11 +288,24 @@ ALTER TABLE <replaceable class="PARAMETER">table</replaceable>
286288
</listitem>
287289
</varlistentry>
288290

291+
<varlistentry>
292+
<term>SET WITHOUT OIDS</term>
293+
<listitem>
294+
<para>
295+
Removes the <literal>OID</literal> column from the the table. Removing (setting without)
296+
oids from a table also do not occur immediately. The space an <literal>OID</literal>
297+
uses will be reclaimed when the tuple is updated. Without updating the tuple, both the
298+
space and the value of the <literal>OID</literal> are maintained indefinitely. This is
299+
semantically similar to the <literal>DROP COLUMN</literal> process.
300+
</para>
301+
</listitem>
302+
</varlistentry>
303+
289304
<varlistentry>
290305
<term>RENAME</term>
291306
<listitem>
292307
<para>
293-
The <literal>RENAME</literal> forms change the name of a table
308+
The <literal>RENAME</literal> forms change the name of a table
294309
(or an index, sequence, or view) or the name of an individual column in
295310
a table. There is no effect on the stored data.
296311
</para>

src/backend/commands/tablecmds.c

+117-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.66 2003/02/09 06:56:26 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.67 2003/02/13 05:19:59 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2134,7 +2134,6 @@ AlterTableAlterColumnSetNotNull(Oid myrelid, bool recurse,
21342134
heap_close(rel, NoLock);
21352135
}
21362136

2137-
21382137
/*
21392138
* ALTER TABLE ALTER COLUMN SET/DROP DEFAULT
21402139
*/
@@ -2384,6 +2383,122 @@ AlterTableAlterColumnFlags(Oid myrelid, bool recurse,
23842383
heap_close(rel, NoLock); /* close rel, but keep lock! */
23852384
}
23862385

2386+
/*
2387+
* ALTER TABLE SET {WITHOUT} OIDS
2388+
*/
2389+
void
2390+
AlterTableAlterOids(Oid myrelid, bool recurse, bool setOid)
2391+
{
2392+
Relation rel;
2393+
Relation class_rel;
2394+
HeapTuple tuple;
2395+
Form_pg_class tuple_class;
2396+
2397+
rel = heap_open(myrelid, AccessExclusiveLock);
2398+
2399+
if (rel->rd_rel->relkind != RELKIND_RELATION)
2400+
elog(ERROR, "ALTER TABLE: relation \"%s\" is not a table",
2401+
RelationGetRelationName(rel));
2402+
2403+
if (!allowSystemTableMods
2404+
&& IsSystemRelation(rel))
2405+
elog(ERROR, "ALTER TABLE: relation \"%s\" is a system catalog",
2406+
RelationGetRelationName(rel));
2407+
2408+
if (!pg_class_ownercheck(myrelid, GetUserId()))
2409+
aclcheck_error(ACLCHECK_NOT_OWNER, RelationGetRelationName(rel));
2410+
2411+
2412+
/* Get its pg_class tuple, too */
2413+
class_rel = heap_openr(RelationRelationName, RowExclusiveLock);
2414+
2415+
tuple = SearchSysCacheCopy(RELOID,
2416+
ObjectIdGetDatum(myrelid),
2417+
0, 0, 0);
2418+
if (!HeapTupleIsValid(tuple))
2419+
elog(ERROR, "ALTER TABLE: relation %u not found", myrelid);
2420+
tuple_class = (Form_pg_class) GETSTRUCT(tuple);
2421+
2422+
/* Can we change the ownership of this tuple? */
2423+
CheckTupleType(tuple_class);
2424+
2425+
/*
2426+
* Okay, this is a valid tuple: check it's hasoids flag
2427+
* to see if we actually need to change anything
2428+
*/
2429+
if (tuple_class->relhasoids == setOid)
2430+
elog(ERROR, "ALTER TABLE: Table is already %s",
2431+
setOid ? "WITH OIDS" : "WITHOUT OIDS");
2432+
2433+
/*
2434+
* Propagate to children if desired
2435+
*/
2436+
if (recurse)
2437+
{
2438+
List *child,
2439+
*children;
2440+
2441+
/* this routine is actually in the planner */
2442+
children = find_all_inheritors(myrelid);
2443+
2444+
/*
2445+
* find_all_inheritors does the recursive search of the
2446+
* inheritance hierarchy, so all we have to do is process all of
2447+
* the relids in the list that it returns.
2448+
*/
2449+
foreach(child, children)
2450+
{
2451+
Oid childrelid = lfirsti(child);
2452+
2453+
if (childrelid == myrelid)
2454+
continue;
2455+
2456+
AlterTableAlterOids(childrelid, false, setOid);
2457+
}
2458+
}
2459+
2460+
2461+
tuple_class->relhasoids = setOid;
2462+
simple_heap_update(class_rel, &tuple->t_self, tuple);
2463+
2464+
/* Keep the catalog indexes up to date */
2465+
CatalogUpdateIndexes(class_rel, tuple);
2466+
2467+
2468+
2469+
if (setOid)
2470+
/*
2471+
* TODO: Generate the now required OID pg_attribute entry
2472+
*/
2473+
elog(ERROR, "ALTER TABLE WITH OIDS is unsupported");
2474+
else
2475+
{
2476+
HeapTuple atttup;
2477+
Relation attrel;
2478+
2479+
/* Add / Remove the oid record from pg_attribute */
2480+
attrel = heap_open(RelOid_pg_attribute, RowExclusiveLock);
2481+
2482+
/*
2483+
* Oids are being removed from the relation, so we need
2484+
* to remove the oid pg_attribute record relating.
2485+
*/
2486+
atttup = SearchSysCache(ATTNUM,
2487+
ObjectIdGetDatum(myrelid),
2488+
ObjectIdAttributeNumber, 0, 0);
2489+
if (!HeapTupleIsValid(atttup))
2490+
elog(ERROR, "ALTER TABLE: relation %u doesn't have an Oid column to remove", myrelid);
2491+
2492+
simple_heap_delete(attrel, &atttup->t_self);
2493+
2494+
ReleaseSysCache(atttup);
2495+
2496+
heap_close(attrel, NoLock); /* close rel, but keep lock! */
2497+
}
2498+
2499+
heap_close(rel, NoLock); /* close rel, but keep lock! */
2500+
heap_close(class_rel, NoLock); /* close rel, but keep lock! */
2501+
}
23872502

23882503
/*
23892504
* ALTER TABLE DROP COLUMN

src/backend/parser/gram.y

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.401 2003/02/10 04:44:45 tgl Exp $
14+
* $Header: /cvsroot/pgsql/src/backend/parser/gram.y,v 2.402 2003/02/13 05:19:59 momjian Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -1132,7 +1132,7 @@ AlterTableStmt:
11321132
| ALTER TABLE relation_expr ALTER opt_column ColId SET NOT NULL_P
11331133
{
11341134
AlterTableStmt *n = makeNode(AlterTableStmt);
1135-
n->subtype = 'O';
1135+
n->subtype = 'n';
11361136
n->relation = $3;
11371137
n->name = $6;
11381138
$$ = (Node *)n;
@@ -1187,6 +1187,14 @@ AlterTableStmt:
11871187
n->behavior = $7;
11881188
$$ = (Node *)n;
11891189
}
1190+
/* ALTER TABLE <relation> SET WITHOUT OIDS */
1191+
| ALTER TABLE relation_expr SET WITHOUT OIDS
1192+
{
1193+
AlterTableStmt *n = makeNode(AlterTableStmt);
1194+
n->relation = $3;
1195+
n->subtype = 'o';
1196+
$$ = (Node *)n;
1197+
}
11901198
/* ALTER TABLE <name> CREATE TOAST TABLE */
11911199
| ALTER TABLE qualified_name CREATE TOAST TABLE
11921200
{

src/backend/tcop/utility.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.191 2003/02/10 04:44:46 tgl Exp $
13+
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.192 2003/02/13 05:20:01 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -549,7 +549,7 @@ ProcessUtility(Node *parsetree,
549549
interpretInhOption(stmt->relation->inhOpt),
550550
stmt->name);
551551
break;
552-
case 'O': /* ALTER COLUMN SET NOT NULL */
552+
case 'n': /* ALTER COLUMN SET NOT NULL */
553553
AlterTableAlterColumnSetNotNull(relid,
554554
interpretInhOption(stmt->relation->inhOpt),
555555
stmt->name);
@@ -611,6 +611,11 @@ ProcessUtility(Node *parsetree,
611611
AlterTableOwner(relid,
612612
get_usesysid(stmt->name));
613613
break;
614+
case 'o': /* ADD OIDS */
615+
AlterTableAlterOids(relid,
616+
interpretInhOption(stmt->relation->inhOpt),
617+
false);
618+
break;
614619
default: /* oops */
615620
elog(ERROR, "ProcessUtility: Invalid type for AlterTableStmt: %d",
616621
stmt->subtype);

src/include/commands/tablecmds.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: tablecmds.h,v 1.10 2002/11/11 22:19:24 tgl Exp $
10+
* $Id: tablecmds.h,v 1.11 2003/02/13 05:20:03 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -47,6 +47,8 @@ extern void AlterTableCreateToastTable(Oid relOid, bool silent);
4747

4848
extern void AlterTableOwner(Oid relationOid, int32 newOwnerSysId);
4949

50+
extern void AlterTableAlterOids(Oid myrelid, bool recurse, bool setOid);
51+
5052
extern Oid DefineRelation(CreateStmt *stmt, char relkind);
5153

5254
extern void RemoveRelation(const RangeVar *relation, DropBehavior behavior);

src/include/nodes/parsenodes.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: parsenodes.h,v 1.229 2003/02/10 04:44:47 tgl Exp $
10+
* $Id: parsenodes.h,v 1.230 2003/02/13 05:20:03 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -698,7 +698,7 @@ typedef struct AlterTableStmt
698698
* A = add column
699699
* T = alter column default
700700
* N = alter column drop not null
701-
* O = alter column set not null
701+
* n = alter column set not null
702702
* S = alter column statistics
703703
* M = alter column storage
704704
* D = drop column
@@ -708,6 +708,7 @@ typedef struct AlterTableStmt
708708
* X = drop constraint
709709
* E = create toast table
710710
* U = change owner
711+
* o = DROP OIDS
711712
*------------
712713
*/
713714
RangeVar *relation; /* table to work on */

src/test/regress/expected/alter_table.out

+56
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,62 @@ order by relname, attnum;
11791179
drop table p1, p2 cascade;
11801180
NOTICE: Drop cascades to table c1
11811181
NOTICE: Drop cascades to table gc1
1182+
--
1183+
-- Test the ALTER TABLE WITHOUT OIDS command
1184+
--
1185+
create table altstartwith (col integer) with oids;
1186+
insert into altstartwith values (1);
1187+
select oid > 0, * from altstartwith;
1188+
?column? | col
1189+
----------+-----
1190+
t | 1
1191+
(1 row)
1192+
1193+
alter table altstartwith set without oids;
1194+
select oid > 0, * from altstartwith; -- fails
1195+
ERROR: Attribute "oid" not found
1196+
select * from altstartwith;
1197+
col
1198+
-----
1199+
1
1200+
(1 row)
1201+
1202+
-- Run inheritance tests
1203+
create table altwithoid (col integer) with oids;
1204+
-- Inherits parents oid column
1205+
create table altinhoid () inherits (altwithoid) without oids;
1206+
insert into altinhoid values (1);
1207+
select oid > 0, * from altwithoid;
1208+
?column? | col
1209+
----------+-----
1210+
t | 1
1211+
(1 row)
1212+
1213+
select oid > 0, * from altinhoid;
1214+
?column? | col
1215+
----------+-----
1216+
t | 1
1217+
(1 row)
1218+
1219+
alter table altwithoid set without oids;
1220+
alter table altinhoid set without oids; -- fails
1221+
ERROR: ALTER TABLE: Table is already WITHOUT OIDS
1222+
select oid > 0, * from altwithoid; -- fails
1223+
ERROR: Attribute "oid" not found
1224+
select oid > 0, * from altinhoid; -- fails
1225+
ERROR: Attribute "oid" not found
1226+
select * from altwithoid;
1227+
col
1228+
-----
1229+
1
1230+
(1 row)
1231+
1232+
select * from altinhoid;
1233+
col
1234+
-----
1235+
1
1236+
(1 row)
1237+
11821238
-- test renumbering of child-table columns in inherited operations
11831239
create table p1 (f1 int);
11841240
create table c1 (f2 text, f3 int) inherits (p1);

src/test/regress/sql/alter_table.sql

+33
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,39 @@ order by relname, attnum;
850850

851851
drop table p1, p2 cascade;
852852

853+
--
854+
-- Test the ALTER TABLE WITHOUT OIDS command
855+
--
856+
create table altstartwith (col integer) with oids;
857+
858+
insert into altstartwith values (1);
859+
860+
select oid > 0, * from altstartwith;
861+
862+
alter table altstartwith set without oids;
863+
864+
select oid > 0, * from altstartwith; -- fails
865+
select * from altstartwith;
866+
867+
-- Run inheritance tests
868+
create table altwithoid (col integer) with oids;
869+
870+
-- Inherits parents oid column
871+
create table altinhoid () inherits (altwithoid) without oids;
872+
873+
insert into altinhoid values (1);
874+
875+
select oid > 0, * from altwithoid;
876+
select oid > 0, * from altinhoid;
877+
878+
alter table altwithoid set without oids;
879+
alter table altinhoid set without oids; -- fails
880+
881+
select oid > 0, * from altwithoid; -- fails
882+
select oid > 0, * from altinhoid; -- fails
883+
select * from altwithoid;
884+
select * from altinhoid;
885+
853886
-- test renumbering of child-table columns in inherited operations
854887

855888
create table p1 (f1 int);

0 commit comments

Comments
 (0)