diff --git a/src/bin/pg_dump/pg_dump.cpp b/src/bin/pg_dump/pg_dump.cpp index 408094c4b..7deb17049 100644 --- a/src/bin/pg_dump/pg_dump.cpp +++ b/src/bin/pg_dump/pg_dump.cpp @@ -7169,6 +7169,7 @@ TableInfo* getTables(Archive* fout, int* numTables) } tblinfo[i].autoinc_attnum = 0; tblinfo[i].autoincconstraint = 0; + tblinfo[i].autoincindex = 0; #ifdef PGXC /* Not all the tables have pgxc locator Data */ if (PQgetisnull(res, i, i_pgxclocatortype)) { @@ -7858,6 +7859,12 @@ void getIndexes(Archive* fout, TableInfo tblinfo[], int numTables) } else { /* Plain secondary index */ indxinfo[j].indexconstraint = 0; + if (tbinfo->autoinc_attnum > 0 && + tbinfo->autoincconstraint == 0 && + tbinfo->autoincindex == 0 && + (Oid)tbinfo->autoinc_attnum == indxinfo[j].indkeys[0]) { + tbinfo->autoincindex = indxinfo[j].dobj.dumpId; + } } } if (0 == contrants_processed) { @@ -19288,6 +19295,26 @@ static void dumpTableSchema(Archive* fout, TableInfo* tbinfo) appendPQExpBuffer(q, "CONSTRAINT %s ", fmtId(coninfo->dobj.name)); dumpUniquePrimaryDef(q, coninfo, indxinfo, isBcompatibility); actual_atts++; + } else if (tbinfo->autoincindex != 0) { + ddr_Assert(actual_atts != 0); + IndxInfo* indxinfo = (IndxInfo*)findObjectByDumpId(tbinfo->autoincindex); + uint32 posoffset; + const char* endpos = NULL; + char* usingpos = strstr(indxinfo->indexdef, " USING "); + if (usingpos != NULL) { + endpos = strstr(usingpos, " TABLESPACE "); + if (endpos != NULL) { + posoffset = (uint32)(endpos - usingpos); + } else { + posoffset = (uint32)strlen(usingpos); + } + /* get "USING ... (keypart, ...)" from indexdef */ + char indexpartdef[posoffset + 1] = {0}; + errno_t rc = strncpy_s(indexpartdef, posoffset + 1, usingpos, posoffset); + securec_check_c(rc, "\0", "\0"); + appendPQExpBuffer(q, ",\n INDEX %s%s", fmtId(indxinfo->dobj.name), indexpartdef); + actual_atts++; + } } if (actual_atts) { @@ -20282,7 +20309,7 @@ static void dumpIndex(Archive* fout, IndxInfo* indxinfo) * emitted comment has to be shown as depending on the constraint, not * the index, in such cases. */ - if (!is_constraint) { + if (!is_constraint && (tbinfo->autoincconstraint > 0 || indxinfo->dobj.dumpId != tbinfo->autoincindex)) { if (binary_upgrade) { binary_upgrade_set_pg_class_oids(fout, q, indxinfo->dobj.catId.oid, true, false); diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 49a1d7fbd..4414e059d 100644 --- a/src/bin/pg_dump/pg_dump.h +++ b/src/bin/pg_dump/pg_dump.h @@ -242,6 +242,7 @@ typedef struct _tableInfo { bool interesting; /* true if need to collect more data */ int autoinc_attnum; DumpId autoincconstraint; + DumpId autoincindex; char* autoinc_seqname; #ifdef PGXC /* PGXC table locator Data */ diff --git a/src/common/backend/parser/parse_utilcmd.cpp b/src/common/backend/parser/parse_utilcmd.cpp index ec2bb8243..926fc7f9c 100644 --- a/src/common/backend/parser/parse_utilcmd.cpp +++ b/src/common/backend/parser/parse_utilcmd.cpp @@ -8023,8 +8023,8 @@ static void CheckAutoIncrementIndex(CreateStmtContext *cxt) continue; } Assert(cons->keys->length > 0); - Value *name = (Value *)lfirst(cons->keys->head); - if (strcasecmp(name->val.str, column->colname) == 0) { + IndexElem *elem = (IndexElem *)lfirst(cons->keys->head); + if (elem->name != NULL && strcasecmp(elem->name, column->colname) == 0) { has_found = true; break; } @@ -8041,7 +8041,7 @@ static void CheckAutoIncrementIndex(CreateStmtContext *cxt) } Assert(index->indexParams->length > 0); IndexElem *elem = (IndexElem *)lfirst(index->indexParams->head); - if (strcasecmp(elem->name, column->colname) == 0) { + if (elem->name != NULL && strcasecmp(elem->name, column->colname) == 0) { has_found = true; break; } diff --git a/src/common/backend/utils/adt/ruleutils.cpp b/src/common/backend/utils/adt/ruleutils.cpp index e7855768c..b6bfe9278 100644 --- a/src/common/backend/utils/adt/ruleutils.cpp +++ b/src/common/backend/utils/adt/ruleutils.cpp @@ -2201,25 +2201,23 @@ static void get_index_list_info(Oid tableoid, StringInfo buf, const char* relnam constriantid = get_index_constraint(index->indexrelid); if (OidIsValid(constriantid)) { - if (tableinfo->autoinc_consoid == constriantid) { - continue; - } + if (tableinfo->autoinc_consoid != constriantid) { + HeapTuple tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constriantid)); + if (!HeapTupleIsValid(tup)) { /* should not happen */ + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("cache lookup failed for constraint %u", constriantid))); + } + Form_pg_constraint conForm = (Form_pg_constraint)GETSTRUCT(tup); - HeapTuple tup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(constriantid)); - if (!HeapTupleIsValid(tup)) { /* should not happen */ - ereport(ERROR, - (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("cache lookup failed for constraint %u", constriantid))); - } - Form_pg_constraint conForm = (Form_pg_constraint)GETSTRUCT(tup); - - if (conForm->contype == CONSTRAINT_UNIQUE || conForm->contype == CONSTRAINT_PRIMARY) { - get_table_constraint_info(conForm, tup, buf, constriantid, relname); - appendStringInfo(buf, ";"); - } else { - appendStringInfo(buf, "\n%s;", pg_get_indexdef_worker(index->indexrelid, 0, NULL, false, true, 0)); - } - /* Cleanup */ - ReleaseSysCache(tup); + if (conForm->contype == CONSTRAINT_UNIQUE || conForm->contype == CONSTRAINT_PRIMARY) { + get_table_constraint_info(conForm, tup, buf, constriantid, relname); + appendStringInfo(buf, ";"); + } else { + appendStringInfo(buf, "\n%s;", pg_get_indexdef_worker(index->indexrelid, 0, NULL, false, true, 0)); + } + /* Cleanup */ + ReleaseSysCache(tup); + } } else { appendStringInfo(buf, "\n%s;", pg_get_indexdef_worker(index->indexrelid, 0, NULL, false, true, 0)); diff --git a/src/gausskernel/optimizer/commands/tablecmds.cpp b/src/gausskernel/optimizer/commands/tablecmds.cpp index e88d6d437..e531edde9 100644 --- a/src/gausskernel/optimizer/commands/tablecmds.cpp +++ b/src/gausskernel/optimizer/commands/tablecmds.cpp @@ -8329,6 +8329,9 @@ static void ATRewriteCatalogs(List** wqueue, LOCKMODE lockmode) AlterTableCreateToastTable(tab->relid, toast_reloptions); relation_close(rel, NoLock); } + if (tab->relkind == RELKIND_RELATION) { + CheckRelAutoIncrementIndex(tab->relid, NoLock); + } } } @@ -13172,24 +13175,6 @@ bool ConstraintSatisfyAutoIncrement(HeapTuple tuple, TupleDesc desc, AttrNumber pfree(keys); return false; } - -static void CheckAutoIncrementConstraints(Relation conrel, SysScanDesc scan, HeapTuple tuple, - AttrNumber attrnum, bool satisfy_autoinc) -{ - if (attrnum == 0 || satisfy_autoinc) { - return; - } - Form_pg_constraint con; - while (HeapTupleIsValid(tuple = systable_getnext(scan))) { - con = (Form_pg_constraint)GETSTRUCT(tuple); - if (ConstraintSatisfyAutoIncrement(tuple, RelationGetDescr(conrel), attrnum, con->contype)) { - return; - } - } - ereport(ERROR, (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - (errmsg("auto_increment column must be defined as a unique or primary key")))); -} - /* * ALTER TABLE DROP CONSTRAINT * @@ -13207,8 +13192,6 @@ static void ATExecDropConstraint(Relation rel, const char* constrName, DropBehav HeapTuple tuple; bool found = false; bool is_no_inherit_constraint = false; - AttrNumber autoinc_attnum = RelAutoIncAttrNum(rel); - bool satisfy_autoinc = false; /* At top level, permission check was done in ATPrepCmd, else do it */ if (recursing) @@ -13229,9 +13212,6 @@ static void ATExecDropConstraint(Relation rel, const char* constrName, DropBehav con = (Form_pg_constraint)GETSTRUCT(tuple); if (strcmp(NameStr(con->conname), constrName) != 0) { - if (ConstraintSatisfyAutoIncrement(tuple, RelationGetDescr(conrel), autoinc_attnum, con->contype)) { - satisfy_autoinc = true; - } continue; } @@ -13294,7 +13274,6 @@ static void ATExecDropConstraint(Relation rel, const char* constrName, DropBehav /* constraint found and dropped -- no need to keep looping */ break; } - CheckAutoIncrementConstraints(conrel, scan, tuple, autoinc_attnum, satisfy_autoinc); systable_endscan(scan); @@ -28611,3 +28590,43 @@ static void CopyTempAutoIncrement(Relation oldrel, Relation newrel) tmptable_autoinc_reset(newrel->rd_rel->relfilenode, *value); } } + +void CheckRelAutoIncrementIndex(Oid relid, LOCKMODE lockmode) +{ + List* idxoidlist = NULL; + bool found = false; + Relation rel = relation_open(relid, lockmode); + AttrNumber autoinc_attnum = RelAutoIncAttrNum(rel); + + if (autoinc_attnum <= 0) { + relation_close(rel, lockmode); + return; + } + + if (!rel->rd_rel->relhasindex) { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + (errmsg("Incorrect table definition, auto_increment column must be defined as a key")))); + } + + idxoidlist = RelationGetIndexList(rel); + relation_close(rel, lockmode); + + foreach_cell(l, idxoidlist) { + Relation idxrel = index_open(lfirst_oid(l), AccessShareLock); + Form_pg_index index = idxrel->rd_index; + + if (IndexIsValid(index) && (index->indisunique || index->indisprimary) && + index->indkey.values[0] == autoinc_attnum) { + found = true; + index_close(idxrel, AccessShareLock); + break; + } + index_close(idxrel, AccessShareLock); + } + + list_free(idxoidlist); + if (!found) { + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + (errmsg("Incorrect table definition, auto_increment column must be defined as a key")))); + } +} \ No newline at end of file diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 998a4ea08..b190a459f 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -160,4 +160,5 @@ extern int getPartitionElementsIndexByOid(Relation partTableRel, Oid partOid); extern void SetPartionIndexType(IndexStmt* stmt, Relation rel, bool is_alter_table); extern bool ConstraintSatisfyAutoIncrement(HeapTuple tuple, TupleDesc desc, AttrNumber attrnum, char contype); +extern void CheckRelAutoIncrementIndex(Oid relid, LOCKMODE lockmode); #endif /* TABLECMDS_H */ diff --git a/src/test/regress/expected/test_auto_increment.out b/src/test/regress/expected/test_auto_increment.out index b8a9f8d23..51ad1d667 100644 --- a/src/test/regress/expected/test_auto_increment.out +++ b/src/test/regress/expected/test_auto_increment.out @@ -9,22 +9,117 @@ ERROR: auto_increment is supported only in B-format database create database autoinc_b_db with dbcompatibility = 'B'; \c autoinc_b_db -- test CREATE TABLE with AUTO_INCREMENT --- syntax error -CREATE TABLE test_create_autoinc_err(id int auto_increment key, name varchar(200),a int); -ERROR: syntax error at or near "key" -LINE 1: ...BLE test_create_autoinc_err(id int auto_increment key, name ... - ^ -CREATE TABLE test_create_autoinc_err(id int auto_increment unique key, name varchar(200),a int); -ERROR: syntax error at or near "key" -LINE 1: ...t_create_autoinc_err(id int auto_increment unique key, name ... - ^ +-- test create table +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT INDEX, + b varchar(32) +); -- ERROR +ERROR: syntax error at or near "INDEX" +LINE 2: a int AUTO_INCREMENT INDEX, + ^ +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT KEY, + b varchar(32) +); -- ERROR +ERROR: syntax error at or near "KEY" +LINE 2: a int AUTO_INCREMENT KEY, + ^ +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT PRIMARY KEY, + b varchar(32) +); +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_create_autoinc_pkey" for table "test_create_autoinc" +\d test_create_autoinc; + Table "public.test_create_autoinc" + Column | Type | Modifiers +--------+-----------------------+------------------------- + a | integer | not null AUTO_INCREMENT + b | character varying(32) | +Indexes: + "test_create_autoinc_pkey" PRIMARY KEY, btree (a) TABLESPACE pg_default + +DROP TABLE test_create_autoinc; +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT UNIQUE KEY, + b varchar(32) +); -- ERROR +ERROR: syntax error at or near "KEY" +LINE 2: a int AUTO_INCREMENT UNIQUE KEY, + ^ +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT UNIQUE, + b varchar(32) +); +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +NOTICE: CREATE TABLE / UNIQUE will create implicit index "test_create_autoinc_a_key" for table "test_create_autoinc" +\d test_create_autoinc; + Table "public.test_create_autoinc" + Column | Type | Modifiers +--------+-----------------------+------------------------- + a | integer | not null AUTO_INCREMENT + b | character varying(32) | +Indexes: + "test_create_autoinc_a_key" UNIQUE CONSTRAINT, btree (a) TABLESPACE pg_default + +DROP TABLE test_create_autoinc; +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + UNIQUE ((b||'1'),a) +); -- ERROR +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +ERROR: Incorrect table definition, auto_increment column must be defined as a key +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + UNIQUE (a,b) +); +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +NOTICE: CREATE TABLE / UNIQUE will create implicit index "test_create_autoinc_a_b_key" for table "test_create_autoinc" +\d test_create_autoinc; + Table "public.test_create_autoinc" + Column | Type | Modifiers +--------+-----------------------+------------------------- + a | integer | not null AUTO_INCREMENT + b | character varying(32) | +Indexes: + "test_create_autoinc_a_b_key" UNIQUE CONSTRAINT, btree (a, b) TABLESPACE pg_default + +DROP TABLE test_create_autoinc; +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + PRIMARY KEY ((b||'1'),a) +); -- ERROR +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +ERROR: Incorrect table definition, auto_increment column must be defined as a key +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + PRIMARY KEY (a,b) +); +NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_a_seq" for serial column "test_create_autoinc.a" +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "test_create_autoinc_pkey" for table "test_create_autoinc" +\d test_create_autoinc; + Table "public.test_create_autoinc" + Column | Type | Modifiers +--------+-----------------------+------------------------- + a | integer | not null AUTO_INCREMENT + b | character varying(32) | not null +Indexes: + "test_create_autoinc_pkey" PRIMARY KEY, btree (a, b) TABLESPACE pg_default + +DROP TABLE test_create_autoinc; +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + KEY (b,a) +); -- ERROR +ERROR: syntax error at or near "(" +LINE 4: KEY (b,a) + ^ -- constraint error -CREATE TABLE test_create_autoinc_err(id int auto_increment, name varchar(200),a int, primary key(name, id)); -NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_err_id_seq" for serial column "test_create_autoinc_err.id" -ERROR: Incorrect table definition, auto_increment column must be defined as a key -CREATE TABLE test_create_autoinc_err(id int auto_increment, name varchar(200),a int, unique(name, id)); -NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_err_id_seq" for serial column "test_create_autoinc_err.id" -ERROR: Incorrect table definition, auto_increment column must be defined as a key CREATE TABLE test_create_autoinc_err(id int auto_increment primary key CHECK (id < 500), name varchar(200),a int); NOTICE: CREATE TABLE will create implicit sequence "test_create_autoinc_err_id_seq" for serial column "test_create_autoinc_err.id" ERROR: check constraint cannot refer to an auto_increment column @@ -578,7 +673,7 @@ ALTER TABLE test_alter_autoinc DROP COLUMN id, ADD new_id NUMERIC(10,4) AUTO_INC NOTICE: ALTER TABLE will create implicit sequence "test_alter_autoinc_new_id_seq" for serial column "test_alter_autoinc.new_id" ERROR: The datatype of column 'new_id' does not support auto_increment ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_pkey; -ERROR: auto_increment column must be defined as a unique or primary key +ERROR: Incorrect table definition, auto_increment column must be defined as a key ALTER TABLE test_alter_autoinc auto_increment=-1; ERROR: syntax error at or near "-" LINE 1: ALTER TABLE test_alter_autoinc auto_increment=-1; @@ -880,7 +975,7 @@ ERROR: cannot alter auto_increment column "id" default ALTER TABLE test_alter_autoinc_ltemp DROP COLUMN id, ADD new_id NUMERIC(10,4) AUTO_INCREMENT PRIMARY KEY; ERROR: must have at least one column ALTER TABLE test_alter_autoinc_ltemp DROP CONSTRAINT test_alter_autoinc_ltemp_u1; -ERROR: auto_increment column must be defined as a unique or primary key +ERROR: Incorrect table definition, auto_increment column must be defined as a key ALTER TABLE test_alter_autoinc_ltemp auto_increment=-1; ERROR: syntax error at or near "-" LINE 1: ALTER TABLE test_alter_autoinc_ltemp auto_increment=-1; @@ -980,6 +1075,40 @@ SELECT col1, col2, col3 FROM test_alter_partition_autoinc ORDER BY 1, 2; (1 row) DROP TABLE test_alter_partition_autoinc; +-- test alter table add column, add/drop index +CREATE TABLE test_alter_autoinc( + a int, + b varchar(32) +); +ALTER TABLE test_alter_autoinc ADD COLUMN seq int AUTO_INCREMENT, ADD CONSTRAINT test_alter_autoinc_uk UNIQUE ((seq + 1), seq); -- ERROR +NOTICE: ALTER TABLE will create implicit sequence "test_alter_autoinc_seq_seq" for serial column "test_alter_autoinc.seq" +ERROR: Incorrect table definition, auto_increment column must be defined as a key +ALTER TABLE test_alter_autoinc ADD COLUMN seq int AUTO_INCREMENT, ADD CONSTRAINT test_alter_autoinc_uk UNIQUE (seq); +NOTICE: ALTER TABLE will create implicit sequence "test_alter_autoinc_seq_seq" for serial column "test_alter_autoinc.seq" +NOTICE: ALTER TABLE / ADD UNIQUE will create implicit index "test_alter_autoinc_uk" for table "test_alter_autoinc" +CREATE INDEX test_alter_autoinc_idx1 ON test_alter_autoinc (seq,a); +SELECT pg_get_tabledef('test_alter_autoinc'::regclass); + pg_get_tabledef +-------------------------------------------------------------------------------------------------------- + SET search_path = public; + + CREATE TABLE test_alter_autoinc ( + + a integer, + + b character varying(32), + + seq integer AUTO_INCREMENT NOT NULL, + + CONSTRAINT test_alter_autoinc_uk UNIQUE (seq) + + ) AUTO_INCREMENT = 1 + + WITH (orientation=row, compression=no); + + CREATE INDEX test_alter_autoinc_idx1 ON test_alter_autoinc USING btree (seq, a) TABLESPACE pg_default; +(1 row) + +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_uk; -- ERROR +ERROR: Incorrect table definition, auto_increment column must be defined as a key +DROP INDEX test_alter_autoinc_idx1; +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_uk, ADD CONSTRAINT test_alter_autoinc_pk PRIMARY KEY (seq); +NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index "test_alter_autoinc_pk" for table "test_alter_autoinc" +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_pk; -- ERROR +ERROR: Incorrect table definition, auto_increment column must be defined as a key +DROP TABLE test_alter_autoinc; -- auto_increment in table with single column PRIMARY KEY CREATE TABLE single_autoinc_pk(col int auto_increment PRIMARY KEY) AUTO_INCREMENT = 10; NOTICE: CREATE TABLE will create implicit sequence "single_autoinc_pk_col_seq" for serial column "single_autoinc_pk.col" diff --git a/src/test/regress/sql/test_auto_increment.sql b/src/test/regress/sql/test_auto_increment.sql index 599232565..dc010441a 100644 --- a/src/test/regress/sql/test_auto_increment.sql +++ b/src/test/regress/sql/test_auto_increment.sql @@ -6,12 +6,71 @@ ALTER TABLE test_create_autoinc_err AUTO_INCREMENT=100; create database autoinc_b_db with dbcompatibility = 'B'; \c autoinc_b_db -- test CREATE TABLE with AUTO_INCREMENT --- syntax error -CREATE TABLE test_create_autoinc_err(id int auto_increment key, name varchar(200),a int); -CREATE TABLE test_create_autoinc_err(id int auto_increment unique key, name varchar(200),a int); +-- test create table +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT INDEX, + b varchar(32) +); -- ERROR + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT KEY, + b varchar(32) +); -- ERROR + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT PRIMARY KEY, + b varchar(32) +); +\d test_create_autoinc; +DROP TABLE test_create_autoinc; + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT UNIQUE KEY, + b varchar(32) +); -- ERROR + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT UNIQUE, + b varchar(32) +); +\d test_create_autoinc; +DROP TABLE test_create_autoinc; + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + UNIQUE ((b||'1'),a) +); -- ERROR + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + UNIQUE (a,b) +); +\d test_create_autoinc; +DROP TABLE test_create_autoinc; + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + PRIMARY KEY ((b||'1'),a) +); -- ERROR + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + PRIMARY KEY (a,b) +); +\d test_create_autoinc; +DROP TABLE test_create_autoinc; + +CREATE TABLE test_create_autoinc( + a int AUTO_INCREMENT, + b varchar(32), + KEY (b,a) +); -- ERROR + -- constraint error -CREATE TABLE test_create_autoinc_err(id int auto_increment, name varchar(200),a int, primary key(name, id)); -CREATE TABLE test_create_autoinc_err(id int auto_increment, name varchar(200),a int, unique(name, id)); CREATE TABLE test_create_autoinc_err(id int auto_increment primary key CHECK (id < 500), name varchar(200),a int); CREATE TABLE test_create_autoinc_err(id int auto_increment primary key, name varchar(200),a int CHECK ((id + a) < 500)); CREATE TABLE test_create_autoinc_err(id int auto_increment primary key DEFAULT 100, name varchar(200),a int); @@ -322,6 +381,21 @@ INSERT INTO test_alter_partition_autoinc VALUES(1,1,DEFAULT); SELECT col1, col2, col3 FROM test_alter_partition_autoinc ORDER BY 1, 2; DROP TABLE test_alter_partition_autoinc; +-- test alter table add column, add/drop index +CREATE TABLE test_alter_autoinc( + a int, + b varchar(32) +); +ALTER TABLE test_alter_autoinc ADD COLUMN seq int AUTO_INCREMENT, ADD CONSTRAINT test_alter_autoinc_uk UNIQUE ((seq + 1), seq); -- ERROR +ALTER TABLE test_alter_autoinc ADD COLUMN seq int AUTO_INCREMENT, ADD CONSTRAINT test_alter_autoinc_uk UNIQUE (seq); +CREATE INDEX test_alter_autoinc_idx1 ON test_alter_autoinc (seq,a); +SELECT pg_get_tabledef('test_alter_autoinc'::regclass); +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_uk; -- ERROR +DROP INDEX test_alter_autoinc_idx1; +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_uk, ADD CONSTRAINT test_alter_autoinc_pk PRIMARY KEY (seq); +ALTER TABLE test_alter_autoinc DROP CONSTRAINT test_alter_autoinc_pk; -- ERROR +DROP TABLE test_alter_autoinc; + -- auto_increment in table with single column PRIMARY KEY CREATE TABLE single_autoinc_pk(col int auto_increment PRIMARY KEY) AUTO_INCREMENT = 10; INSERT INTO single_autoinc_pk VALUES(NULL);