Skip to content

Commit 69949ea

Browse files
committed
Adjust pg_dump's priority ordering for casts.
When a stored expression depends on a user-defined cast, the backend records the dependency as being on the cast's implementation function --- or indeed, if there's no cast function involved but just RelabelType or CoerceViaIO, no dependency is recorded at all. This is problematic for pg_dump, which is at risk of dumping things in the wrong order leading to restore failures. Given the lack of previous reports, the risk isn't that high, but it can be demonstrated if the cast is used in some view whose rowtype is then used as an input or result type for some other function. (That results in the view getting hoisted into the functions portion of the dump, ahead of the cast.) A logically bulletproof fix for this would require including the cast's OID in the parsed form of the expression, whence it could be extracted by dependency.c, and then the stored dependency would force pg_dump to do the right thing. Such a change would be fairly invasive, and certainly not back-patchable. Moreover, since we'd prefer that an expression using cast syntax be equal() to one doing the same thing by explicit function call, the cast OID field would have to have special ignored-by-comparisons semantics, making things messy. So, let's instead fix this by a very simple hack in pg_dump: change the object-type priority order so that casts are initially sorted before functions, immediately after types. This fixes the problem in a fairly direct way for casts that have no implementation function. For those that do, the implementation function will be hoisted to just before the cast by the dependency sorting step, so that we still have a valid dump order. (I'm not sure that this provides a full guarantee of no problems; but since it's been like this for many years without any previous reports, this is probably enough to fix it in practice.) Per report from Дмитрий Иванов. Back-patch to all supported branches. Discussion: https://postgr.es/m/CAPL5KHoGa3uvyKp6z6m48LwCnTsK+LRQ_mcA4uKGfqAVSEjV_A@mail.gmail.com
1 parent 7258253 commit 69949ea

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/bin/pg_dump/pg_dump_sort.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
* restore state). If you think to change this, see also the RestorePass
3636
* mechanism in pg_backup_archiver.c.
3737
*
38+
* On the other hand, casts are intentionally sorted earlier than you might
39+
* expect; logically they should come after functions, since they usually
40+
* depend on those. This works around the backend's habit of recording
41+
* views that use casts as dependent on the cast's underlying function.
42+
* We initially sort casts first, and then any functions used by casts
43+
* will be hoisted above the casts, and in turn views that those functions
44+
* depend on will be hoisted above the functions. But views not used that
45+
* way won't be hoisted.
46+
*
3847
* NOTE: object-type priorities must match the section assignments made in
3948
* pg_dump.c; that is, PRE_DATA objects must sort before DO_PRE_DATA_BOUNDARY,
4049
* POST_DATA objects must sort after DO_POST_DATA_BOUNDARY, and DATA objects
@@ -46,12 +55,12 @@ static const int dbObjectTypePriority[] =
4655
4, /* DO_EXTENSION */
4756
5, /* DO_TYPE */
4857
5, /* DO_SHELL_TYPE */
49-
6, /* DO_FUNC */
50-
7, /* DO_AGG */
51-
8, /* DO_OPERATOR */
52-
8, /* DO_ACCESS_METHOD */
53-
9, /* DO_OPCLASS */
54-
9, /* DO_OPFAMILY */
58+
7, /* DO_FUNC */
59+
8, /* DO_AGG */
60+
9, /* DO_OPERATOR */
61+
9, /* DO_ACCESS_METHOD */
62+
10, /* DO_OPCLASS */
63+
10, /* DO_OPFAMILY */
5564
3, /* DO_COLLATION */
5665
11, /* DO_CONVERSION */
5766
18, /* DO_TABLE */
@@ -64,7 +73,7 @@ static const int dbObjectTypePriority[] =
6473
27, /* DO_CONSTRAINT */
6574
33, /* DO_FK_CONSTRAINT */
6675
2, /* DO_PROCLANG */
67-
10, /* DO_CAST */
76+
6, /* DO_CAST */
6877
23, /* DO_TABLE_DATA */
6978
24, /* DO_SEQUENCE_SET */
7079
19, /* DO_DUMMY_TYPE */

0 commit comments

Comments
 (0)