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

Commit 3655b46

Browse files
committed
pg_dump: Refactor code that constructs ALTER ... OWNER TO commands
Avoid having to list all the possible object types twice. Instead, only _getObjectDescription() needs to know about specific object types. It communicates back to _printTocEntry() whether an owner is to be set. In passing, remove the logic to use ALTER TABLE to set the owner of views and sequences. This is no longer necessary. Furthermore, if pg_dump doesn't recognize the object type, this is now a fatal error, not a warning. Reviewed-by: Corey Huinker <corey.huinker@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/0a00f923-599a-381b-923f-0d802a727715@enterprisedb.com
1 parent be541ef commit 3655b46

File tree

1 file changed

+47
-83
lines changed

1 file changed

+47
-83
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+47-83
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ typedef struct _parallelReadyList
7272
static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
7373
const int compression, bool dosync, ArchiveMode mode,
7474
SetupWorkerPtrType setupWorkerPtr);
75-
static void _getObjectDescription(PQExpBuffer buf, TocEntry *te);
75+
static void _getObjectDescription(PQExpBuffer buf, const TocEntry *te);
7676
static void _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData);
7777
static char *sanitize_line(const char *str, bool want_hyphen);
7878
static void _doSetFixedOutputState(ArchiveHandle *AH);
@@ -3398,27 +3398,27 @@ _selectTableAccessMethod(ArchiveHandle *AH, const char *tableam)
33983398
* Extract an object description for a TOC entry, and append it to buf.
33993399
*
34003400
* This is used for ALTER ... OWNER TO.
3401+
*
3402+
* If the object type has no owner, do nothing.
34013403
*/
34023404
static void
3403-
_getObjectDescription(PQExpBuffer buf, TocEntry *te)
3405+
_getObjectDescription(PQExpBuffer buf, const TocEntry *te)
34043406
{
34053407
const char *type = te->desc;
34063408

3407-
/* Use ALTER TABLE for views and sequences */
3408-
if (strcmp(type, "VIEW") == 0 || strcmp(type, "SEQUENCE") == 0 ||
3409-
strcmp(type, "MATERIALIZED VIEW") == 0)
3410-
type = "TABLE";
3411-
34123409
/* objects that don't require special decoration */
34133410
if (strcmp(type, "COLLATION") == 0 ||
34143411
strcmp(type, "CONVERSION") == 0 ||
34153412
strcmp(type, "DOMAIN") == 0 ||
3416-
strcmp(type, "TABLE") == 0 ||
3417-
strcmp(type, "TYPE") == 0 ||
34183413
strcmp(type, "FOREIGN TABLE") == 0 ||
3414+
strcmp(type, "MATERIALIZED VIEW") == 0 ||
3415+
strcmp(type, "SEQUENCE") == 0 ||
3416+
strcmp(type, "STATISTICS") == 0 ||
3417+
strcmp(type, "TABLE") == 0 ||
34193418
strcmp(type, "TEXT SEARCH DICTIONARY") == 0 ||
34203419
strcmp(type, "TEXT SEARCH CONFIGURATION") == 0 ||
3421-
strcmp(type, "STATISTICS") == 0 ||
3420+
strcmp(type, "TYPE") == 0 ||
3421+
strcmp(type, "VIEW") == 0 ||
34223422
/* non-schema-specified objects */
34233423
strcmp(type, "DATABASE") == 0 ||
34243424
strcmp(type, "PROCEDURAL LANGUAGE") == 0 ||
@@ -3427,33 +3427,28 @@ _getObjectDescription(PQExpBuffer buf, TocEntry *te)
34273427
strcmp(type, "FOREIGN DATA WRAPPER") == 0 ||
34283428
strcmp(type, "SERVER") == 0 ||
34293429
strcmp(type, "PUBLICATION") == 0 ||
3430-
strcmp(type, "SUBSCRIPTION") == 0 ||
3431-
strcmp(type, "USER MAPPING") == 0)
3430+
strcmp(type, "SUBSCRIPTION") == 0)
34323431
{
34333432
appendPQExpBuffer(buf, "%s ", type);
34343433
if (te->namespace && *te->namespace)
34353434
appendPQExpBuffer(buf, "%s.", fmtId(te->namespace));
34363435
appendPQExpBufferStr(buf, fmtId(te->tag));
3437-
return;
34383436
}
3439-
34403437
/* BLOBs just have a name, but it's numeric so must not use fmtId */
3441-
if (strcmp(type, "BLOB") == 0)
3438+
else if (strcmp(type, "BLOB") == 0)
34423439
{
34433440
appendPQExpBuffer(buf, "LARGE OBJECT %s", te->tag);
3444-
return;
34453441
}
3446-
34473442
/*
34483443
* These object types require additional decoration. Fortunately, the
34493444
* information needed is exactly what's in the DROP command.
34503445
*/
3451-
if (strcmp(type, "AGGREGATE") == 0 ||
3452-
strcmp(type, "FUNCTION") == 0 ||
3453-
strcmp(type, "OPERATOR") == 0 ||
3454-
strcmp(type, "OPERATOR CLASS") == 0 ||
3455-
strcmp(type, "OPERATOR FAMILY") == 0 ||
3456-
strcmp(type, "PROCEDURE") == 0)
3446+
else if (strcmp(type, "AGGREGATE") == 0 ||
3447+
strcmp(type, "FUNCTION") == 0 ||
3448+
strcmp(type, "OPERATOR") == 0 ||
3449+
strcmp(type, "OPERATOR CLASS") == 0 ||
3450+
strcmp(type, "OPERATOR FAMILY") == 0 ||
3451+
strcmp(type, "PROCEDURE") == 0)
34573452
{
34583453
/* Chop "DROP " off the front and make a modifiable copy */
34593454
char *first = pg_strdup(te->dropStmt + 5);
@@ -3472,9 +3467,24 @@ _getObjectDescription(PQExpBuffer buf, TocEntry *te)
34723467
free(first);
34733468
return;
34743469
}
3475-
3476-
pg_log_warning("don't know how to set owner for object type \"%s\"",
3477-
type);
3470+
/* these object types don't have separate owners */
3471+
else if (strcmp(type, "CAST") == 0 ||
3472+
strcmp(type, "CHECK CONSTRAINT") == 0 ||
3473+
strcmp(type, "CONSTRAINT") == 0 ||
3474+
strcmp(type, "DATABASE PROPERTIES") == 0 ||
3475+
strcmp(type, "DEFAULT") == 0 ||
3476+
strcmp(type, "FK CONSTRAINT") == 0 ||
3477+
strcmp(type, "INDEX") == 0 ||
3478+
strcmp(type, "RULE") == 0 ||
3479+
strcmp(type, "TRIGGER") == 0 ||
3480+
strcmp(type, "ROW SECURITY") == 0 ||
3481+
strcmp(type, "POLICY") == 0 ||
3482+
strcmp(type, "USER MAPPING") == 0)
3483+
{
3484+
/* do nothing */
3485+
}
3486+
else
3487+
pg_fatal("don't know how to set owner for object type \"%s\"", type);
34783488
}
34793489

