From 8272749e8ca1dbbcb5f8cf5632ec26a573ac3111 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 17 Oct 2022 14:02:05 -0400 Subject: Record dependencies of a cast on other casts that it requires. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When creating a cast that uses a conversion function, we've historically allowed the input and result types to be binary-compatible with the function's input and result types, rather than necessarily being identical. This means that the new cast is logically dependent on the binary-compatible cast or casts that it references: if those are defined by pg_cast entries, and you try to restore the new cast without having defined them, it'll fail. Hence, we should make pg_depend entries to record these dependencies so that pg_dump knows that there is an ordering requirement. This is not the only place where we allow such shortcuts; aggregate functions for example are similarly lax, and in principle should gain similar dependencies. However, for now it seems sufficient to fix the cast-versus-cast case, as pg_dump's other ordering heuristics should keep it out of trouble for other object types. Per report from David TuroĊˆ; thanks also to Robert Haas for preliminary investigation. I considered back-patching, but seeing that this issue has existed for many years without previous reports, it's not clear it's worth the trouble. Moreover, back-patching wouldn't be enough to ensure that the new pg_depend entries exist in existing databases anyway. Discussion: https://postgr.es/m/OF0A160F3E.578B15D1-ONC12588DA.003E4857-C12588DA.0045A428@notes.linuxbox.cz --- src/backend/commands/functioncmds.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'src/backend/commands/functioncmds.c') diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c index e6fcfc23b93..1f820c93e96 100644 --- a/src/backend/commands/functioncmds.c +++ b/src/backend/commands/functioncmds.c @@ -1526,6 +1526,8 @@ CreateCast(CreateCastStmt *stmt) char sourcetyptype; char targettyptype; Oid funcid; + Oid incastid = InvalidOid; + Oid outcastid = InvalidOid; int nargs; char castcontext; char castmethod; @@ -1603,7 +1605,9 @@ CreateCast(CreateCastStmt *stmt) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("cast function must take one to three arguments"))); - if (!IsBinaryCoercible(sourcetypeid, procstruct->proargtypes.values[0])) + if (!IsBinaryCoercibleWithCast(sourcetypeid, + procstruct->proargtypes.values[0], + &incastid)) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("argument of cast function must match or be binary-coercible from source data type"))); @@ -1617,7 +1621,9 @@ CreateCast(CreateCastStmt *stmt) (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("third argument of cast function must be type %s", "boolean"))); - if (!IsBinaryCoercible(procstruct->prorettype, targettypeid)) + if (!IsBinaryCoercibleWithCast(procstruct->prorettype, + targettypeid, + &outcastid)) ereport(ERROR, (errcode(ERRCODE_INVALID_OBJECT_DEFINITION), errmsg("return data type of cast function must match or be binary-coercible to target data type"))); @@ -1756,8 +1762,8 @@ CreateCast(CreateCastStmt *stmt) break; } - myself = CastCreate(sourcetypeid, targettypeid, funcid, castcontext, - castmethod, DEPENDENCY_NORMAL); + myself = CastCreate(sourcetypeid, targettypeid, funcid, incastid, outcastid, + castcontext, castmethod, DEPENDENCY_NORMAL); return myself; } -- cgit v1.2.3