On Wed, Jan 14, 2015 at 07:29:24PM -0500, Tom Lane wrote:
> dst1 doesn't get an OID column:
> 
> regression=# create table src1 (f1 int) with oids;
> CREATE TABLE
> regression=# create table dst1 (like src1);
> CREATE TABLE
> regression=# \d+ src1
>                          Table "public.src1"
>  Column |  Type   | Modifiers | Storage | Stats target | Description 
> --------+---------+-----------+---------+--------------+-------------
>  f1     | integer |           | plain   |              | 
> Has OIDs: yes
> 
> regression=# \d+ dst1
>                          Table "public.dst1"
>  Column |  Type   | Modifiers | Storage | Stats target | Description 
> --------+---------+-----------+---------+--------------+-------------
>  f1     | integer |           | plain   |              | 
> 
> 
> If you don't find that problematic, how about this case?
> 
> regression=# create table src2 (f1 int, primary key(oid)) with oids;
> CREATE TABLE
> regression=# create table dst2 (like src2 including indexes);
> ERROR:  column "oid" named in key does not exist

I have developed the attached patch to fix this.  The code was basically
confused because setting cxt.hasoids had no effect, and the LIKE
relation was never checked.  

The fix is to default cxt.hasoids to false, set it to true if the LIKE
relation has oids, and add WITH OIDS to the CREATE TABLE statement, if
necessary.  It also honors WITH/WITHOUT OIDS specified literally in the
CREATE TABLE clause because the first specification is honored, and we
only append WITH OIDS if the LIKE table has oids.

Should this be listed in the release notes as a backward-incompatibility?

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +
diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c
new file mode 100644
index 1fc8c2c..405c347
*** a/src/backend/parser/parse_utilcmd.c
--- b/src/backend/parser/parse_utilcmd.c
*************** transformCreateStmt(CreateStmt *stmt, co
*** 222,228 ****
  	cxt.blist = NIL;
  	cxt.alist = NIL;
  	cxt.pkey = NULL;
! 	cxt.hasoids = interpretOidsOption(stmt->options, true);
  
  	Assert(!stmt->ofTypename || !stmt->inhRelations);	/* grammar enforces */
  
--- 222,228 ----
  	cxt.blist = NIL;
  	cxt.alist = NIL;
  	cxt.pkey = NULL;
! 	cxt.hasoids = false;
  
  	Assert(!stmt->ofTypename || !stmt->inhRelations);	/* grammar enforces */
  
*************** transformCreateStmt(CreateStmt *stmt, co
*** 281,286 ****
--- 281,290 ----
  	 * Output results.
  	 */
  	stmt->tableElts = cxt.columns;
+ 	/* add WITH OIDS, if necessary */
+ 	if (!interpretOidsOption(stmt->options, true) && cxt.hasoids)
+ 		stmt->options = lappend(stmt->options, makeDefElem("oids",
+ 								(Node *)makeInteger(TRUE)));
  	stmt->constraints = cxt.ckconstraints;
  
  	result = lappend(cxt.blist, stmt);
*************** transformTableLikeClause(CreateStmtConte
*** 849,854 ****
--- 853,860 ----
  		}
  	}
  
+ 	cxt->hasoids = relation->rd_rel->relhasoids;
+ 
  	/*
  	 * Copy CHECK constraints if requested, being careful to adjust attribute
  	 * numbers so they match the child.
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to