Skip to content
This repository has been archived by the owner on Sep 15, 2022. It is now read-only.

Editing table properties drops all foreign keys and recreates them (RM #622) #393

Open
dpage opened this issue Sep 23, 2012 · 5 comments
Open
Labels

Comments

@dpage
Copy link
Contributor

dpage commented Sep 23, 2012

Issue migrated from Redmine: https://redmine.postgresql.org/issues/622
Originally created by Anonymous at 2012-09-23 13:50:18 UTC.

When I open the properties on a table containing foreign key constraints, and then to go the SQL tab to see what will be executed, I notice all foreign keys are dropped and then recreated. For example when I open the table properties for the table zulook.inv_equipment on my database, the SQL pane reads as follow:

ALTER TABLE zulook.inv_equipment
DROP CONSTRAINT inv_equipment_company_entity_id_fkey;
ALTER TABLE zulook.inv_equipment
DROP CONSTRAINT inv_equipment_item_id_fkey;
ALTER TABLE zulook.inv_equipment
ADD CONSTRAINT inv_equipment_company_entity_id_fkey FOREIGN KEY
(company_entity_id)
REFERENCES zulook.ab_company (company_entity_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY
IMMEDIATE;
ALTER TABLE zulook.inv_equipment
ADD CONSTRAINT inv_equipment_item_id_fkey FOREIGN KEY (item_id)
REFERENCES zulook.inv_item (item_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY
IMMEDIATE;

I didn't have this behaviour problem under 1.12.x versions, I've noticed it starting with 1.14.x versions, and now it is still present in 1.16.0. I use pgAdmin 1.16.0 (Sep 7 2012, rev: REL-1_16_0, from binary) under Windows XP SP3, English version but on a Canadian French OS.

@dpage
Copy link
Contributor Author

dpage commented Dec 13, 2012

Attachment migrated from Redmine: https://redmine.postgresql.org/attachments/download/427
Originally created by Anonymous at 2012-12-13 13:37:34 UTC.

https://pgadmin-archive.postgresql.org/redmine/622/427-pgadmin.diff

Description: proposed patch

@dpage
Copy link
Contributor Author

dpage commented Dec 13, 2012

Comment migrated from Redmine: https://redmine.postgresql.org/issues/622#note-2
Originally created by Anonymous at 2012-12-13 13:42:57 UTC.

First of all, I disagree with categorizing this bug as "minor". Recreating the foreign key constraint will hold an exclusive lock on both the local and the foreign table while the foreign key constraint is verified (see http://archives.postgresql.org/pgsql-hackers/2012-11/msg00497.php). Depending on how big the tables are, the locks can be held for a long time.

At one client, we had a production database grind to a halt because the DBA wanted to add a GRANT to a table and did not inspect the SQL tab closely, resulting in the above scenario.

Thankfully, the client allowed us to look at the problem on their time. It appears this bug is a regression introduced with http://git.postgresql.org/gitweb/?p=pgadmin3.git;a=patch;h=9280f9654714872816ac3b1a40455536c754ea4d;hp=b80b851a35fdd090b0d5fe9ed43d5563a4f8bd3d

I debugged PGAdmin3 (version 1.14.2) and it appears the problem is as follows: when comparing the constraints around line 820 of pgadmin/dlg/dlgTable.cpp, the content of the wxStringArray constraintsDefinition contains additional whitespace and newlines compared to the content of previousConstraints (and thus tmpDef, in which the definition is searched for), resulting in no match (index = -1) and the subsequent deletion of the constraint.

It appears the culprit is when adding the existing constraints to constraintsDefinition and lstConstraints in dlgTable::Go. For the PGM_FOREIGNKEY case around line 355 in pgadmin/dlg/dlgTable.cpp, whitespace and newlines get stripped off for lstConstraints but not for constraintsDefinition, resulting in different strings. It is unclear why the whitespace is removed for PGM_FOREIGNKEY but not for any of the other constraints types, and git annotate does not help either as this code got last touched during a reindent run.

I propose to remove that stripping of whitespace from lstConstraints, this fixes the problem for me. However, I do not have a Windows development environment in order to check whether the original bug as reported by Vjacheslav Vjacheslav in http://archives.postgresql.org/pgadmin-support/2011-12/msg00010.php is still fixed, but I suppose so.

Best regards,

Michael

@dpage
Copy link
Contributor Author

dpage commented Jan 3, 2013

Comment migrated from Redmine: https://redmine.postgresql.org/issues/622#note-3
Originally created by Anonymous at 2013-01-03 12:28:01 UTC.

Hi,

did somebody take a look at this? I'd like to push this fix for Debian if it gets approved, as it appears to be pretty serious to me.

@dpage
Copy link
Contributor Author

dpage commented Jan 4, 2013

Comment migrated from Redmine: https://redmine.postgresql.org/issues/622#note-4
Originally created by Guillaume Lelarge at 2013-01-04 10:33:12 UTC.

I took a quick look at it and while it seems to fix the issue, it still makes me uncomfortable. I'll try to find some time next week to work on this.

@dpage
Copy link
Contributor Author

dpage commented Jan 22, 2013

Comment migrated from Redmine: https://redmine.postgresql.org/issues/622#note-5
Originally created by Anonymous at 2013-01-22 16:45:26 UTC.

Any news on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant