Commit e2d156fa authored by Tom Lane's avatar Tom Lane

Add attisinherited column to pg_attribute; use it to guard against

column additions, deletions, and renames that would let a child table
get out of sync with its parent.  Patch by Alvaro Herrera, with some
kibitzing by Tom Lane.
parent 96fd7192
<!--
Documentation of the system catalogs, directed toward PostgreSQL developers
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.55 2002/08/28 15:02:55 tgl Exp $
$Header: /cvsroot/pgsql/doc/src/sgml/catalogs.sgml,v 2.56 2002/08/30 19:23:18 tgl Exp $
-->
<chapter id="catalogs">
......@@ -821,6 +821,16 @@
</entry>
</row>
<row>
<entry>attisinherited</entry>
<entry><type>bool</type></entry>
<entry></entry>
<entry>
This column is inherited from some other relation. An inherited
column cannot be dropped nor renamed.
</entry>
</row>
</tbody>
</tgroup>
</table>
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.86 2002/08/29 00:17:02 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/access/common/tupdesc.c,v 1.87 2002/08/30 19:23:18 tgl Exp $
*
* NOTES
* some of the executor utility code such as "ExecTypeFromTL" should be
......@@ -231,6 +231,9 @@ FreeTupleDesc(TupleDesc tupdesc)
}
/*
* Compare two TupleDesc structures for logical equality
*/
bool
equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
{
......@@ -264,8 +267,12 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
return false;
if (attr1->attnotnull != attr2->attnotnull)
return false;
if (attr1->atthasdef != attr2->atthasdef)
return false;
if (attr1->attisdropped != attr2->attisdropped)
return false;
if (attr1->attisinherited != attr2->attisinherited)
return false;
}
if (tupdesc1->constr != NULL)
{
......@@ -389,6 +396,7 @@ TupleDescInitEntry(TupleDesc desc,
att->attnotnull = false;
att->atthasdef = false;
att->attisdropped = false;
att->attisinherited = false;
tuple = SearchSysCache(TYPEOID,
ObjectIdGetDatum(oidtypeid),
......@@ -514,7 +522,7 @@ BuildDescForRelation(List *schema)
typenameTypeId(entry->typename),
atttypmod, attdim, attisset);
/* This is for constraints */
/* Fill in additional stuff not handled by TupleDescInitEntry */
if (entry->is_not_null)
constr->has_not_null = true;
desc->attrs[attnum - 1]->attnotnull = entry->is_not_null;
......@@ -533,7 +541,9 @@ BuildDescForRelation(List *schema)
desc->attrs[attnum - 1]->atthasdef = true;
}
desc->attrs[attnum - 1]->attisinherited = entry->is_inherited;
}
if (constr->has_not_null || ndef > 0)
{
desc->constr = constr;
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.191 2002/08/29 15:56:19 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.192 2002/08/30 19:23:18 tgl Exp $
*
*
* INTERFACE ROUTINES
......@@ -259,6 +259,7 @@ ConstructTupleDescriptor(Relation heapRelation,
to->attcacheoff = -1;
to->attnotnull = false;
to->atthasdef = false;
to->attisinherited = false;
/*
* We do not yet have the correct relation OID for the index, so
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.84 2002/08/06 02:36:34 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.85 2002/08/30 19:23:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -127,6 +127,7 @@ DefineSequence(CreateSeqStmt *seq)
coldef = makeNode(ColumnDef);
coldef->typename = typnam;
coldef->is_inherited = false;
coldef->is_not_null = true;
coldef->raw_default = NULL;
coldef->cooked_default = NULL;
......
This diff is collapsed.
......@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: view.c,v 1.67 2002/07/16 22:12:19 tgl Exp $
* $Id: view.c,v 1.68 2002/08/30 19:23:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -68,6 +68,7 @@ DefineVirtualRelation(const RangeVar *relation, List *tlist)
typename->typmod = res->restypmod;
def->typename = typename;
def->is_inherited = false;
def->is_not_null = false;
def->raw_default = NULL;
def->cooked_default = NULL;
......
......@@ -15,7 +15,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.207 2002/08/27 04:55:07 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.208 2002/08/30 19:23:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -1729,6 +1729,7 @@ _copyColumnDef(ColumnDef *from)
if (from->colname)
newnode->colname = pstrdup(from->colname);
Node_Copy(from, newnode, typename);
newnode->is_inherited = from->is_inherited;
newnode->is_not_null = from->is_not_null;
Node_Copy(from, newnode, raw_default);
if (from->cooked_default)
......
......@@ -20,7 +20,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.155 2002/08/27 04:55:07 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.156 2002/08/30 19:23:19 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -1748,6 +1748,8 @@ _equalColumnDef(ColumnDef *a, ColumnDef *b)
return false;
if (!equal(a->typename, b->typename))
return false;
if (a->is_inherited != b->is_inherited)
return false;
if (a->is_not_null != b->is_not_null)
return false;
if (!equal(a->raw_default, b->raw_default))
......
......@@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.170 2002/08/29 00:17:04 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/outfuncs.c,v 1.171 2002/08/30 19:23:19 tgl Exp $
*
* NOTES
* Every (plan) node in POSTGRES has an associated "out" routine which
......@@ -176,7 +176,8 @@ _outColumnDef(StringInfo str, ColumnDef *node)
_outToken(str, node->colname);
appendStringInfo(str, " :typename ");
_outNode(str, node->typename);
appendStringInfo(str, " :is_not_null %s :raw_default ",
appendStringInfo(str, " :is_inherited %s :is_not_null %s :raw_default ",
booltostr(node->is_inherited),
booltostr(node->is_not_null));
_outNode(str, node->raw_default);
appendStringInfo(str, " :cooked_default ");
......
......@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.129 2002/08/26 17:53:58 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/nodes/readfuncs.c,v 1.130 2002/08/30 19:23:19 tgl Exp $
*
* NOTES
* Most of the read functions for plan nodes are tested. (In fact, they
......@@ -1485,6 +1485,10 @@ _readColumnDef(void)
token = pg_strtok(&length); /* eat :typename */
local_node->typename = nodeRead(true); /* now read it */
token = pg_strtok(&length); /* eat :is_inherited */
token = pg_strtok(&length); /* get :is_inherited */
local_node->is_inherited = strtobool(token);
token = pg_strtok(&length); /* eat :is_not_null */
token = pg_strtok(&length); /* get :is_not_null */
local_node->is_not_null = strtobool(token);
......
......@@ -10,7 +10,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.174 2002/08/29 00:17:04 tgl Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.175 2002/08/30 19:23:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -430,7 +430,8 @@ ProcessUtility(Node *parsetree,
renameatt(relid,
stmt->oldname, /* old att name */
stmt->newname, /* new att name */
interpretInhOption(stmt->relation->inhOpt)); /* recursive? */
interpretInhOption(stmt->relation->inhOpt), /* recursive? */
false); /* recursing already? */
break;
case RENAME_TRIGGER:
renametrig(relid,
......@@ -470,6 +471,7 @@ ProcessUtility(Node *parsetree,
*/
AlterTableAddColumn(relid,
interpretInhOption(stmt->relation->inhOpt),
false,
(ColumnDef *) stmt->def);
break;
case 'T': /* ALTER COLUMN DEFAULT */
......@@ -505,13 +507,13 @@ ProcessUtility(Node *parsetree,
&(stmt->subtype));
break;
case 'D': /* DROP COLUMN */
/*
* XXX We don't actually recurse yet, but what we should do would be:
/*
* Recursively drop column from table and,
* if requested, from descendants
*/
AlterTableDropColumn(relid,
interpretInhOption(stmt->relation->inhOpt),
false,
stmt->name,
stmt->behavior);
break;
......
......@@ -37,7 +37,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: catversion.h,v 1.154 2002/08/27 04:00:28 momjian Exp $
* $Id: catversion.h,v 1.155 2002/08/30 19:23:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -53,6 +53,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 200208271
#define CATALOG_VERSION_NO 200208301
#endif
This diff is collapsed.
......@@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: pg_class.h,v 1.71 2002/08/15 16:36:07 momjian Exp $
* $Id: pg_class.h,v 1.72 2002/08/30 19:23:20 tgl Exp $
*
* NOTES
* the genbki.sh script reads this file and generates .bki
......@@ -136,7 +136,7 @@ typedef FormData_pg_class *Form_pg_class;
DATA(insert OID = 1247 ( pg_type PGNSP 71 PGUID 0 1247 0 0 0 0 f f r 20 0 0 0 0 0 t f f f _null_ ));
DESCR("");
DATA(insert OID = 1249 ( pg_attribute PGNSP 75 PGUID 0 1249 0 0 0 0 f f r 16 0 0 0 0 0 f f f f _null_ ));
DATA(insert OID = 1249 ( pg_attribute PGNSP 75 PGUID 0 1249 0 0 0 0 f f r 17 0 0 0 0 0 f f f f _null_ ));
DESCR("");
DATA(insert OID = 1255 ( pg_proc PGNSP 81 PGUID 0 1255 0 0 0 0 f f r 15 0 0 0 0 0 t f f f _null_ ));
DESCR("");
......
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: tablecmds.h,v 1.5 2002/07/01 15:27:56 tgl Exp $
* $Id: tablecmds.h,v 1.6 2002/08/30 19:23:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -16,31 +16,31 @@
#include "nodes/parsenodes.h"
extern void AlterTableAddColumn(Oid myrelid, bool inherits,
extern void AlterTableAddColumn(Oid myrelid, bool recurse, bool recursing,
ColumnDef *colDef);
extern void AlterTableAlterColumnDefault(Oid myrelid, bool inh,
const char *colName,
Node *newDefault);
extern void AlterTableAlterColumnDropNotNull(Oid myrelid, bool inh,
extern void AlterTableAlterColumnDropNotNull(Oid myrelid, bool recurse,
const char *colName);
extern void AlterTableAlterColumnSetNotNull(Oid myrelid, bool inh,
extern void AlterTableAlterColumnSetNotNull(Oid myrelid, bool recurse,
const char *colName);
extern void AlterTableAlterColumnFlags(Oid myrelid, bool inh,
extern void AlterTableAlterColumnDefault(Oid myrelid, bool recurse,
const char *colName,
Node *newDefault);
extern void AlterTableAlterColumnFlags(Oid myrelid, bool recurse,
const char *colName,
Node *flagValue, const char *flagType);
extern void AlterTableDropColumn(Oid myrelid, bool inh,
extern void AlterTableDropColumn(Oid myrelid, bool recurse, bool recursing,
const char *colName,
DropBehavior behavior);
extern void AlterTableAddConstraint(Oid myrelid, bool inh,
extern void AlterTableAddConstraint(Oid myrelid, bool recurse,
List *newConstraints);
extern void AlterTableDropConstraint(Oid myrelid, bool inh,
extern void AlterTableDropConstraint(Oid myrelid, bool recurse,
const char *constrName,
DropBehavior behavior);
......@@ -54,12 +54,13 @@ extern void RemoveRelation(const RangeVar *relation, DropBehavior behavior);
extern void TruncateRelation(const RangeVar *relation);
extern void renameatt(Oid relid,
extern void renameatt(Oid myrelid,
const char *oldattname,
const char *newattname,
bool recurse);
bool recurse,
bool recursing);
extern void renamerel(Oid relid,
extern void renamerel(Oid myrelid,
const char *newrelname);
#endif /* TABLECMDS_H */
......@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* $Id: parsenodes.h,v 1.202 2002/08/27 04:55:12 tgl Exp $
* $Id: parsenodes.h,v 1.203 2002/08/30 19:23:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
......@@ -279,6 +279,7 @@ typedef struct ColumnDef
NodeTag type;
char *colname; /* name of column */
TypeName *typename; /* type of column */
bool is_inherited; /* column is inherited? */
bool is_not_null; /* NOT NULL constraint specified? */
Node *raw_default; /* default value (untransformed parse
* tree) */
......
......@@ -1023,3 +1023,34 @@ select * from test;
(3 rows)
drop table test;
-- test inheritance
create table dropColumn (a int, b int, e int);
create table dropColumnChild (c int) inherits (dropColumn);
create table dropColumnAnother (d int) inherits (dropColumnChild);
-- these two should fail
alter table dropColumnchild drop column a;
ERROR: ALTER TABLE: Cannot drop inherited column "a"
alter table only dropColumnChild drop column b;
ERROR: ALTER TABLE: Cannot drop inherited column "b"
-- these three should work
alter table only dropColumn drop column e;
alter table dropColumnChild drop column c;
alter table dropColumn drop column a;
create table renameColumn (a int);
create table renameColumnChild (b int) inherits (renameColumn);
create table renameColumnAnother (c int) inherits (renameColumnChild);
-- these three should fail
alter table renameColumnChild rename column a to d;
ERROR: renameatt: inherited attribute "a" may not be renamed
alter table only renameColumnChild rename column a to d;
ERROR: Inherited attribute "a" must be renamed in child tables too
alter table only renameColumn rename column a to d;
ERROR: Inherited attribute "a" must be renamed in child tables too
-- these should work
alter table renameColumn rename column a to d;
alter table renameColumnChild rename column b to a;
-- this should work
alter table renameColumn add column w int;
-- this should fail
alter table only renameColumn add column x int;
ERROR: Attribute must be added to child tables too
......@@ -731,3 +731,36 @@ copy test(b,c) from stdin;
select * from test;
drop table test;
-- test inheritance
create table dropColumn (a int, b int, e int);
create table dropColumnChild (c int) inherits (dropColumn);
create table dropColumnAnother (d int) inherits (dropColumnChild);
-- these two should fail
alter table dropColumnchild drop column a;
alter table only dropColumnChild drop column b;
-- these three should work
alter table only dropColumn drop column e;
alter table dropColumnChild drop column c;
alter table dropColumn drop column a;
create table renameColumn (a int);
create table renameColumnChild (b int) inherits (renameColumn);
create table renameColumnAnother (c int) inherits (renameColumnChild);
-- these three should fail
alter table renameColumnChild rename column a to d;
alter table only renameColumnChild rename column a to d;
alter table only renameColumn rename column a to d;
-- these should work
alter table renameColumn rename column a to d;
alter table renameColumnChild rename column b to a;
-- this should work
alter table renameColumn add column w int;
-- this should fail
alter table only renameColumn add column x int;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment