Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEZvn-0000up-V1 for pgsql-general@arkaria.postgresql.org; Thu, 21 Jul 2022 17:25:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oEZvl-0008Ba-Qd for pgsql-general@arkaria.postgresql.org; Thu, 21 Jul 2022 17:25:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEZvl-00085X-Fg for pgsql-general@lists.postgresql.org; Thu, 21 Jul 2022 17:25:45 +0000 Received: from mout.kundenserver.de ([217.72.192.74]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEZvi-0005Sh-PJ for pgsql-general@lists.postgresql.org; Thu, 21 Jul 2022 17:25:45 +0000 Received: from x280 ([84.189.156.9]) by mrelayeu.kundenserver.de (mreue108 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MVe9i-1o6yi90USa-00Raft for ; Thu, 21 Jul 2022 19:25:41 +0200 From: Christian Barthel To: pgsql-general@lists.postgresql.org Subject: FK Constraint sort order with pg_dump X-Gpg-Key: https://onfire.org/bch/pgp/pubkey.asc X-Archive: expiry=7 Date: Thu, 21 Jul 2022 19:25:40 +0200 Message-ID: <87h73a4bu3.fsf@online.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Provags-ID: V03:K1:7J91KBIz/KxXMznGhE9kySDz5JTQTER7YTvFkyvwl6Oh60fauG9 PdfPxuzGaDDnqQ9+6eOhjsL/uIqPKi77b9RzHa7CsBI5iZ5arLT2WIhz+Q1tFPcnb1Oj2pl +T8QqwAeXOn4Fv0zBhC2eQyBIA9q6SeUo7S6D5bI8lPeaWntXb3EsYvxB2HRhaiI1NNP7H0 6TUDx/Js3Eag/BMwkLRXg== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:2ydKz99+dSA=:o531geoEREyNyhQHu1uYKE GfqLT0cIpb3XGVlFkhuSwSIqgUOXekspvNWHQZ4g/NepobnckJGo8/l3dDl7hYJ2vGa4E2M07 Edi8Vj7+sr7Q8WOcDsnw3APeT+lR00N6O3sbQ+2qMu51gKgk7iZOg8kO8STCiErFaL2CkOsiO fzz1ObkZoSgHm4jDWl4pxTeEaTtj/YhCNjrvNs45eWNzaiFS2Io8ox7OeRgYwLZuMAhQ1G24K P/pI5w+XsBqOZ+J4dPLpeHtZXV8uOrsQypVntRzQcMVyx0faybWyyTUvg5Wg6NDN3UA3Hug1u 67ygxoihWTkhH3R8vL8Nocgu498sAimFtpZrYEospIMWv6Gj3clMlk/8fNNSRddGB+jt6gIkz 5iC8pf23W2GwzoTZfQIUlHqzkfOwDuygmTJtEupEKolJAxqNMm3gsv44VY2u6tTkOxbWeFr6E gzDmvjG1ghmJN/1tv6iFibzCMKA0qVQqbyc9wkBUWw0aj04wY4ZTi2sVkyoBTwivvMEqd7zrK KUvNV/QtzUUXMYCFWBl2FvxAM3htEzdWr/YIoreSfUilZ+24Im0Zu+/ktkTpKs6XPDlDZBWpW OLHYH8Ew/pn0NFhnrt83Nz2kc1bnFUGcW0y9oc5AV/N3SDtweN7rcrV4z8QzrYfJwj7x3yP98 VaDlk6BbYpYQbsuPQOkjUerfh8JxzegqS40IeKS/MJp9bHjvNdRdprYAZcy2qXQ1nfNfhqsGR hDOk2ulaWcaMYQd7cvCKiRk1JtpdFB6UlBQD/Q== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello, The sorting order of FK constraints with the same name is based on the OID (because it lands in the =E2=80=9CUsually shouldn=E2=80=99t get here=E2= =80=9D OID comparison block at [1]). Wouldn=E2=80=99t it be better if the order of those constra= ints 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--- --- version1 2022-07-21 19:16:31.369010843 +0200 +++ version2 2022-07-21 19:16:26.688976178 +0200 @@ -86,18 +86,18 @@ =20 =20 -- --- Name: b x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch +-- Name: c x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch -- =20 -ALTER TABLE ONLY public.b +ALTER TABLE ONLY public.c ADD CONSTRAINT x_fkey FOREIGN KEY (id) REFERENCES public.a(id); =20 =20 -- --- Name: c x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch +-- Name: b x_fkey; Type: FK CONSTRAINT; Schema: public; Owner: bch -- =20 -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=3Dpostgresql.git;a=3Dblob;f=3Dsrc/bin/= pg_dump/pg_dump_sort.c;h=3D80641cd79a2e6ce0a10bd55218b10d22ac369ed5;hb=3D7c= 850320d8cfa5503ecec61c2559661b924f7595#l212 (*) Tested on 14.4 --=20 Christian Barthel --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=fk-sorting.patch 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); --=-=-=--