I took my first stab at hacking the sources to fix the bug reported here:

http://archives.postgresql.org/pgsql-bugs/2012-01/msg00152.php

It's such a simple patch but it took me several hours with Google and IRC
and I'm sure I did many things wrong.  It seems to work as advertised,
though, so I'm submitting it.

I suppose since I have now submitted a patch, it is my moral obligation to
review at least one.  I'm not sure how helpful I'll be, but I'll go bite
the bullet and sign myself up anyway.
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
new file mode 100644
index cb8ac67..7555d19
*** a/src/backend/commands/tablecmds.c
--- b/src/backend/commands/tablecmds.c
*************** ATExecAddColumn(List **wqueue, AlteredTa
*** 4235,4240 ****
--- 4235,4241 ----
  	List	   *children;
  	ListCell   *child;
  	AclResult	aclresult;
+ 	HeapTuple	attTuple;
  
  	/* At top level, permission check was done in ATPrepCmd, else do it */
  	if (recursing)
*************** ATExecAddColumn(List **wqueue, AlteredTa
*** 4314,4326 ****
  	 * this test is deliberately not attisdropped-aware, since if one tries to
  	 * add a column matching a dropped column name, it's gonna fail anyway.
  	 */
! 	if (SearchSysCacheExists2(ATTNAME,
! 							  ObjectIdGetDatum(myrelid),
! 							  PointerGetDatum(colDef->colname)))
! 		ereport(ERROR,
! 				(errcode(ERRCODE_DUPLICATE_COLUMN),
! 				 errmsg("column \"%s\" of relation \"%s\" already exists",
! 						colDef->colname, RelationGetRelationName(rel))));
  
  	/* Determine the new attribute's number */
  	if (isOid)
--- 4315,4341 ----
  	 * this test is deliberately not attisdropped-aware, since if one tries to
  	 * add a column matching a dropped column name, it's gonna fail anyway.
  	 */
! 	attTuple = SearchSysCache2(ATTNAME,
! 							   ObjectIdGetDatum(myrelid),
! 							   PointerGetDatum(colDef->colname));
! 
! 	if (HeapTupleIsValid(attTuple))
! 	{
!     	int attnum;
!     	attnum = ((Form_pg_attribute) GETSTRUCT(attTuple))->attnum;
!     	if (attnum <= 0)
! 		    ereport(ERROR,
! 				    (errcode(ERRCODE_DUPLICATE_COLUMN),
! 				     errmsg("column \"%s\" conflicts with a system column name",
! 						    colDef->colname)));
!     	else
! 		    ereport(ERROR,
! 				    (errcode(ERRCODE_DUPLICATE_COLUMN),
! 				     errmsg("column \"%s\" of relation \"%s\" already exists",
! 						    colDef->colname, RelationGetRelationName(rel))));
! 
! 		ReleaseSysCache(attTuple);
!     }
  
  	/* Determine the new attribute's number */
  	if (isOid)
diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out
new file mode 100644
index e992549..8385afb
*** a/src/test/regress/expected/alter_table.out
--- b/src/test/regress/expected/alter_table.out
*************** COMMENT ON TABLE tmp_wrong IS 'table com
*** 7,12 ****
--- 7,14 ----
  ERROR:  relation "tmp_wrong" does not exist
  COMMENT ON TABLE tmp IS 'table comment';
  COMMENT ON TABLE tmp IS NULL;
+ ALTER TABLE tmp ADD COLUMN xmin integer; -- fails
+ ERROR:  column "xmin" conflicts with a system column name
  ALTER TABLE tmp ADD COLUMN a int4 default 3;
  ALTER TABLE tmp ADD COLUMN b name;
  ALTER TABLE tmp ADD COLUMN c text;
diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql
new file mode 100644
index d9bf08d..d4e4c49
*** a/src/test/regress/sql/alter_table.sql
--- b/src/test/regress/sql/alter_table.sql
*************** COMMENT ON TABLE tmp_wrong IS 'table com
*** 9,14 ****
--- 9,16 ----
  COMMENT ON TABLE tmp IS 'table comment';
  COMMENT ON TABLE tmp IS NULL;
  
+ ALTER TABLE tmp ADD COLUMN xmin integer; -- fails
+ 
  ALTER TABLE tmp ADD COLUMN a int4 default 3;
  
  ALTER TABLE tmp ADD COLUMN b name;
-- 
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