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 1moWvq-0007jB-KH for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Nov 2021 20:25:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1moWvp-0005iL-F8 for pgsql-bugs@arkaria.postgresql.org; Sat, 20 Nov 2021 20:25:53 +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 1moWvp-0005iC-6y for pgsql-bugs@lists.postgresql.org; Sat, 20 Nov 2021 20:25:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1moWvl-00066W-N8 for pgsql-bugs@lists.postgresql.org; Sat, 20 Nov 2021 20:25:51 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 1AKKPls13317885; Sat, 20 Nov 2021 15:25:47 -0500 From: Tom Lane To: =?UTF-8?B?0JTQvNC40YLRgNC40Lkg0JjQstCw0L3QvtCy?= cc: pgsql-bugs@lists.postgresql.org Subject: Re: pg_restore depending on user functions In-reply-to: References: <2011525.1636911069@sss.pgh.pa.us> <2016240.1636915579@sss.pgh.pa.us> <2048100.1636934771@sss.pgh.pa.us> <2124696.1636991365@sss.pgh.pa.us> <2157331.1637014065@sss.pgh.pa.us> <2230909.1637080154@sss.pgh.pa.us> <2254203.1637093046@sss.pgh.pa.us> <2361578.1637113664@sss.pgh.pa.us> <2915502.1637249556@sss.pgh.pa.us> Comments: In-reply-to =?UTF-8?B?0JTQvNC40YLRgNC40Lkg0JjQstCw0L3QvtCy?= message dated "Fri, 19 Nov 2021 09:45:45 +0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3317817.1637439882.0@sss.pgh.pa.us> Date: Sat, 20 Nov 2021 15:25:47 -0500 Message-ID: <3317884.1637439947@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3317817.1637439882.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable =3D?UTF-8?B?0JTQvNC40YLRgNC40Lkg0JjQstCw0L3QvtCy?=3D writes: > I hope this helps you. Okay, that's better. The problem seems to be that the view using the cast is itself depended on (indirectly through another view) by another function a_ext_by_id(), which is using the intermediate view's rowtype as its output type. So that causes the dependency sort to hoist those two views above the function a_ext_by_id(), and now they're ahead of the cast, which is just left at its initial priority-driven location. I think we can fix this by adjusting the sort priority order as per the attached patch. This fixes this toy test case anyway. Can you check to see if it fixes your real database? (Note that as given, the patch will only apply to v14 not earlier branches.) regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="change-dump-priority-of-casts.patch"; charset="us-ascii" Content-ID: <3317817.1637439882.2@sss.pgh.pa.us> Content-Description: change-dump-priority-of-casts.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort= .c index 410d1790ee..5399ec1cd9 100644 --- a/src/bin/pg_dump/pg_dump_sort.c +++ b/src/bin/pg_dump/pg_dump_sort.c @@ -34,6 +34,15 @@ * restore state). If you think to change this, see also the RestorePass * mechanism in pg_backup_archiver.c. * + * On the other hand, casts are intentionally sorted earlier than you mig= ht + * expect; logically they should come after functions, since they usually + * depend on those. This works around the backend's habit of recording + * views that use casts as dependent on the cast's underlying function. + * We initially sort casts first, and then any functions used by casts + * will be hoisted above the casts, and in turn views that those function= s + * depend on will be hoisted above the functions. But views not used tha= t + * way won't be hoisted. + * * NOTE: object-type priorities must match the section assignments made i= n * pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUN= DARY, * POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA obje= cts @@ -49,12 +58,12 @@ enum dbObjectTypePriorities PRIO_TRANSFORM, PRIO_EXTENSION, PRIO_TYPE, /* used for DO_TYPE and DO_SHELL_TYPE */ + PRIO_CAST, PRIO_FUNC, PRIO_AGG, PRIO_ACCESS_METHOD, PRIO_OPERATOR, PRIO_OPFAMILY, /* used for DO_OPFAMILY and DO_OPCLASS */ - PRIO_CAST, PRIO_CONVERSION, PRIO_TSPARSER, PRIO_TSTEMPLATE, ------- =_aaaaaaaaaa0--