diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2002-07-15 16:33:32 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2002-07-15 16:33:32 +0000 |
commit | 7bd631bfa4740c20625ae1f16856707baeda466d (patch) | |
tree | e851f618c76895be9e78fc8458315a34c9c938a5 /src/backend/commands/tablecmds.c | |
parent | 3c580b8d979a94ea0e41578c7ac1d4625d242853 (diff) | |
download | postgresql-7bd631bfa4740c20625ae1f16856707baeda466d.tar.gz postgresql-7bd631bfa4740c20625ae1f16856707baeda466d.zip |
Use the dependency mechanism to manage column defaults. We need this
so that dependencies in default expressions (on operators, functions,
etc) can be expressed properly.
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 77 |
1 files changed, 8 insertions, 69 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 0d4d277bae4..3722a071082 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.20 2002/07/12 18:43:16 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/commands/tablecmds.c,v 1.21 2002/07/15 16:33:31 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -23,7 +23,6 @@ #include "catalog/index.h" #include "catalog/indexing.h" #include "catalog/namespace.h" -#include "catalog/pg_attrdef.h" #include "catalog/pg_constraint.h" #include "catalog/pg_inherits.h" #include "catalog/pg_namespace.h" @@ -57,7 +56,6 @@ static bool change_varattnos_of_a_node(Node *node, const AttrNumber *newattno); static void StoreCatalogInheritance(Oid relationId, List *supers); static int findAttrByName(const char *attributeName, List *schema); static void setRelhassubclassInRelation(Oid relationId, bool relhassubclass); -static void drop_default(Oid relid, int16 attnum); static void CheckTupleType(Form_pg_class tuple_class); static bool needs_toast_table(Relation rel); static void validateForeignKeyConstraint(FkConstraint *fkconstraint, @@ -2080,14 +2078,18 @@ AlterTableAlterColumnDefault(Oid myrelid, attnum = ((Form_pg_attribute) GETSTRUCT(tuple))->attnum; ReleaseSysCache(tuple); + /* + * Remove any old default for the column. We use RESTRICT here for + * safety, but at present we do not expect anything to depend on the + * default. + */ + RemoveAttrDefault(myrelid, attnum, DROP_RESTRICT, false); + if (newDefault) { /* SET DEFAULT */ RawColumnDefault *rawEnt; - /* Get rid of the old one first */ - drop_default(myrelid, attnum); - rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault)); rawEnt->attnum = attnum; rawEnt->raw_default = newDefault; @@ -2098,73 +2100,10 @@ AlterTableAlterColumnDefault(Oid myrelid, */ AddRelationRawConstraints(rel, makeList1(rawEnt), NIL); } - else - { - /* DROP DEFAULT */ - Relation attr_rel; - - /* Fix the pg_attribute row */ - attr_rel = heap_openr(AttributeRelationName, RowExclusiveLock); - - tuple = SearchSysCacheCopy(ATTNAME, - ObjectIdGetDatum(myrelid), - PointerGetDatum(colName), - 0, 0); - if (!HeapTupleIsValid(tuple)) /* shouldn't happen */ - elog(ERROR, "ALTER TABLE: relation \"%s\" has no column \"%s\"", - RelationGetRelationName(rel), colName); - - ((Form_pg_attribute) GETSTRUCT(tuple))->atthasdef = FALSE; - - simple_heap_update(attr_rel, &tuple->t_self, tuple); - - /* keep the system catalog indices current */ - if (RelationGetForm(attr_rel)->relhasindex) - { - Relation idescs[Num_pg_attr_indices]; - - CatalogOpenIndices(Num_pg_attr_indices, Name_pg_attr_indices, idescs); - CatalogIndexInsert(idescs, Num_pg_attr_indices, attr_rel, tuple); - CatalogCloseIndices(Num_pg_attr_indices, idescs); - } - - heap_close(attr_rel, RowExclusiveLock); - - /* get rid of actual default definition in pg_attrdef */ - drop_default(myrelid, attnum); - } heap_close(rel, NoLock); } - -static void -drop_default(Oid relid, int16 attnum) -{ - ScanKeyData scankeys[2]; - HeapScanDesc scan; - Relation attrdef_rel; - HeapTuple tuple; - - attrdef_rel = heap_openr(AttrDefaultRelationName, RowExclusiveLock); - ScanKeyEntryInitialize(&scankeys[0], 0x0, - Anum_pg_attrdef_adrelid, F_OIDEQ, - ObjectIdGetDatum(relid)); - ScanKeyEntryInitialize(&scankeys[1], 0x0, - Anum_pg_attrdef_adnum, F_INT2EQ, - Int16GetDatum(attnum)); - - scan = heap_beginscan(attrdef_rel, SnapshotNow, 2, scankeys); - - if ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL) - simple_heap_delete(attrdef_rel, &tuple->t_self); - - heap_endscan(scan); - - heap_close(attrdef_rel, NoLock); -} - - /* * ALTER TABLE ALTER COLUMN SET STATISTICS / STORAGE */ |