Skip site navigation (1) Skip section navigation (2)

Re: pg_dump.save_pkey_name.patch

From: Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>
To: awn(at)bcs(dot)zp(dot)ua
Cc: pgsql-patches(at)postgresql(dot)org
Subject: Re: pg_dump.save_pkey_name.patch
Date: 2000-11-27 20:51:32
Message-ID: 200011272051.PAA21135@candle.pha.pa.us (view raw or flat)
Thread:
Lists: pgsql-patches
Thanks.  Applied.


> This patch allow pg_dump save name of primary key constraint (if primary
> key exist).
> 
> Patch was created around PostgreSQL v7.0.3 sources.
> 
> --- src/bin/pg_dump/pg_dump.c.O	Sun Nov  5 06:03:44 2000
> +++ src/bin/pg_dump/pg_dump.c	Mon Nov 27 03:23:17 2000
> @@ -1216,6 +1216,8 @@ clearTableInfo(TableInfo *tblinfo, int n
>  			free(tblinfo[i].notnull);
>  		if (tblinfo[i].primary_key)
>  			free(tblinfo[i].primary_key);
> +		if (tblinfo[i].primary_key_name)
> +			free(tblinfo[i].primary_key_name);
>  	}
>  	free(tblinfo);
>  }
> @@ -1760,6 +1762,49 @@ getTables(int *numTables, FuncInfo *finf
>  		else
>  			tblinfo[i].primary_key = NULL;
>  
> +		/* Get primary key name (if primary key exist) */
> +		if (tblinfo[i].primary_key)
> +		{
> +			PGresult   *res2;
> +			int		   n;
> +
> +			resetPQExpBuffer(query);
> +			appendPQExpBuffer(query,
> +							  "SELECT c.relname "
> +							  "FROM pg_index i, pg_class c "
> +							  "WHERE i.indrelid = %s"
> +							  "AND   i.indisprimary "
> +							  "AND   c.oid = i.indexrelid ",
> +							  tblinfo[i].oid);
> +			res2 = PQexec(g_conn, query->data);
> +			if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK)
> +			{
> +				fprintf(stderr, "getTables(): SELECT (for PRIMARY KEY NAME) failed.  Explanation from backend: %s",
> +						PQerrorMessage(g_conn));
> +				exit_nicely(g_conn);
> +			}
> +
> +			n = PQntuples(res2);
> +			if (n != 1)
> +			{
> +				fprintf(stderr,
> +						"getTables(): SELECT (for PRIMARY KEY NAME) failed. This is impossible but object with OID == %s have %d primary keys.\n",
> +						tblinfo[i].oid,
> +						n);
> +				exit_nicely(g_conn);
> +			}
> +
> +			tblinfo[i].primary_key_name =
> +				strdup(fmtId(PQgetvalue(res2, 0, 0), force_quotes));
> +			if (tblinfo[i].primary_key_name == NULL)
> +			{
> +				perror("strdup");
> +				exit(1);
> +			}
> +		}
> +		else
> +			tblinfo[i].primary_key_name = NULL;
> +
>  		/* Get Triggers */
>  		if (tblinfo[i].ntrig > 0)
>  		{
> @@ -3206,7 +3251,10 @@ dumpTables(FILE *fout, TableInfo *tblinf
>  			{
>  				if (actual_atts + tblinfo[i].ncheck > 0)
>  					appendPQExpBuffer(q, ",\n\t");
> -				appendPQExpBuffer(q, "PRIMARY KEY (%s)", tblinfo[i].primary_key);
> +				appendPQExpBuffer(q,
> +								  "CONSTRAINT %s PRIMARY KEY (%s)",
> +								  tblinfo[i].primary_key_name,
> +								  tblinfo[i].primary_key);
>  			}
>  
>  			appendPQExpBuffer(q, "\n)");
> --- src/bin/pg_dump/pg_dump.h.O	Sun Nov  5 06:03:44 2000
> +++ src/bin/pg_dump/pg_dump.h	Mon Nov 27 03:23:16 2000
> @@ -99,6 +99,7 @@ typedef struct _tableInfo
>  	char	  **trcomments;		/* COMMENT ON TRIGGER ... */
>  	char	  **troids;			/* TRIGGER oids */
>  	char	   *primary_key;	/* PRIMARY KEY of the table, if any */
> +	char	   *primary_key_name;	/* PRIMARY KEY name, if any */
>  } TableInfo;
>  
>  typedef struct _inhInfo
> 
> -- 
> Andrew W. Nosenko    (awn(at)bcs(dot)zp(dot)ua)
> 


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

In response to

pgsql-patches by date

Next:From: Peter BiermanDate: 2000-11-29 07:59:40
Subject: darwin pgsql patches
Previous:From: awnDate: 2000-11-27 01:46:21
Subject: pg_dump.save_pkey_name.patch

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group