diff options
| author | Tom Lane | 2004-05-05 04:48:48 +0000 |
|---|---|---|
| committer | Tom Lane | 2004-05-05 04:48:48 +0000 |
| commit | 077db40fa1f3ef93a60d995cc5b2666474f81302 (patch) | |
| tree | 911e14b79a368b10ad9fc267fa69f299e86b15f9 /src/include | |
| parent | 3e3cb0a14a608bb058407b6349c3c9e2d09e13b8 (diff) | |
ALTER TABLE rewrite. New cool stuff:
* ALTER ... ADD COLUMN with defaults and NOT NULL constraints works per SQL
spec. A default is implemented by rewriting the table with the new value
stored in each row.
* ALTER COLUMN TYPE. You can change a column's datatype to anything you
want, so long as you can specify how to convert the old value. Rewrites
the table. (Possible future improvement: optimize no-op conversions such
as varchar(N) to varchar(N+1).)
* Multiple ALTER actions in a single ALTER TABLE command. You can perform
any number of column additions, type changes, and constraint additions with
only one pass over the table contents.
Basic documentation provided in ALTER TABLE ref page, but some more docs
work is needed.
Original patch from Rod Taylor, additional work from Tom Lane.
Diffstat (limited to 'src/include')
| -rw-r--r-- | src/include/catalog/dependency.h | 31 | ||||
| -rw-r--r-- | src/include/catalog/heap.h | 14 | ||||
| -rw-r--r-- | src/include/catalog/index.h | 5 | ||||
| -rw-r--r-- | src/include/commands/cluster.h | 6 | ||||
| -rw-r--r-- | src/include/commands/defrem.h | 10 | ||||
| -rw-r--r-- | src/include/commands/tablecmds.h | 39 | ||||
| -rw-r--r-- | src/include/nodes/nodes.h | 3 | ||||
| -rw-r--r-- | src/include/nodes/parsenodes.h | 71 | ||||
| -rw-r--r-- | src/include/parser/analyze.h | 5 | ||||
| -rw-r--r-- | src/include/utils/builtins.h | 4 |
10 files changed, 114 insertions, 74 deletions
diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h index 583851c7493..0e230d7e862 100644 --- a/src/include/catalog/dependency.h +++ b/src/include/catalog/dependency.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/dependency.h,v 1.11 2003/11/29 22:40:58 pgsql Exp $ + * $PostgreSQL: pgsql/src/include/catalog/dependency.h,v 1.12 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -17,7 +17,7 @@ #include "nodes/parsenodes.h" /* for DropBehavior */ -/* +/*---------- * Precise semantics of a dependency relationship are specified by the * DependencyType code (which is stored in a "char" field in pg_depend, * so we assign ASCII-code values to the enumeration members). @@ -56,6 +56,7 @@ * contain zeroes. * * Other dependency flavors may be needed in future. + *---------- */ typedef enum DependencyType @@ -79,6 +80,28 @@ typedef struct ObjectAddress } ObjectAddress; +/* + * This enum covers all system catalogs whose OIDs can appear in classId. + */ +typedef enum ObjectClass +{ + OCLASS_CLASS, /* pg_class */ + OCLASS_PROC, /* pg_proc */ + OCLASS_TYPE, /* pg_type */ + OCLASS_CAST, /* pg_cast */ + OCLASS_CONSTRAINT, /* pg_constraint */ + OCLASS_CONVERSION, /* pg_conversion */ + OCLASS_DEFAULT, /* pg_attrdef */ + OCLASS_LANGUAGE, /* pg_language */ + OCLASS_OPERATOR, /* pg_operator */ + OCLASS_OPCLASS, /* pg_opclass */ + OCLASS_REWRITE, /* pg_rewrite */ + OCLASS_TRIGGER, /* pg_trigger */ + OCLASS_SCHEMA, /* pg_namespace */ + MAX_OCLASS /* MUST BE LAST */ +} ObjectClass; + + /* in dependency.c */ extern void performDeletion(const ObjectAddress *object, @@ -96,6 +119,10 @@ extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, DependencyType behavior, DependencyType self_behavior); +extern ObjectClass getObjectClass(const ObjectAddress *object); + +extern char *getObjectDescription(const ObjectAddress *object); + /* in pg_depend.c */ extern void recordDependencyOn(const ObjectAddress *depender, diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h index f635ef1bd09..2913d53e521 100644 --- a/src/include/catalog/heap.h +++ b/src/include/catalog/heap.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/heap.h,v 1.65 2004/03/23 19:35:17 tgl Exp $ + * $PostgreSQL: pgsql/src/include/catalog/heap.h,v 1.66 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -27,6 +27,14 @@ typedef struct RawColumnDefault * tree) */ } RawColumnDefault; +typedef struct CookedConstraint +{ + ConstrType contype; /* CONSTR_DEFAULT or CONSTR_CHECK */ + char *name; /* name, or NULL if none */ + AttrNumber attnum; /* which attr (only for DEFAULT) */ + Node *expr; /* transformed default or check expr */ +} CookedConstraint; + extern Relation heap_create(const char *relname, Oid relnamespace, TupleDesc tupDesc, @@ -52,10 +60,12 @@ extern void heap_truncate(Oid rid); extern void heap_truncate_check_FKs(Relation rel); -extern void AddRelationRawConstraints(Relation rel, +extern List *AddRelationRawConstraints(Relation rel, List *rawColDefaults, List *rawConstraints); +extern void StoreAttrDefault(Relation rel, AttrNumber attnum, char *adbin); + extern Node *cookDefault(ParseState *pstate, Node *raw_default, Oid atttypid, diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index da210c483ae..13c367c9022 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/catalog/index.h,v 1.54 2003/11/29 22:40:58 pgsql Exp $ + * $PostgreSQL: pgsql/src/include/catalog/index.h,v 1.55 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -37,7 +37,8 @@ extern Oid index_create(Oid heapRelationId, Oid *classObjectId, bool primary, bool isconstraint, - bool allow_system_table_mods); + bool allow_system_table_mods, + bool skip_build); extern void index_drop(Oid indexId); diff --git a/src/include/commands/cluster.h b/src/include/commands/cluster.h index 72295803abb..5474fdd5490 100644 --- a/src/include/commands/cluster.h +++ b/src/include/commands/cluster.h @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994-5, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/commands/cluster.h,v 1.20 2003/11/29 22:40:59 pgsql Exp $ + * $PostgreSQL: pgsql/src/include/commands/cluster.h,v 1.21 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -20,5 +20,9 @@ extern void cluster(ClusterStmt *stmt); extern void rebuild_relation(Relation OldHeap, Oid indexOid); +extern Oid make_new_heap(Oid OIDOldHeap, const char *NewName); +extern List *get_indexattr_list(Relation OldHeap, Oid OldIndex); +extern void rebuild_indexes(Oid OIDOldHeap, List *indexes); +extern void swap_relfilenodes(Oid r1, Oid r2); #endif /* CLUSTER_H */ diff --git a/src/include/commands/defrem.h b/src/include/commands/defrem.h index 00f5fa1a480..78fe4ab9071 100644 --- a/src/include/commands/defrem.h +++ b/src/include/commands/defrem.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/commands/defrem.h,v 1.54 2004/02/21 00:34:53 tgl Exp $ + * $PostgreSQL: pgsql/src/include/commands/defrem.h,v 1.55 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -22,11 +22,15 @@ extern void DefineIndex(RangeVar *heapRelation, char *indexRelationName, char *accessMethodName, List *attributeList, + Expr *predicate, + List *rangetable, bool unique, bool primary, bool isconstraint, - Expr *predicate, - List *rangetable); + bool is_alter_table, + bool check_rights, + bool skip_build, + bool quiet); extern void RemoveIndex(RangeVar *relation, DropBehavior behavior); extern void ReindexIndex(RangeVar *indexRelation, bool force); extern void ReindexTable(RangeVar *relation, bool force); diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 889b4dfa626..f9f03c1bd03 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.15 2004/03/23 19:35:17 tgl Exp $ + * $PostgreSQL: pgsql/src/include/commands/tablecmds.h,v 1.16 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -16,46 +16,17 @@ #include "nodes/parsenodes.h" -extern void AlterTableAddColumn(Oid myrelid, bool recurse, ColumnDef *colDef); -extern void AlterTableAlterColumnDropNotNull(Oid myrelid, bool recurse, - const char *colName); - -extern void AlterTableAlterColumnSetNotNull(Oid myrelid, bool recurse, - const char *colName); - -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 recurse, bool recursing, - const char *colName, - DropBehavior behavior); +extern Oid DefineRelation(CreateStmt *stmt, char relkind); -extern void AlterTableAddConstraint(Oid myrelid, bool recurse, - List *newConstraints); +extern void RemoveRelation(const RangeVar *relation, DropBehavior behavior); -extern void AlterTableDropConstraint(Oid myrelid, bool recurse, - const char *constrName, - DropBehavior behavior); +extern void AlterTable(AlterTableStmt *stmt); -extern void AlterTableClusterOn(Oid relOid, const char *indexName); +extern void AlterTableInternal(Oid relid, List *cmds, bool recurse); extern void AlterTableCreateToastTable(Oid relOid, bool silent); -extern void AlterTableOwner(Oid relationOid, int32 newOwnerSysId); - -extern void AlterTableAlterOids(Oid myrelid, bool setOid, bool recurse, - DropBehavior behavior); - -extern Oid DefineRelation(CreateStmt *stmt, char relkind); - -extern void RemoveRelation(const RangeVar *relation, DropBehavior behavior); - extern void TruncateRelation(const RangeVar *relation); extern void renameatt(Oid myrelid, diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index d5d4f0832a7..a776607ec66 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.152 2004/04/01 21:28:46 tgl Exp $ + * $PostgreSQL: pgsql/src/include/nodes/nodes.h,v 1.153 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -200,6 +200,7 @@ typedef enum NodeTag T_UpdateStmt, T_SelectStmt, T_AlterTableStmt, + T_AlterTableCmd, T_AlterDomainStmt, T_SetOperationStmt, T_GrantStmt, diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index ab505939bc3..8eceb6e59cb 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.254 2004/03/11 01:47:41 ishii Exp $ + * $PostgreSQL: pgsql/src/include/nodes/parsenodes.h,v 1.255 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -762,44 +762,58 @@ typedef enum DropBehavior /* ---------------------- * Alter Table - * - * The fields are used in different ways by the different variants of - * this command. * ---------------------- */ typedef struct AlterTableStmt { NodeTag type; - char subtype; /*------------ - * A = add column - * T = alter column default - * N = alter column drop not null - * n = alter column set not null - * S = alter column statistics - * M = alter column storage - * D = drop column - * C = add constraint - * c = pre-processed add constraint - * (local in parser/analyze.c) - * X = drop constraint - * E = create toast table - * U = change owner - * L = CLUSTER ON - * o = DROP OIDS - *------------ - */ RangeVar *relation; /* table to work on */ + List *cmds; /* list of subcommands */ +} AlterTableStmt; + +typedef enum AlterTableType +{ + AT_AddColumn, /* add column */ + AT_ColumnDefault, /* alter column default */ + AT_DropNotNull, /* alter column drop not null */ + AT_SetNotNull, /* alter column set not null */ + AT_SetStatistics, /* alter column statistics */ + AT_SetStorage, /* alter column storage */ + AT_DropColumn, /* drop column */ + AT_DropColumnRecurse, /* internal to commands/tablecmds.c */ + AT_AddIndex, /* add index */ + AT_ReAddIndex, /* internal to commands/tablecmds.c */ + AT_AddConstraint, /* add constraint */ + AT_ProcessedConstraint, /* pre-processed add constraint + * (local in parser/analyze.c) */ + AT_DropConstraint, /* drop constraint */ + AT_DropConstraintQuietly, /* drop constraint, no error/warning + * (local in commands/tablecmds.c) */ + AT_AlterColumnType, /* alter column type */ + AT_ToastTable, /* create toast table */ + AT_ChangeOwner, /* change owner */ + AT_ClusterOn, /* CLUSTER ON */ + AT_DropOids /* SET WITHOUT OIDS */ +} AlterTableType; + +typedef struct AlterTableCmd /* one subcommand of an ALTER TABLE */ +{ + NodeTag type; + AlterTableType subtype; /* Type of table alteration to apply */ char *name; /* column or constraint name to act on, or * new owner */ - Node *def; /* definition of new column or constraint */ + Node *def; /* definition of new column, column type, + * index, or constraint */ + Node *transform; /* transformation expr for ALTER TYPE */ DropBehavior behavior; /* RESTRICT or CASCADE for DROP cases */ -} AlterTableStmt; +} AlterTableCmd; + /* ---------------------- * Alter Domain * * The fields are used in different ways by the different variants of - * this command. Subtypes should match AlterTable subtypes where possible. + * this command. * ---------------------- */ typedef struct AlterDomainStmt @@ -814,7 +828,7 @@ typedef struct AlterDomainStmt * U = change owner *------------ */ - List *typename; /* table to work on */ + List *typename; /* domain to work on */ char *name; /* column or constraint name to act on, or * new owner */ Node *def; /* definition of default or constraint */ @@ -922,6 +936,8 @@ typedef struct CreateStmt * Definitions for plain (non-FOREIGN KEY) constraints in CreateStmt * * XXX probably these ought to be unified with FkConstraints at some point? + * To this end we include CONSTR_FOREIGN in the ConstrType enum, even though + * the parser does not generate it. * * For constraints that use expressions (CONSTR_DEFAULT, CONSTR_CHECK) * we may have the expression in either "raw" form (an untransformed @@ -944,6 +960,7 @@ typedef enum ConstrType /* types of constraints */ CONSTR_NOTNULL, CONSTR_DEFAULT, CONSTR_CHECK, + CONSTR_FOREIGN, CONSTR_PRIMARY, CONSTR_UNIQUE, CONSTR_ATTR_DEFERRABLE, /* attributes for previous constraint node */ @@ -1291,7 +1308,7 @@ typedef struct FetchStmt typedef struct IndexStmt { NodeTag type; - char *idxname; /* name of the index */ + char *idxname; /* name of new index, or NULL for default */ RangeVar *relation; /* relation to build index on */ char *accessMethod; /* name of access method (eg. btree) */ List *indexParams; /* a list of IndexElem */ diff --git a/src/include/parser/analyze.h b/src/include/parser/analyze.h index e3cf88b4fd7..76c77025ece 100644 --- a/src/include/parser/analyze.h +++ b/src/include/parser/analyze.h @@ -6,7 +6,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.25 2004/01/23 02:13:12 neilc Exp $ + * $PostgreSQL: pgsql/src/include/parser/analyze.h,v 1.26 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -22,6 +22,9 @@ extern List *parse_analyze_varparams(Node *parseTree, Oid **paramTypes, extern List *parse_sub_analyze(Node *parseTree, ParseState *parentParseState); extern List *analyzeCreateSchemaStmt(CreateSchemaStmt *stmt); +extern char *makeObjectName(const char *name1, const char *name2, + const char *typename); + extern void CheckSelectForUpdate(Query *qry); #endif /* ANALYZE_H */ diff --git a/src/include/utils/builtins.h b/src/include/utils/builtins.h index 02e6cbca49d..59fb0a9a853 100644 --- a/src/include/utils/builtins.h +++ b/src/include/utils/builtins.h @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.236 2004/04/01 21:28:46 tgl Exp $ + * $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.237 2004/05/05 04:48:47 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -463,9 +463,11 @@ extern Datum pg_get_viewdef_name(PG_FUNCTION_ARGS); extern Datum pg_get_viewdef_name_ext(PG_FUNCTION_ARGS); extern Datum pg_get_indexdef(PG_FUNCTION_ARGS); extern Datum pg_get_indexdef_ext(PG_FUNCTION_ARGS); +extern char *pg_get_indexdef_string(Oid indexrelid); extern Datum pg_get_triggerdef(PG_FUNCTION_ARGS); extern Datum pg_get_constraintdef(PG_FUNCTION_ARGS); extern Datum pg_get_constraintdef_ext(PG_FUNCTION_ARGS); +extern char *pg_get_constraintdef_string(Oid constraintId); extern Datum pg_get_userbyid(PG_FUNCTION_ARGS); extern Datum pg_get_expr(PG_FUNCTION_ARGS); extern Datum pg_get_expr_ext(PG_FUNCTION_ARGS); |