34803490
/*
@@ -3575,8 +3585,7 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData)
35753585
* instead issue an ALTER OWNER command. Schema "public" is special; when
35763586
* a dump emits a comment in lieu of creating it, we use ALTER OWNER even
35773587
* when using SET SESSION for all other objects. We assume that anything
3578-
* without a DROP command is not a separately ownable object. All the
3579-
* categories with DROP commands must appear in one list or the other.
3588+
* without a DROP command is not a separately ownable object.
35803589
*/
35813590
if (!ropt->noOwner &&
35823591
(!ropt->use_setsessauth ||
@@ -3585,62 +3594,17 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData)
35853594
te->owner && strlen(te->owner) > 0 &&
35863595
te->dropStmt && strlen(te->dropStmt) > 0)
35873596
{
3588-
if (strcmp(te->desc, "AGGREGATE") == 0 ||
3589-
strcmp(te->desc, "BLOB") == 0 ||
3590-
strcmp(te->desc, "COLLATION") == 0 ||
3591-
strcmp(te->desc, "CONVERSION") == 0 ||
3592-
strcmp(te->desc, "DATABASE") == 0 ||
3593-
strcmp(te->desc, "DOMAIN") == 0 ||
3594-
strcmp(te->desc, "FUNCTION") == 0 ||
3595-
strcmp(te->desc, "OPERATOR") == 0 ||
3596-
strcmp(te->desc, "OPERATOR CLASS") == 0 ||
3597-
strcmp(te->desc, "OPERATOR FAMILY") == 0 ||
3598-
strcmp(te->desc, "PROCEDURE") == 0 ||
3599-
strcmp(te->desc, "PROCEDURAL LANGUAGE") == 0 ||
3600-
strcmp(te->desc, "SCHEMA") == 0 ||
3601-
strcmp(te->desc, "EVENT TRIGGER") == 0 ||
3602-
strcmp(te->desc, "TABLE") == 0 ||
3603-
strcmp(te->desc, "TYPE") == 0 ||
3604-
strcmp(te->desc, "VIEW") == 0 ||
3605-
strcmp(te->desc, "MATERIALIZED VIEW") == 0 ||
3606-
strcmp(te->desc, "SEQUENCE") == 0 ||
3607-
strcmp(te->desc, "FOREIGN TABLE") == 0 ||
3608-
strcmp(te->desc, "TEXT SEARCH DICTIONARY") == 0 ||
3609-
strcmp(te->desc, "TEXT SEARCH CONFIGURATION") == 0 ||
3610-
strcmp(te->desc, "FOREIGN DATA WRAPPER") == 0 ||
3611-
strcmp(te->desc, "SERVER") == 0 ||
3612-
strcmp(te->desc, "STATISTICS") == 0 ||
3613-
strcmp(te->desc, "PUBLICATION") == 0 ||
3614-
strcmp(te->desc, "SUBSCRIPTION") == 0)
3615-
{
3616-
PQExpBuffer temp = createPQExpBuffer();
3597+
PQExpBufferData temp;
36173598

3618-
appendPQExpBufferStr(temp, "ALTER ");
3619-
_getObjectDescription(temp, te);
3620-
appendPQExpBuffer(temp, " OWNER TO %s;", fmtId(te->owner));
3621-
ahprintf(AH, "%s\n\n", temp->data);
3622-
destroyPQExpBuffer(temp);
3623-
}
3624-
else if (strcmp(te->desc, "CAST") == 0 ||
3625-
strcmp(te->desc, "CHECK CONSTRAINT") == 0 ||
3626-
strcmp(te->desc, "CONSTRAINT") == 0 ||
3627-
strcmp(te->desc, "DATABASE PROPERTIES") == 0 ||
3628-
strcmp(te->desc, "DEFAULT") == 0 ||
3629-
strcmp(te->desc, "FK CONSTRAINT") == 0 ||
3630-
strcmp(te->desc, "INDEX") == 0 ||
3631-
strcmp(te->desc, "RULE") == 0 ||
3632-
strcmp(te->desc, "TRIGGER") == 0 ||
3633-
strcmp(te->desc, "ROW SECURITY") == 0 ||
3634-
strcmp(te->desc, "POLICY") == 0 ||
3635-
strcmp(te->desc, "USER MAPPING") == 0)
3636-
{
3637-
/* these object types don't have separate owners */
3638-
}
3639-
else
3640-
{
3641-
pg_log_warning("don't know how to set owner for object type \"%s\"",
3642-
te->desc);
3643-
}
3599+
initPQExpBuffer(&temp);
3600+
_getObjectDescription(&temp, te);
3601+
/*
3602+
* If _getObjectDescription() didn't fill the buffer, then there is no
3603+
* owner.
3604+
*/
3605+
if (temp.data[0])
3606+
ahprintf(AH, "ALTER %s OWNER TO %s;\n\n", temp.data, fmtId(te->owner));
3607+
termPQExpBuffer(&temp);
36443608
}
36453609

36463610
/*

0 commit comments

Comments
 (0)