? src/bin/pg_dump/.pg_dump.c.swp Index: doc/src/sgml/datatype.sgml =================================================================== RCS file: /projects/cvsroot/pgsql-server/doc/src/sgml/datatype.sgml,v retrieving revision 1.98 diff -c -r1.98 datatype.sgml *** doc/src/sgml/datatype.sgml 2002/08/13 20:40:43 1.98 --- doc/src/sgml/datatype.sgml 2002/08/18 20:40:58 *************** *** 665,671 **** CREATE SEQUENCE tablename_colname_seq; CREATE TABLE tablename ( ! colname integer DEFAULT nextval('tablename_colname_seq') UNIQUE NOT NULL ); --- 665,671 ---- CREATE SEQUENCE tablename_colname_seq; CREATE TABLE tablename ( ! colname integer DEFAULT nextval('tablename_colname_seq') NOT NULL ); *************** *** 684,704 **** the use of more than 231 identifiers over the lifetime of the table. ! ! Implicit sequences supporting the serial types are ! not automatically dropped when a table containing a serial type ! is dropped. So, the following commands executed in order will likely fail: ! ! ! CREATE TABLE tablename (colname SERIAL); ! DROP TABLE tablename; ! CREATE TABLE tablename (colname SERIAL); ! ! The sequence will remain in the database until explicitly dropped using ! DROP SEQUENCE. (This annoyance will probably be ! fixed in some future release.) ! --- 684,704 ---- the use of more than 231 identifiers over the lifetime of the table. ! ! In 7.3 and later serial sequences are automatically ! dropped when the column is dropped, however this only occurs if ! the serial was created as a serial. A dump / reload from a prior ! database version will maintain the serial type exactly ! as it operated in that database, the sequence will be preserved on ! drop of the table. ! ! ! In 7.3 the implicit UNIQUE constraint was removed. If ! you wish a serial column to be UNIQUE or a ! PRIMARY KEY it must now be specified, same as with ! any other datatype. ! Index: src/backend/parser/analyze.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/parser/analyze.c,v retrieving revision 1.240 diff -c -r1.240 analyze.c *** src/backend/parser/analyze.c 2002/08/02 18:15:06 1.240 --- src/backend/parser/analyze.c 2002/08/18 20:41:37 *************** *** 881,891 **** column->constraints = lappend(column->constraints, constraint); constraint = makeNode(Constraint); - constraint->contype = CONSTR_UNIQUE; - constraint->name = NULL; /* assign later */ - column->constraints = lappend(column->constraints, constraint); - - constraint = makeNode(Constraint); constraint->contype = CONSTR_NOTNULL; column->constraints = lappend(column->constraints, constraint); } --- 881,886 ---- Index: src/backend/utils/adt/format_type.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/adt/format_type.c,v retrieving revision 1.31 diff -c -r1.31 format_type.c *** src/backend/utils/adt/format_type.c 2002/08/04 06:44:47 1.31 --- src/backend/utils/adt/format_type.c 2002/08/18 20:41:39 *************** *** 17,27 **** --- 17,34 ---- #include + #include "access/genam.h" + #include "access/heapam.h" + #include "catalog/catname.h" + #include "catalog/dependency.h" + #include "catalog/indexing.h" #include "catalog/namespace.h" + #include "catalog/pg_depend.h" #include "catalog/pg_type.h" #include "fmgr.h" #include "utils/builtins.h" #include "utils/datetime.h" + #include "utils/fmgroids.h" #include "utils/numeric.h" #include "utils/lsyscache.h" #include "utils/syscache.h" *************** *** 536,538 **** --- 543,610 ---- return buf; } + + /* + * pg_column_is_serial + * Determine whether a column is a serial column by viewing the + * dependency tree for the supplied attribute. + */ + Datum + pg_column_is_serial(PG_FUNCTION_ARGS) + { + Oid relOid = PG_GETARG_OID(0); + int32 attNum = PG_GETARG_INT32(1); + Relation depRel; + ScanKeyData key[3]; + int nkeys = 0; + SysScanDesc scan; + HeapTuple tup; + bool result = false; + + depRel = heap_openr(DependRelationName, RowExclusiveLock); + + ScanKeyEntryInitialize(&key[nkeys++], 0x0, + Anum_pg_depend_refclassid, F_OIDEQ, + ObjectIdGetDatum(RelOid_pg_class)); + ScanKeyEntryInitialize(&key[nkeys++], 0x0, + Anum_pg_depend_refobjid, F_OIDEQ, + ObjectIdGetDatum(relOid)); + ScanKeyEntryInitialize(&key[nkeys++], 0x0, + Anum_pg_depend_refobjsubid, F_INT4EQ, + Int32GetDatum(attNum)); + + scan = systable_beginscan(depRel, DependReferenceIndex, true, + SnapshotNow, nkeys, key); + + /* + * Run through list of dependencies seeking a sequence with an + * internal dependency type, or until a match is found. + */ + while (!result && HeapTupleIsValid(tup = systable_getnext(scan))) + { + Form_pg_depend ref = (Form_pg_depend) GETSTRUCT(tup); + + if (ref->deptype == DEPENDENCY_INTERNAL + && ref->classid == RelOid_pg_class) + { + HeapTuple relTup; + + relTup = SearchSysCache(RELOID, + ObjectIdGetDatum(ref->objid), + 0, 0, 0); + + /* This relation a sequence -- implying serial due to INTERNAL dependency */ + if (((Form_pg_class) GETSTRUCT(relTup))->relkind == RELKIND_SEQUENCE) + result = true; + + ReleaseSysCache(relTup); + } + } + + systable_endscan(scan); + + heap_close(depRel, RowExclusiveLock); + + PG_RETURN_BOOL(result); + } + Index: src/bin/pg_dump/pg_dump.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/pg_dump/pg_dump.c,v retrieving revision 1.285 diff -c -r1.285 pg_dump.c *** src/bin/pg_dump/pg_dump.c 2002/08/18 09:36:25 1.285 --- src/bin/pg_dump/pg_dump.c 2002/08/18 20:42:32 *************** *** 2056,2061 **** --- 2056,2062 ---- int i_relhasindex; int i_relhasrules; int i_relhasoids; + int i_isserial; /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); *************** *** 2076,2083 **** --- 2077,2095 ---- if (g_fout->remoteVersion >= 70300) { + /* + * Avoid pulling serial sequences. They're dumped as SERIAL. + */ appendPQExpBuffer(query, "SELECT pg_class.oid, relname, relacl, relkind, " + "case when relkind = '%c' and exists " + " (select * from pg_depend pd" + " join pg_class pc on (pd.refclassid = pc.oid) " + " join pg_namespace nsp on (pc.relnamespace = nsp.oid) " + " where pd.deptype = 'i' and pc.relname = 'pg_class' " + " and nsp.nspname = 'pg_catalog' " + " and pd.objid = pg_class.oid and pd.refobjsubid > 0) " + " then true else false end as isserial, " "relnamespace, " "(select usename from pg_user where relowner = usesysid) as usename, " "relchecks, reltriggers, " *************** *** 2085,2098 **** "from pg_class " "where relkind in ('%c', '%c', '%c') " "order by oid", ! RELKIND_RELATION, RELKIND_SEQUENCE, RELKIND_VIEW); } else if (g_fout->remoteVersion >= 70200) { /* before 7.3 there were no type relations with relkind 'c' */ appendPQExpBuffer(query, "SELECT pg_class.oid, relname, relacl, relkind, " ! "0::oid as relnamespace, " "(select usename from pg_user where relowner = usesysid) as usename, " "relchecks, reltriggers, " "relhasindex, relhasrules, relhasoids " --- 2097,2110 ---- "from pg_class " "where relkind in ('%c', '%c', '%c') " "order by oid", ! RELKIND_SEQUENCE, RELKIND_RELATION, RELKIND_VIEW, RELKIND_SEQUENCE); } else if (g_fout->remoteVersion >= 70200) { /* before 7.3 there were no type relations with relkind 'c' */ appendPQExpBuffer(query, "SELECT pg_class.oid, relname, relacl, relkind, " ! "false as isserial, 0::oid as relnamespace, " "(select usename from pg_user where relowner = usesysid) as usename, " "relchecks, reltriggers, " "relhasindex, relhasrules, relhasoids " *************** *** 2106,2112 **** /* all tables have oids in 7.1 */ appendPQExpBuffer(query, "SELECT pg_class.oid, relname, relacl, relkind, " ! "0::oid as relnamespace, " "(select usename from pg_user where relowner = usesysid) as usename, " "relchecks, reltriggers, " "relhasindex, relhasrules, 't'::bool as relhasoids " --- 2118,2124 ---- /* all tables have oids in 7.1 */ appendPQExpBuffer(query, "SELECT pg_class.oid, relname, relacl, relkind, " ! "false as isserial, 0::oid as relnamespace, " "(select usename from pg_user where relowner = usesysid) as usename, " "relchecks, reltriggers, " "relhasindex, relhasrules, 't'::bool as relhasoids " *************** *** 2122,2128 **** * if we have a view by looking for a rule in pg_rewrite. */ appendPQExpBuffer(query, ! "SELECT c.oid, relname, relacl, " "CASE WHEN relhasrules and relkind = 'r' " " and EXISTS(SELECT rulename FROM pg_rewrite r WHERE " " r.ev_class = c.oid AND r.ev_type = '1') " --- 2134,2140 ---- * if we have a view by looking for a rule in pg_rewrite. */ appendPQExpBuffer(query, ! "SELECT c.oid, relname, relacl, false as isserial, " "CASE WHEN relhasrules and relkind = 'r' " " and EXISTS(SELECT rulename FROM pg_rewrite r WHERE " " r.ev_class = c.oid AND r.ev_type = '1') " *************** *** 2175,2180 **** --- 2187,2193 ---- i_relhasindex = PQfnumber(res, "relhasindex"); i_relhasrules = PQfnumber(res, "relhasrules"); i_relhasoids = PQfnumber(res, "relhasoids"); + i_isserial = PQfnumber(res, "isserial"); for (i = 0; i < ntups; i++) { *************** *** 2190,2195 **** --- 2203,2209 ---- tblinfo[i].hasoids = (strcmp(PQgetvalue(res, i, i_relhasoids), "t") == 0); tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers)); + tblinfo[i].isserial = (strcmp(PQgetvalue(res, i, i_isserial), "t") == 0); /* other fields were zeroed above */ *************** *** 2365,2374 **** if (g_fout->remoteVersion >= 70300) { appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, attstattarget, " "attnotnull, atthasdef, attisdropped, " ! "pg_catalog.format_type(atttypid,atttypmod) as atttypname " "from pg_catalog.pg_attribute a " "where attrelid = '%s'::pg_catalog.oid " "and attnum > 0::pg_catalog.int2 " "order by attrelid, attnum", --- 2379,2398 ---- if (g_fout->remoteVersion >= 70300) { + /* + * The case around t.typname are to determine whether the int4 or int8 + * is really a serial or bigserial. + */ appendPQExpBuffer(q, "SELECT attnum, attname, atttypmod, attstattarget, " "attnotnull, atthasdef, attisdropped, " ! "case when t.typname = 'int4' " ! " and pg_column_is_serial(a.attrelid, attnum) then 'serial' " ! "when t.typname = 'int8' " ! " and pg_column_is_serial(a.attrelid, attnum) then 'bigserial' " ! "else pg_catalog.format_type(atttypid,atttypmod) " ! "end as atttypname " "from pg_catalog.pg_attribute a " + " join pg_catalog.pg_type t on (a.atttypid = t.oid) " "where attrelid = '%s'::pg_catalog.oid " "and attnum > 0::pg_catalog.int2 " "order by attrelid, attnum", *************** *** 2505,2511 **** adnum, tblinfo[i].relname); exit_nicely(); } ! tblinfo[i].adef_expr[adnum-1] = strdup(PQgetvalue(res, j, 1)); } PQclear(res); } --- 2529,2538 ---- adnum, tblinfo[i].relname); exit_nicely(); } ! ! if (strcmp(tblinfo[i].atttypnames[adnum-1], "serial") != 0 ! && strcmp(tblinfo[i].atttypnames[adnum-1], "bigserial") != 0) ! tblinfo[i].adef_expr[adnum-1] = strdup(PQgetvalue(res, j, 1)); } PQclear(res); } *************** *** 4902,4908 **** { int i; ! /* Dump sequences first, in case they are referenced in table defn's */ for (i = 0; i < numTables; i++) { TableInfo *tbinfo = &tblinfo[i]; --- 4929,4935 ---- { int i; ! /* Dump non-serial sequences first, in case they are referenced in table defn's */ for (i = 0; i < numTables; i++) { TableInfo *tbinfo = &tblinfo[i]; *************** *** 5716,5722 **** * data */ ! if (!dataOnly) { resetPQExpBuffer(delqry); --- 5743,5749 ---- * data */ ! if (!dataOnly && !tbinfo->isserial) { resetPQExpBuffer(delqry); Index: src/bin/pg_dump/pg_dump.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/pg_dump/pg_dump.h,v retrieving revision 1.96 diff -c -r1.96 pg_dump.h *** src/bin/pg_dump/pg_dump.h 2002/08/18 09:36:26 1.96 --- src/bin/pg_dump/pg_dump.h 2002/08/18 20:42:33 *************** *** 110,115 **** --- 110,116 ---- bool hasoids; /* does it have OIDs? */ int ncheck; /* # of CHECK expressions */ int ntrig; /* # of triggers */ + bool isserial; /* Is this a serial sequence? */ bool interesting; /* true if need to collect more data */ bool dump; /* true if we want to dump it */ Index: src/include/catalog/pg_proc.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/catalog/pg_proc.h,v retrieving revision 1.256 diff -c -r1.256 pg_proc.h *** src/include/catalog/pg_proc.h 2002/08/17 13:04:15 1.256 --- src/include/catalog/pg_proc.h 2002/08/18 20:43:36 *************** *** 1669,1674 **** --- 1669,1677 ---- DATA(insert OID = 1359 ( timestamptz PGNSP PGUID 12 f f t f i 2 1184 "1082 1266" datetimetz_timestamptz - _null_ )); DESCR("convert date and time with time zone to timestamp with time zone"); + DATA(insert OID = 1362 ( pg_column_is_serial PGNSP PGUID 12 f f t f i 2 16 "26 21" pg_column_is_serial - _null_ )); + DESCR("determine if a relations column is serial or not"); + DATA(insert OID = 1364 ( time PGNSP PGUID 14 f f t f i 1 1083 "702" "select time(cast($1 as timestamp without time zone))" - _null_ )); DESCR("convert abstime to time"); Index: src/include/utils/builtins.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/include/utils/builtins.h,v retrieving revision 1.192 diff -c -r1.192 builtins.h *** src/include/utils/builtins.h 2002/08/16 23:01:21 1.192 --- src/include/utils/builtins.h 2002/08/18 20:43:48 *************** *** 655,660 **** --- 655,661 ---- extern char *format_type_with_typemod(Oid type_oid, int32 typemod); extern Datum oidvectortypes(PG_FUNCTION_ARGS); extern int32 type_maximum_size(Oid type_oid, int32 typemod); + extern Datum pg_column_is_serial(PG_FUNCTION_ARGS); /* quote.c */ extern Datum quote_ident(PG_FUNCTION_ARGS); Index: src/test/regress/expected/copy2.out =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/copy2.out,v retrieving revision 1.4 diff -c -r1.4 copy2.out *** src/test/regress/expected/copy2.out 2002/08/02 18:15:09 1.4 --- src/test/regress/expected/copy2.out 2002/08/18 20:44:32 *************** *** 6,12 **** e text ); NOTICE: CREATE TABLE will create implicit sequence 'x_a_seq' for SERIAL column 'x.a' - NOTICE: CREATE TABLE / UNIQUE will create implicit index 'x_a_key' for table 'x' CREATE FUNCTION fn_x_before () RETURNS OPAQUE AS ' BEGIN NEW.e := ''before trigger fired''::text; --- 6,11 ---- Index: src/test/regress/expected/create_misc.out =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/create_misc.out,v retrieving revision 1.17 diff -c -r1.17 create_misc.out *** src/test/regress/expected/create_misc.out 2002/07/11 21:36:20 1.17 --- src/test/regress/expected/create_misc.out 2002/08/18 20:44:33 *************** *** 137,143 **** --- CREATE TABLE serialTest (f1 text, f2 serial); NOTICE: CREATE TABLE will create implicit sequence 'serialtest_f2_seq' for SERIAL column 'serialtest.f2' - NOTICE: CREATE TABLE / UNIQUE will create implicit index 'serialtest_f2_key' for table 'serialtest' INSERT INTO serialTest VALUES ('foo'); INSERT INTO serialTest VALUES ('bar'); INSERT INTO serialTest VALUES ('force', 100); --- 137,142 ---- Index: src/test/regress/expected/sanity_check.out =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/test/regress/expected/sanity_check.out,v retrieving revision 1.21 diff -c -r1.21 sanity_check.out *** src/test/regress/expected/sanity_check.out 2002/08/10 15:54:04 1.21 --- src/test/regress/expected/sanity_check.out 2002/08/18 20:44:34 *************** *** 59,69 **** pg_trigger | t pg_type | t road | t - serialtest | t shighway | t tenk1 | t tenk2 | t ! (53 rows) -- -- another sanity check: every system catalog that has OIDs should have --- 59,68 ---- pg_trigger | t pg_type | t road | t shighway | t tenk1 | t tenk2 | t ! (52 rows) -- -- another sanity check: every system catalog that has OIDs should have