Index: src/bin/pg_dump/pg_dump.c =================================================================== RCS file: /var/cvsup/pgsql/src/bin/pg_dump/pg_dump.c,v retrieving revision 1.236 diff -c -r1.236 pg_dump.c *** src/bin/pg_dump/pg_dump.c 28 Oct 2001 06:25:58 -0000 1.236 --- src/bin/pg_dump/pg_dump.c 16 Dec 2001 01:24:43 -0000 *************** *** 2652,2657 **** --- 2652,2713 ---- } /* + * getSerialSequenceName + * return the name of a SERIAL-created sequence for this + * relation or NULL if none is found. + * + * XXX: This does not yet account for multiple SERIAL sequences + * on a relation, but it is /better/ than the previous + * sequence dumping logic. + * -- dbv 20011215 + */ + char* + getSerialSequenceName(const char* relname) + { + /* There is no direct relationship between a table and + * a SERIAL-created sequence in the system catalog, so + * we've gotta get the pg_attrdef.adsrc for the default + * and extract the sequence name from the nextval(...) + * function call definition. This is a kludge. + * -- dbv 20011215 + */ + int fnum; + char* o_adsrc; + char* seqname; + PQExpBuffer query; + PGresult* res; + + query = createPQExpBuffer(); + appendPQExpBuffer(query, "SELECT adsrc FROM pg_attrdef WHERE adrelid=(SELECT oid FROM pg_class WHERE relname='%s')",relname); + res = PQexec(g_conn,query->data); + + if( !res || PQresultStatus(res) != PGRES_TUPLES_OK){ + write_msg(NULL, "query to lookup SERIAL-create sequences for relation '%s' failed: %s", + relname, + PQerrorMessage(g_conn)); + exit_nicely(); + } + + if( PQntuples(res) <= 0 ){ + /* no SERIAL-created sequences on this relation */ + PQclear(res); + destroyPQExpBuffer(query); + return NULL; + } + + fnum = PQfnumber(res,"adsrc"); + o_adsrc = strdup(PQgetvalue(res,0,fnum)); + strtok(o_adsrc,"\""); + seqname = strdup(strtok(NULL,"\"")); + + free(o_adsrc); + PQclear(res); + destroyPQExpBuffer(query); + + return seqname; + } + + /* * getInherits * read all the inheritance information * from the system catalogs return them in the InhInfo* structure *************** *** 4069,4076 **** PQExpBuffer delq = createPQExpBuffer(); char *serialSeq = NULL; /* implicit sequence name created * by SERIAL datatype */ - const char *serialSeqSuffix = "_id_seq"; /* suffix for implicit - * SERIAL sequences */ char **parentRels; /* list of names of parent relations */ int numParents; int actual_atts; /* number of attrs in this CREATE statment */ --- 4125,4130 ---- *************** *** 4081,4091 **** /* First - dump SEQUENCEs */ if (tablename && strlen(tablename) > 0) { ! /* XXX this code only works for serial columns named "id" */ ! /* We really need dependency analysis! */ ! serialSeq = malloc(strlen(tablename) + strlen(serialSeqSuffix) + 1); ! strcpy(serialSeq, tablename); ! strcat(serialSeq, serialSeqSuffix); } for (i = 0; i < numTables; i++) { --- 4135,4141 ---- /* First - dump SEQUENCEs */ if (tablename && strlen(tablename) > 0) { ! serialSeq = getSerialSequenceName(tablename); } for (i = 0; i < numTables; i++) { Index: src/bin/pg_dump/pg_dump.h =================================================================== RCS file: /var/cvsup/pgsql/src/bin/pg_dump/pg_dump.h,v retrieving revision 1.76 diff -c -r1.76 pg_dump.h *** src/bin/pg_dump/pg_dump.h 5 Nov 2001 17:46:30 -0000 1.76 --- src/bin/pg_dump/pg_dump.h 16 Dec 2001 01:08:42 -0000 *************** *** 279,283 **** --- 279,284 ---- TableInfo *tbinfo, int numTables, const char *tablename); extern const char *fmtId(const char *identifier, bool force_quotes); extern void exit_nicely(void); + extern char* getSerialSequenceName(const char*); #endif /* PG_DUMP_H */