Re: [HACKERS] fix pg_dump to dump sequences created by SERIAL datatype

Bruce Momjian <maillist@candle.pha.pa.us>

From: Bruce Momjian <maillist@candle.pha.pa.us>
To: brook@trillium.NMSU.Edu (Brook Milligan)
Cc: hackers@postgreSQL.org, patches@postgreSQL.org
Date: 1999-01-18T04:59:55Z
Lists: pgsql-hackers
Applied.

> As it stands in 6.4.2 pg_dump does not dump sequences created by the
> SERIAL datatype when the -t tablename option is used.  The following
> patch fixes that (with a couple of cosmetic cleanups) so that whenever
> the -t option is used, the appropriate *_id_seq sequence is also
> dumped if it exists.
> 
> The second patch just fixes a bunch of cosmetic details concerning
> alignment and changes nothing substantive in the code.
> 
> Cheers,
> Brook
> 
> ===========================================================================
> 
> --- bin/pg_dump/pg_dump.c.orig	Wed Jan 13 09:37:40 1999
> +++ bin/pg_dump/pg_dump.c	Wed Jan 13 10:08:35 1999
> @@ -2593,17 +2592,26 @@
>  	int			i,
>  				j,
>  				k;
> -	char		q[MAXQUERYLEN];
> -	char	  **parentRels;		/* list of names of parent relations */
> +	char			q[MAXQUERYLEN];
> +	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 */
> +	int			actual_atts;			/* number of attrs in this CREATE statment */
>  
>  	/* First - dump SEQUENCEs */
> +	if (tablename)
> +	  {
> +	    serialSeq = malloc (strlen (tablename) + strlen (serialSeqSuffix) + 1);
> +	    strcpy (serialSeq, tablename);
> +	    strcat (serialSeq, serialSeqSuffix);
> +	  }
>  	for (i = 0; i < numTables; i++)
>  	{
>  		if (!(tblinfo[i].sequence))
>  			continue;
> -		if (!tablename || (!strcmp(tblinfo[i].relname, tablename)))
> +		if (!tablename || (!strcmp(tblinfo[i].relname, tablename))
> +		    || (serialSeq && !strcmp(tblinfo[i].relname,serialSeq)))
>  		{
>  			becomeUser(fout, tblinfo[i].usename);
>  			dumpSequence(fout, tblinfo[i]);
> @@ -2611,6 +2619,8 @@
>  				dumpACL(fout, tblinfo[i]);
>  		}
>  	}
> +	if (tablename)
> +	  free (serialSeq);
>  
>  	for (i = 0; i < numTables; i++)
>  	{
> 
> ===========================================================================
> 
> --- bin/pg_dump/pg_dump.c.orig	Wed Jan 13 09:37:40 1999
> +++ bin/pg_dump/pg_dump.c	Wed Jan 13 10:08:35 1999
> @@ -56,7 +56,7 @@
>  #include <stdio.h>
>  #include <string.h>
>  #include <ctype.h>
> -#include <sys/param.h>			/* for MAXHOSTNAMELEN on most */
> +#include <sys/param.h>				/* for MAXHOSTNAMELEN on most */
>  #ifdef solaris_sparc
>  #include <netdb.h>				/* for MAXHOSTNAMELEN on some */
>  #endif
> @@ -96,25 +96,25 @@
>  static char *GetPrivileges(const char *s);
>  static void becomeUser(FILE *fout, const char *username);
>  
> -extern char *optarg;
> +extern char	*optarg;
>  extern int	optind,
> -			opterr;
> +		opterr;
>  
>  /* global decls */
>  bool		g_verbose;			/* User wants verbose narration of our
> -								 * activities. */
> -int			g_last_builtin_oid; /* value of the last builtin oid */
> -FILE	   *g_fout;				/* the script file */
> -PGconn	   *g_conn;				/* the database connection */
> -
> -bool		force_quotes;		/* User wants to suppress double-quotes */
> -int			dumpData;			/* dump data using proper insert strings */
> -int			attrNames;			/* put attr names into insert strings */
> -int			schemaOnly;
> -int			dataOnly;
> -int			aclsOption;
> +						 * activities. */
> +int		g_last_builtin_oid;		/* value of the last builtin oid */
> +FILE		*g_fout;			/* the script file */
> +PGconn		*g_conn;			/* the database connection */
> +
> +bool		force_quotes;			/* User wants to suppress double-quotes */
> +int		dumpData;			/* dump data using proper insert strings */
> +int		attrNames;			/* put attr names into insert strings */
> +int		schemaOnly;
> +int		dataOnly;
> +int		aclsOption;
>  
> -char		g_opaque_type[10];	/* name for the opaque type */
> +char		g_opaque_type[10];		/* name for the opaque type */
>  
>  /* placeholders for the delimiters for comments */
>  char		g_comment_start[10];
> @@ -179,8 +179,8 @@
>  static bool
>  isViewRule(char *relname)
>  {
> -	PGresult   *res;
> -	int			ntups;
> +	PGresult	*res;
> +	int		ntups;
>  	char		query[MAXQUERYLEN];
>  
>  	res = PQexec(g_conn, "begin");
> @@ -315,13 +315,13 @@
>  					 const TableInfo tblinfo, bool oids)
>  {
>  
> -	PGresult   *res;
> +	PGresult	*res;
>  	char		query[255];
> -	int			actual_atts;	/* number of attrs in this a table */
> +	int		actual_atts;	/* number of attrs in this a table */
>  	char		expandbuf[COPYBUFSIZ];
>  	char		q[MAXQUERYLEN];
> -	int			tuple;
> -	int			field;
> +	int		tuple;
> +	int		field;
>  
>  	sprintf(query, "SELECT * FROM %s", fmtId(classname, force_quotes));
>  	res = PQexec(g_conn, query);
> @@ -421,7 +421,7 @@
>  {
>  
>  	int			i;
> -	char	   *all_only;
> +	char			*all_only;
>  
>  	if (onlytable == NULL)
>  		all_only = "all";
> @@ -482,12 +482,11 @@
>  prompt_for_password(char *username, char *password)
>  {
>  	char		buf[512];
> -	int			length;
> +	int		length;
>  
>  #ifdef HAVE_TERMIOS_H
> -	struct termios t_orig,
> -				t;
> -
> +	struct termios	t_orig,
> +			t;
>  #endif
>  
>  	printf("Username: ");
> @@ -535,21 +534,21 @@
>  int
>  main(int argc, char **argv)
>  {
> -	int			c;
> -	const char *progname;
> -	const char *filename = NULL;
> -	const char *dbname = NULL;
> -	const char *pghost = NULL;
> -	const char *pgport = NULL;
> -	char	   *tablename = NULL;
> -	int			oids = 0;
> -	TableInfo  *tblinfo;
> -	int			numTables;
> +	int		c;
> +	const char	*progname;
> +	const char	*filename = NULL;
> +	const char	*dbname = NULL;
> +	const char	*pghost = NULL;
> +	const char	*pgport = NULL;
> +	char		*tablename = NULL;
> +	int		oids = 0;
> +	TableInfo	*tblinfo;
> +	int		numTables;
>  	char		connect_string[512] = "";
>  	char		tmp_string[128];
>  	char		username[100];
>  	char		password[100];
> -	int			use_password = 0;
> +	int		use_password = 0;
>  
>  	g_verbose = false;
>  	force_quotes = true;
> @@ -727,11 +726,11 @@
>  TypeInfo   *
>  getTypes(int *numTypes)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	TypeInfo   *tinfo;
> +	char			query[MAXQUERYLEN];
> +	TypeInfo		*tinfo;
>  
>  	int			i_oid;
>  	int			i_typowner;
> @@ -856,12 +855,12 @@
>  OprInfo    *
>  getOperators(int *numOprs)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> +	char			query[MAXQUERYLEN];
>  
> -	OprInfo    *oprinfo;
> +	OprInfo			*oprinfo;
>  
>  	int			i_oid;
>  	int			i_oprname;
> @@ -1199,11 +1198,11 @@
>  AggInfo    *
>  getAggregates(int *numAggs)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	AggInfo    *agginfo;
> +	char			query[MAXQUERYLEN];
> +	AggInfo			*agginfo;
>  
>  	int			i_oid;
>  	int			i_aggname;
> @@ -1293,11 +1292,11 @@
>  FuncInfo   *
>  getFuncs(int *numFuncs)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	FuncInfo   *finfo;
> +	char			query[MAXQUERYLEN];
> +	FuncInfo		*finfo;
>  
>  	int			i_oid;
>  	int			i_proname;
> @@ -1393,11 +1392,11 @@
>  TableInfo  *
>  getTables(int *numTables, FuncInfo *finfo, int numFuncs)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	TableInfo  *tblinfo;
> +	char			query[MAXQUERYLEN];
> +	TableInfo		*tblinfo;
>  
>  	int			i_oid;
>  	int			i_relname;
> @@ -1726,11 +1725,11 @@
>  InhInfo    *
>  getInherits(int *numInherits)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	InhInfo    *inhinfo;
> +	char			query[MAXQUERYLEN];
> +	InhInfo			*inhinfo;
>  
>  	int			i_inhrel;
>  	int			i_inhparent;
> @@ -1791,13 +1790,13 @@
>  {
>  	int			i,
>  				j;
> -	char		q[MAXQUERYLEN];
> +	char			q[MAXQUERYLEN];
>  	int			i_attname;
>  	int			i_typname;
>  	int			i_atttypmod;
>  	int			i_attnotnull;
>  	int			i_atthasdef;
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  
>  	for (i = 0; i < numTables; i++)
> @@ -1902,10 +1901,10 @@
>  getIndices(int *numIndices)
>  {
>  	int			i;
> -	char		query[MAXQUERYLEN];
> -	PGresult   *res;
> +	char			query[MAXQUERYLEN];
> +	PGresult		*res;
>  	int			ntups;
> -	IndInfo    *indinfo;
> +	IndInfo			*indinfo;
>  
>  	int			i_indexrelname;
>  	int			i_indrelname;
> @@ -1994,7 +1993,7 @@
>  		  TypeInfo *tinfo, int numTypes)
>  {
>  	int			i;
> -	char		q[MAXQUERYLEN];
> +	char			q[MAXQUERYLEN];
>  	int			funcInd;
>  
>  	for (i = 0; i < numTypes; i++)
> @@ -2167,7 +2166,7 @@
>  			TypeInfo *tinfo, int numTypes)
>  {
>  	char		q[MAXQUERYLEN];
> -	int			j;
> +	int		j;
>  	char		*func_def;
>  	char		func_lang[NAMEDATALEN + 1];
>  
> @@ -2268,7 +2267,7 @@
>  dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
>  		 TypeInfo *tinfo, int numTypes)
>  {
> -	int			i;
> +	int		i;
>  	char		q[MAXQUERYLEN];
>  	char		leftarg[MAXQUERYLEN];
>  	char		rightarg[MAXQUERYLEN];
> @@ -2375,14 +2374,14 @@
>  dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
>  		 TypeInfo *tinfo, int numTypes)
>  {
> -	int			i;
> +	int		i;
>  	char		q[MAXQUERYLEN];
>  	char		sfunc1[MAXQUERYLEN];
>  	char		sfunc2[MAXQUERYLEN];
>  	char		basetype[MAXQUERYLEN];
>  	char		finalfunc[MAXQUERYLEN];
>  	char		comma1[2],
> -				comma2[2];
> +			comma2[2];
>  
>  	for (i = 0; i < numAggs; i++)
>  	{
> @@ -2514,9 +2513,9 @@
>  {
>  	const char *acls = tbinfo.relacl;
>  	char	   *aclbuf,
> -			   *tok,
> -			   *eqpos,
> -			   *priv;
> +		   *tok,
> +		   *eqpos,
> +		   *priv;
>  
>  	if (strlen(acls) == 0)
>  		return;					/* table has default permissions */
> @@ -2726,15 +2736,15 @@
>  	int			i,
>  				k;
>  	int			tableInd;
> -	char		attlist[1000];
> -	char	   *classname[INDEX_MAX_KEYS];
> -	char	   *funcname;		/* the name of the function to comput the
> -								 * index key from */
> +	char			attlist[1000];
> +	char			*classname[INDEX_MAX_KEYS];
> +	char			*funcname;		/* the name of the function to comput the
> +							 * index key from */
>  	int			indkey,
>  				indclass;
>  	int			nclass;
>  
> -	char		q[MAXQUERYLEN],
> +	char			q[MAXQUERYLEN],
>  				id1[MAXQUERYLEN],
>  				id2[MAXQUERYLEN];
>  	PGresult   *res;
> @@ -2887,7 +2897,7 @@
>  				k;
>  	int			m,
>  				n;
> -	char	  **outVals = NULL; /* values to copy out */
> +	char			**outVals = NULL; /* values to copy out */
>  
>  	n = PQntuples(res);
>  	m = PQnfields(res);
> @@ -2940,7 +2950,7 @@
>  static void
>  setMaxOid(FILE *fout)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	Oid			max_oid;
>  
>  	res = PQexec(g_conn, "CREATE TABLE pgdump_oid (dummy int4)");
> @@ -2993,7 +3003,7 @@
>  static int
>  findLastBuiltinOid(void)
>  {
> -	PGresult   *res;
> +	PGresult		*res;
>  	int			ntups;
>  	int			last_oid;
>  
> @@ -3025,9 +3035,9 @@
>  static char *
>  checkForQuote(const char *s)
>  {
> -	char	   *r;
> +	char		*r;
>  	char		c;
> -	char	   *result;
> +	char		*result;
>  
>  	int			j = 0;
>  
> @@ -3056,16 +3066,16 @@
>  static void
>  dumpSequence(FILE *fout, TableInfo tbinfo)
>  {
> -	PGresult   *res;
> -	int4		last,
> +	PGresult		*res;
> +	int4			last,
>  				incby,
>  				maxv,
>  				minv,
>  				cache;
> -	char		cycled,
> +	char			cycled,
>  				called,
> -			   *t;
> -	char		query[MAXQUERYLEN];
> +				*t;
> +	char			query[MAXQUERYLEN];
>  
>  	sprintf(query,
>  			"SELECT sequence_name, last_value, increment_by, max_value, "
> 
> 


-- 
  Bruce Momjian                        |  http://www.op.net/~candle
  maillist@candle.pha.pa.us            |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026