Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a592e58

Browse files
committed
Fix pg_dump's heuristic for deciding which casts to dump.
Back in 2003 we had a discussion about how to decide which casts to dump. At the time pg_dump really only considered an object's containing schema to decide what to dump (ie, dump whatever's not in pg_catalog), and so we chose a complicated idea involving whether the underlying types were to be dumped (cf commit a6790ce). But users are allowed to create casts between built-in types, and we failed to dump such casts. Let's get rid of that heuristic, which has accreted even more ugliness since then, in favor of just looking at the cast's OID to decide if it's a built-in cast or not. In passing, also fix some really ancient code that supposed that it had to manufacture a dependency for the cast on its cast function; that's only true when dumping from a pre-7.3 server. This just resulted in some wasted cycles and duplicate dependency-list entries with newer servers, but we might as well improve it. Per gripes from a number of people, most recently Greg Sabino Mullane. Back-patch to all supported branches.
1 parent 433c79d commit a592e58

File tree

1 file changed

+26
-50
lines changed

1 file changed

+26
-50
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,24 @@ selectDumpableDefaultACL(DefaultACLInfo *dinfo)
13421342
dinfo->dobj.dump = include_everything;
13431343
}
13441344

1345+
/*
1346+
* selectDumpableCast: policy-setting subroutine
1347+
* Mark a cast as to be dumped or not
1348+
*
1349+
* Casts do not belong to any particular namespace (since they haven't got
1350+
* names), nor do they have identifiable owners. To distinguish user-defined
1351+
* casts from built-in ones, we must resort to checking whether the cast's
1352+
* OID is in the range reserved for initdb.
1353+
*/
1354+
static void
1355+
selectDumpableCast(CastInfo *cast)
1356+
{
1357+
if (cast->dobj.catId.oid < (Oid) FirstNormalObjectId)
1358+
cast->dobj.dump = false;
1359+
else
1360+
cast->dobj.dump = include_everything;
1361+
}
1362+
13451363
/*
13461364
* selectDumpableExtension: policy-setting subroutine
13471365
* Mark an extension as to be dumped or not
@@ -6258,12 +6276,13 @@ getCasts(Archive *fout, int *numCasts)
62586276
sTypeInfo->dobj.name, tTypeInfo->dobj.name);
62596277
castinfo[i].dobj.name = namebuf.data;
62606278

6261-
if (OidIsValid(castinfo[i].castfunc))
6279+
if (fout->remoteVersion < 70300 &&
6280+
OidIsValid(castinfo[i].castfunc))
62626281
{
62636282
/*
62646283
* We need to make a dependency to ensure the function will be
62656284
* dumped first. (In 7.3 and later the regular dependency
6266-
* mechanism will handle this for us.)
6285+
* mechanism handles this for us.)
62676286
*/
62686287
FuncInfo *funcInfo;
62696288

@@ -6272,6 +6291,9 @@ getCasts(Archive *fout, int *numCasts)
62726291
addObjectDependency(&castinfo[i].dobj,
62736292
funcInfo->dobj.dumpId);
62746293
}
6294+
6295+
/* Decide whether we want to dump it */
6296+
selectDumpableCast(&(castinfo[i]));
62756297
}
62766298

62776299
PQclear(res);
@@ -10154,55 +10176,9 @@ dumpCast(Archive *fout, CastInfo *cast)
1015410176
}
1015510177

1015610178
/*
10157-
* As per discussion we dump casts if one or more of the underlying
10158-
* objects (the conversion function and the two data types) are not
10159-
* builtin AND if all of the non-builtin objects are included in the dump.
10160-
* Builtin meaning, the namespace name does not start with "pg_".
10161-
*
10162-
* However, for a cast that belongs to an extension, we must not use this
10163-
* heuristic, but just dump the cast iff we're told to (via dobj.dump).
10179+
* Make sure we are in proper schema (needed for getFormattedTypeName).
10180+
* Casts don't have a schema of their own, so use pg_catalog.
1016410181
*/
10165-
if (!cast->dobj.ext_member)
10166-
{
10167-
TypeInfo *sourceInfo = findTypeByOid(cast->castsource);
10168-
TypeInfo *targetInfo = findTypeByOid(cast->casttarget);
10169-
10170-
if (sourceInfo == NULL || targetInfo == NULL)
10171-
return;
10172-
10173-
/*
10174-
* Skip this cast if all objects are from pg_
10175-
*/
10176-
if ((funcInfo == NULL ||
10177-
strncmp(funcInfo->dobj.namespace->dobj.name, "pg_", 3) == 0) &&
10178-
strncmp(sourceInfo->dobj.namespace->dobj.name, "pg_", 3) == 0 &&
10179-
strncmp(targetInfo->dobj.namespace->dobj.name, "pg_", 3) == 0)
10180-
return;
10181-
10182-
/*
10183-
* Skip cast if function isn't from pg_ and is not to be dumped.
10184-
*/
10185-
if (funcInfo &&
10186-
strncmp(funcInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10187-
!funcInfo->dobj.dump)
10188-
return;
10189-
10190-
/*
10191-
* Same for the source type
10192-
*/
10193-
if (strncmp(sourceInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10194-
!sourceInfo->dobj.dump)
10195-
return;
10196-
10197-
/*
10198-
* and the target type.
10199-
*/
10200-
if (strncmp(targetInfo->dobj.namespace->dobj.name, "pg_", 3) != 0 &&
10201-
!targetInfo->dobj.dump)
10202-
return;
10203-
}
10204-
10205-
/* Make sure we are in proper schema (needed for getFormattedTypeName) */
1020610182
selectSourceSchema(fout, "pg_catalog");
1020710183

1020810184
defqry = createPQExpBuffer();

0 commit comments

Comments
 (0)