pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christian Barthel <bch@online.de>
To: pgsql-general@lists.postgresql.org
Subject: FK Constraint sort order with pg_dump
Date: Thu, 21 Jul 2022 19:25:40 +0200
Message-ID: <87h73a4bu3.fsf@online.de> (raw)

Hello,

The sorting order of FK constraints with the same name is based on the
OID (because it lands in the “Usually shouldn’t get here” OID comparison
block at [1]).  Wouldn’t it be better if the order of those constraints
were based on the table name?

Details:

The above schema is identical except in the order how the constraints
were added (the constraint name is the same on those two tables):

--8<---------------cut here---------------start------------->8---
  -- --- Schema Version 1:
  CREATE TABLE a (id int unique);

  CREATE TABLE b (id int);
  ALTER TABLE b ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES a(id);
  CREATE TABLE c (id int);
  ALTER TABLE c ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES a(id);

  -- --- Schema Version 2:

  CREATE TABLE a (id int unique);

  CREATE TABLE c (id int);
  ALTER TABLE c ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES a(id);
  CREATE TABLE b (id int);
  ALTER TABLE b ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES a(id);
--8<---------------cut here---------------end--------------->8---

Doing a pg_dump on Version 1 and Version 2 leads to two different dumps
despite being the same schema: (*)

--8<---------------cut here---------------start------------->8---
inline patch 1: version2 (+4/-5 lines) download inline-1.patch--- version1 2022-07-21 19:16:31.369010843 +0200 +++ version2 2022-07-21 19:16:26.688976178 +0200 @@ -86,18 +86,18 @@ -- --- Name: b x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch +-- Name: c x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch -- -ALTER TABLE ONLY public.b +ALTER TABLE ONLY public.c ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES public.a(id); -- --- Name: c x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch +-- Name: b x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch -- -ALTER TABLE ONLY public.c +ALTER TABLE ONLY public.b ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES public.a(id); --8<---------------cut here---------------end--------------->8---
Attached is a patch file that adds a string comparison function call to sort FK constraints (based on the table if it exists). Any thoughts on that? [1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/bin/pg_dump/pg_dump_sort.c;h=80641c... (*) Tested on 14.4 -- Christian Barthel Patches and attachments: download all (2 files, .tar.gz)
fk-sorting.patch (text/x-diff, 787B) download modified src/bin/pg_dump/pg_dump_sort.c @@ -300,6 +300,23 @@ DOTypeNameCompare(const void *p1, const void *p2) if (cmpval != 0) return cmpval; } + else if (obj1->objType == DO_FK_CONSTRAINT) + { + ConstraintInfo *cobj1 = *(ConstraintInfo *const *)p1; + ConstraintInfo *cobj2 = *(ConstraintInfo *const *)p2; + + if (cobj1->contable != NULL && cobj2->contable != NULL) { + /* Sort two FK constraints with the same name by their + * corresponding relationname: + */ + cmpval = strcmp( + cobj1->contable->dobj.name, + cobj2->contable->dobj.name); + if (cmpval != 0) + return cmpval; + } + } /* Usually shouldn't get here, but if we do, sort by OID */ return oidcmp(obj1->catId.oid, obj2->catId.oid);

view thread (6+ messages)  latest in thread

Message-ID: <87h73a4bu3.fsf@online.de>
Permalink:  ../87h73a4bu3.fsf@online.de/
Also on:    postgresql.org/message-id/87h73a4bu3.fsf@online.de

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bch@online.de
  Subject: Re: FK Constraint sort order with pg_dump
  In-Reply-To: <87h73a4bu3.fsf@online.de>
  References: <87h73a4bu3.fsf@online.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox