Re: [PATCHES] TODO-Item: Rename of constraints

2006-02-11 Thread Bruce Momjian

Patch applied.  Thanks.

---


Joachim Wieland wrote:
> On Wed, Dec 07, 2005 at 09:54:44PM +, Simon Riggs wrote:
> > On Mon, 2005-12-05 at 10:24 +0100, Joachim Wieland wrote:
> 
> > > o %Allow ALTER TABLE ... ALTER CONSTRAINT ... RENAME
> > > o Have ALTER INDEX update the name of a constraint using that 
> > > index
> > > o Add ALTER TABLE RENAME CONSTRAINT, update index name also
> 
> 
> > My compiler complains:
> > pg_constraint.c: In function ???RenameConstraint???:
> > pg_constraint.c:726: warning: ISO C90 forbids mixed declarations and
> > code
> 
> Sorry, that's some leftover from code rearrangements. I attach a new version
> of the patch, this time in the requested context-diff format which I forgot
> to create the other day.
> 
> 
> > This probably allows you to rename an inherited constraint to another
> > name. Not sure if that is a problem, but it probably ought to throw an
> > error, but I'm not sure who would care.
> 
> I thought about that but since the constraint gets copied anyway and you
> have two (more or less) independent constraints afterwards I didn't see a
> reason why it should be forbidden to rename it. One could throw a warning at
> least but I don't remember many cases where postgresql issues a warning
> saying: "hey, what you're doing might be bad but I'll do it anyway". If
> the consensus on this one however is to forbid it or issue a warning at
> least, it would be no problem to detect this situation once your inherited
> constraint patch is in.
> 
> 
> > I'll test some more to see if my work on inherited constraints conflicts
> > in any way.
> 
> Ok, thanks.
> 
> 
> Joachim
> 

[ Attachment, skipping... ]

> 
> ---(end of broadcast)---
> TIP 2: Don't 'kill -9' the postmaster

-- 
  Bruce Momjian|  http://candle.pha.pa.us
  pgman@candle.pha.pa.us   |  (610) 359-1001
  +  If your life is a hard drive, |  13 Roberts Road
  +  Christ can be your backup.|  Newtown Square, Pennsylvania 19073

---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


Re: [PATCHES] TODO-Item: Rename of constraints

2005-12-08 Thread Joachim Wieland
On Wed, Dec 07, 2005 at 09:54:44PM +, Simon Riggs wrote:
> On Mon, 2005-12-05 at 10:24 +0100, Joachim Wieland wrote:

> > o %Allow ALTER TABLE ... ALTER CONSTRAINT ... RENAME
> > o Have ALTER INDEX update the name of a constraint using that index
> > o Add ALTER TABLE RENAME CONSTRAINT, update index name also


> My compiler complains:
> pg_constraint.c: In function ???RenameConstraint???:
> pg_constraint.c:726: warning: ISO C90 forbids mixed declarations and
> code

Sorry, that's some leftover from code rearrangements. I attach a new version
of the patch, this time in the requested context-diff format which I forgot
to create the other day.


> This probably allows you to rename an inherited constraint to another
> name. Not sure if that is a problem, but it probably ought to throw an
> error, but I'm not sure who would care.

I thought about that but since the constraint gets copied anyway and you
have two (more or less) independent constraints afterwards I didn't see a
reason why it should be forbidden to rename it. One could throw a warning at
least but I don't remember many cases where postgresql issues a warning
saying: "hey, what you're doing might be bad but I'll do it anyway". If
the consensus on this one however is to forbid it or issue a warning at
least, it would be no problem to detect this situation once your inherited
constraint patch is in.


> I'll test some more to see if my work on inherited constraints conflicts
> in any way.

Ok, thanks.


Joachim

diff -cr cvs/pgsql/doc/src/sgml/ddl.sgml cvs.build/pgsql/doc/src/sgml/ddl.sgml
*** cvs/pgsql/doc/src/sgml/ddl.sgml 2005-11-20 13:42:45.0 +0100
--- cvs.build/pgsql/doc/src/sgml/ddl.sgml   2005-12-08 13:37:19.0 
+0100
***
*** 543,548 
--- 543,552 
  price numeric
  );
  
+Since PostgreSQL implements a UNIQUE constraint 
by
+means of an index, the above command will also create an index with the 
same
+name as the constraint. If you later on change the name of one of those, 
the
+name of the corresponding object will be changed automatically as well.
 
  
 
diff -cr cvs/pgsql/doc/src/sgml/ref/alter_index.sgml 
cvs.build/pgsql/doc/src/sgml/ref/alter_index.sgml
*** cvs/pgsql/doc/src/sgml/ref/alter_index.sgml 2005-08-28 23:04:33.0 
+0200
--- cvs.build/pgsql/doc/src/sgml/ref/alter_index.sgml   2005-12-08 
13:37:19.0 +0100
***
*** 108,113 
--- 108,122 
 
  
 
+ Indexes are also used internally by constraints, namely by UNIQUE and
+ PRIMARY KEY constraints. If you rename an index that is used internally by
+ a constraint of that type, this constraint will implicitly be renamed as
+ well. On the other hand, if you rename such a constraint, it will
+ implicitly rename its corresponding index such that both objects always
+ have the same name.
+
+ 
+
  There was formerly an ALTER INDEX OWNER variant, but
  this is now ignored (with a warning).  An index cannot have an owner
  different from its table's owner.  Changing the table's owner
diff -cr cvs/pgsql/doc/src/sgml/ref/alter_table.sgml 
cvs.build/pgsql/doc/src/sgml/ref/alter_table.sgml
*** cvs/pgsql/doc/src/sgml/ref/alter_table.sgml 2005-12-05 15:53:37.0 
+0100
--- cvs.build/pgsql/doc/src/sgml/ref/alter_table.sgml   2005-12-08 
13:37:19.0 +0100
***
*** 24,29 
--- 24,31 
  action [, ... ]
  ALTER TABLE [ ONLY ] name [ * ]
  RENAME [ COLUMN ] column TO 
new_column
+ ALTER TABLE [ ONLY ] name [ * ]
+ ALTER CONSTRAINT constraint_name RENAME TO new_constraint_name
  ALTER TABLE name
  RENAME TO new_name
  ALTER TABLE name
***
*** 170,175 
--- 172,189 
 
  
 
+ ALTER CONSTRAINT constraint_name RENAME TO new_constraint_name
+ 
+  
+   This form renames a constraint that is defined on the table. Note that 
if
+   a constraint is using an index internally (UNIQUE or
+   PRIMARY KEY constraints), the corresponding index will be
+   renamed as well.
+  
+ 
+
+ 
+
  ADD table_constraint
  
   
diff -cr cvs/pgsql/src/backend/catalog/pg_constraint.c 
cvs.build/pgsql/src/backend/catalog/pg_constraint.c
*** cvs/pgsql/src/backend/catalog/pg_constraint.c   2005-12-01 
22:38:13.0 +0100
--- cvs.build/pgsql/src/backend/catalog/pg_constraint.c 2005-12-08 
13:37:19.0 +0100
***
*** 664,666 
--- 664,854 
  
heap_close(conRel, RowExclusiveLock);
  }
+ 
+ 
+ /*
+  * RenameConstraint
+  *Rename a single constraint record
+  *conId: The OID of the constraint to rename
+  *newName: The new name of the constraint
+  *implicitRename: is this an implicit rename? If so, we will issue
+  *a notice about the implicit rename
+  *cmdName: the command that triggered the rename for the 
"

Re: [PATCHES] TODO-Item: Rename of constraints

2005-12-07 Thread Simon Riggs
On Mon, 2005-12-05 at 10:24 +0100, Joachim Wieland wrote:

> I propose the appended patch for the following TODO-items:
> 
> o %Allow ALTER TABLE ... ALTER CONSTRAINT ... RENAME
> o Have ALTER INDEX update the name of a constraint using that index
> o Add ALTER TABLE RENAME CONSTRAINT, update index name also
> 

Patch looks very interesting. I've read the additional docs you supply
and agree with the thought processes therein. Not looked at detailed
coding.

Passes make check for me. 

My compiler complains:
pg_constraint.c: In function ‘RenameConstraint’:
pg_constraint.c:726: warning: ISO C90 forbids mixed declarations and
code

This probably allows you to rename an inherited constraint to another
name. Not sure if that is a problem, but it probably ought to throw an
error, but I'm not sure who would care. I'll test some more to see if my
work on inherited constraints conflicts in any way. Patch applies (with
some fuzz) alongside my inherited constraints patch.

Best Regards, Simon Riggs


---(end of broadcast)---
TIP 3: Have you checked our extensive FAQ?

   http://www.postgresql.org/docs/faq


[PATCHES] TODO-Item: Rename of constraints

2005-12-05 Thread Joachim Wieland
Hi

I propose the appended patch for the following TODO-items:

o %Allow ALTER TABLE ... ALTER CONSTRAINT ... RENAME
o Have ALTER INDEX update the name of a constraint using that index
o Add ALTER TABLE RENAME CONSTRAINT, update index name also


Joachim

diff -ur cvs/pgsql/doc/src/sgml/ddl.sgml cvs.build/pgsql/doc/src/sgml/ddl.sgml
--- cvs/pgsql/doc/src/sgml/ddl.sgml 2005-11-20 13:42:45.0 +0100
+++ cvs.build/pgsql/doc/src/sgml/ddl.sgml   2005-12-05 08:38:08.0 
+0100
@@ -543,6 +543,10 @@
 price numeric
 );
 
+   Since PostgreSQL implements a UNIQUE constraint 
by
+   means of an index, the above command will also create an index with the same
+   name as the constraint. If you later on change the name of one of those, the
+   name of the corresponding object will be changed automatically as well.

 

diff -ur cvs/pgsql/doc/src/sgml/ref/alter_index.sgml 
cvs.build/pgsql/doc/src/sgml/ref/alter_index.sgml
--- cvs/pgsql/doc/src/sgml/ref/alter_index.sgml 2005-08-28 23:04:33.0 
+0200
+++ cvs.build/pgsql/doc/src/sgml/ref/alter_index.sgml   2005-12-05 
08:47:09.0 +0100
@@ -108,6 +108,15 @@

 

+Indexes are also used internally by constraints, namely by UNIQUE and
+PRIMARY KEY constraints. If you rename an index that is used internally by
+a constraint of that type, this constraint will implicitly be renamed as
+well. On the other hand, if you rename such a constraint, it will
+implicitly rename its corresponding index such that both objects always
+have the same name.
+   
+
+   
 There was formerly an ALTER INDEX OWNER variant, but
 this is now ignored (with a warning).  An index cannot have an owner
 different from its table's owner.  Changing the table's owner
diff -ur cvs/pgsql/doc/src/sgml/ref/alter_table.sgml 
cvs.build/pgsql/doc/src/sgml/ref/alter_table.sgml
--- cvs/pgsql/doc/src/sgml/ref/alter_table.sgml 2005-08-28 23:04:33.0 
+0200
+++ cvs.build/pgsql/doc/src/sgml/ref/alter_table.sgml   2005-12-05 
08:38:08.0 +0100
@@ -24,6 +24,8 @@
 action [, ... ]
 ALTER TABLE [ ONLY ] name [ * ]
 RENAME [ COLUMN ] column TO 
new_column
+ALTER TABLE [ ONLY ] name [ * ]
+ALTER CONSTRAINT constraint_name RENAME TO new_constraint_name
 ALTER TABLE name
 RENAME TO new_name
 ALTER TABLE name
@@ -170,6 +172,18 @@

 

+ALTER CONSTRAINT constraint_name RENAME TO new_constraint_name
+
+ 
+  This form renames a constraint that is defined on the table. Note that if
+  a constraint is using an index internally (UNIQUE or
+  PRIMARY KEY constraints), the corresponding index will be
+  renamed as well.
+ 
+
+   
+
+   
 ADD table_constraint
 
  
diff -ur cvs/pgsql/src/backend/catalog/pg_constraint.c 
cvs.build/pgsql/src/backend/catalog/pg_constraint.c
--- cvs/pgsql/src/backend/catalog/pg_constraint.c   2005-12-01 
22:38:13.0 +0100
+++ cvs.build/pgsql/src/backend/catalog/pg_constraint.c 2005-12-05 
08:56:44.0 +0100
@@ -664,3 +664,190 @@
 
heap_close(conRel, RowExclusiveLock);
 }
+
+
+/*
+ * RenameConstraint
+ * Rename a single constraint record
+ * conId: The OID of the constraint to rename
+ * newName: The new name of the constraint
+ * implicitRename: is this an implicit rename? If so, we will issue
+ * a notice about the implicit rename
+ * cmdName: the command that triggered the rename for the 
"implicitly
+ *  renames" notice message
+ */
+void
+RenameConstraint(Oid conId, const char* newName,
+bool implicitRename, const char* cmdName)
+{
+   RelationconRel;
+   ScanKeyData key[1];
+   SysScanDesc scan;
+   HeapTuple   tup;
+   NameDatanewNameData;
+   Relationrel;
+   Oid relId;
+   Oid nspOid;
+
+   /* before reading the tuple, lock the table it constraints in
+* AccessExclusiveLock mode. Otherwise, if we read it before locking 
this
+* table, the tuple might be changed by another transaction and our copy
+* would be out of date
+*/
+   relId = GetConstraintRelationId(conId);
+   if (!OidIsValid(relId))
+   {
+   ereport(ERROR,
+   (errcode(ERRCODE_UNDEFINED_OBJECT),
+errmsg("constraint with OID %d does not 
exist", conId)));
+   }
+
+   rel = relation_open(relId, AccessExclusiveLock);
+   nspOid = get_rel_namespace(relId);
+
+   conRel = heap_open(ConstraintRelationId, RowExclusiveLock);
+
+   ScanKeyInit(&key[0],
+   ObjectIdAttributeNumber,
+   BTEqualStrategyNumber, F_OIDEQ,
+   Obj