Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit916710f

Browse files
committed
pg_dump -z has gotten rather thoroughly broken in the last couple
of days --- it was emitting stuff likeREVOKE ALL on 'table' from PUBLIC; GRANT ALL on "table" to"Public"; neither of which work. While I was at it Icleaned up a few other things:* \connect commands are issued only in -z mode. In this way,reloading a pg_dump script made without -z will generate a simpledatabase wholly owned by the invoking user, rather than a mishmashof tables owned by various people but lacking in access rights.(Analogy: cp versus cp -p.)* \connect commands are issued just before COPY FROM stdin commands;without this, reloading a database containing non-world-writabletables tended to fail because the COPY was not necessarily attemptedas the table owner.* Redundant \connect commands are suppressed (each one costs abackend launch, so...).* Man page updated (-z wasn't ever documented).The first two items were discussed in a pgsql-hackers thread around6 May 98 ("An item for the TODO list: pg_dump and multiple tableowners") but no one had bothered to deal with 'em yet.regards, tom lane
1 parentb41468d commit916710f

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

‎src/bin/pg_dump/pg_dump.c

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
*
2323
* IDENTIFICATION
24-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.77 1998/07/08 14:33:19 thomas Exp $
24+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.78 1998/07/19 05:24:49 momjian Exp $
2525
*
2626
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2727
*
@@ -95,6 +95,7 @@ static void setMaxOid(FILE *fout);
9595
staticchar*AddAcl(char*s,constchar*add);
9696
staticchar*GetPrivledges(char*s);
9797
staticACL*ParseACL(constchar*acls,int*count);
98+
staticvoidbecomeUser(FILE*fout,constchar*username);
9899

99100
externchar*optarg;
100101
externintoptind,
@@ -110,6 +111,7 @@ intdumpData;/* dump data using proper insert strings */
110111
intattrNames;/* put attr names into insert strings */
111112
intschemaOnly;
112113
intdataOnly;
114+
intaclsOption;
113115

114116
charg_opaque_type[10];/* name for the opaque type */
115117

@@ -141,12 +143,12 @@ usage(const char *progname)
141143
"\t -s \t\t dump out only the schema, no data\n");
142144
fprintf(stderr,
143145
"\t -t table \t\t dump for this table only\n");
146+
fprintf(stderr,
147+
"\t -u \t\t use password authentication\n");
144148
fprintf(stderr,
145149
"\t -v \t\t verbose\n");
146150
fprintf(stderr,
147151
"\t -z \t\t dump ACLs (grant/revoke)\n");
148-
fprintf(stderr,
149-
"\t -u \t\t use password authentication\n");
150152
fprintf(stderr,
151153
"\nIf dbname is not supplied, then the DATABASE environment "
152154
"variable value is used.\n");
@@ -435,7 +437,7 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout,
435437
if (g_verbose)
436438
fprintf(stderr,"%s dumping out schema of sequence '%s' %s\n",
437439
g_comment_start,tblinfo[i].relname,g_comment_end);
438-
fprintf(fout,"\\connect - %s\n",tblinfo[i].usename);
440+
becomeUser(fout,tblinfo[i].usename);
439441
dumpSequence(fout,tblinfo[i]);
440442
}
441443
}
@@ -458,6 +460,8 @@ dumpClasses(const TableInfo tblinfo[], const int numTables, FILE *fout,
458460
fprintf(stderr,"%s dumping out the contents of Table '%s' %s\n",
459461
g_comment_start,classname,g_comment_end);
460462

463+
becomeUser(fout,tblinfo[i].usename);
464+
461465
if (!dumpData)
462466
dumpClasses_nodumpData(fout,classname,oids);
463467
else
@@ -534,8 +538,7 @@ main(int argc, char **argv)
534538
constchar*pghost=NULL;
535539
constchar*pgport=NULL;
536540
char*tablename=NULL;
537-
intoids=0,
538-
acls=0;
541+
intoids=0;
539542
TableInfo*tblinfo;
540543
intnumTables;
541544
charconnect_string[512]="";
@@ -598,8 +601,8 @@ main(int argc, char **argv)
598601
case'v':/* verbose */
599602
g_verbose= true;
600603
break;
601-
case'z':/* Dumpoids */
602-
acls=1;
604+
case'z':/* DumpACLs and table ownership info */
605+
aclsOption=1;
603606
break;
604607
case'u':
605608
use_password=1;
@@ -657,11 +660,11 @@ main(int argc, char **argv)
657660
strcat(connect_string,tmp_string);
658661
sprintf(tmp_string,"password=%s ",password);
659662
strcat(connect_string,tmp_string);
660-
bzero(tmp_string,sizeof(tmp_string));
661-
bzero(password,sizeof(password));
663+
MemSet(tmp_string,0,sizeof(tmp_string));
664+
MemSet(password,0,sizeof(password));
662665
}
663666
g_conn=PQconnectdb(connect_string);
664-
bzero(connect_string,sizeof(connect_string));
667+
MemSet(connect_string,0,sizeof(connect_string));
665668
/* check to see that the backend connection was successfully made */
666669
if (PQstatus(g_conn)==CONNECTION_BAD)
667670
{
@@ -679,10 +682,10 @@ main(int argc, char **argv)
679682
if (g_verbose)
680683
fprintf(stderr,"%s last builtin oid is %d %s\n",
681684
g_comment_start,g_last_builtin_oid,g_comment_end);
682-
tblinfo=dumpSchema(g_fout,&numTables,tablename,acls);
685+
tblinfo=dumpSchema(g_fout,&numTables,tablename,aclsOption);
683686
}
684687
else
685-
tblinfo=dumpSchema(NULL,&numTables,tablename,acls);
688+
tblinfo=dumpSchema(NULL,&numTables,tablename,aclsOption);
686689

687690
if (!schemaOnly)
688691
dumpClasses(tblinfo,numTables,g_fout,tablename,oids);
@@ -1961,7 +1964,7 @@ dumpTypes(FILE *fout, FuncInfo *finfo, int numFuncs,
19611964
if (funcInd!=-1)
19621965
dumpOneFunc(fout,finfo,funcInd,tinfo,numTypes);
19631966

1964-
fprintf(fout,"\\connect - %s\n",tinfo[i].usename);
1967+
becomeUser(fout,tinfo[i].usename);
19651968

19661969
sprintf(q,
19671970
"CREATE TYPE \"%s\" "
@@ -2028,7 +2031,7 @@ dumpOneFunc(FILE *fout, FuncInfo *finfo, int i,
20282031
else
20292032
finfo[i].dumped=1;
20302033

2031-
fprintf(fout,"\\connect - %s\n",finfo[i].usename);
2034+
becomeUser(fout,finfo[i].usename);
20322035

20332036
sprintf(q,"CREATE FUNCTION \"%s\" (",finfo[i].proname);
20342037
for (j=0;j<finfo[i].nargs;j++)
@@ -2143,7 +2146,7 @@ dumpOprs(FILE *fout, OprInfo *oprinfo, int numOperators,
21432146
oprinfo[i].oprlsortop));
21442147
}
21452148

2146-
fprintf(fout,"\\connect - %s\n",oprinfo[i].usename);
2149+
becomeUser(fout,oprinfo[i].usename);
21472150

21482151
sprintf(q,
21492152
"CREATE OPERATOR %s "
@@ -2238,7 +2241,7 @@ dumpAggs(FILE *fout, AggInfo *agginfo, int numAggs,
22382241
else
22392242
comma2[0]='\0';
22402243

2241-
fprintf(fout,"\\connect - %s\n",agginfo[i].usename);
2244+
becomeUser(fout,agginfo[i].usename);
22422245

22432246
sprintf(q,"CREATE AGGREGATE %s ( %s %s%s %s%s %s );\n",
22442247
agginfo[i].aggname,
@@ -2349,7 +2352,7 @@ ParseACL(const char *acls, int *count)
23492352
s=strdup(acls);
23502353

23512354
/* Setup up public */
2352-
ParsedAcl[0].user=strdup("Public");
2355+
ParsedAcl[0].user=NULL;/* indicates PUBLIC */
23532356
tok=strtok(s,",");
23542357
ParsedAcl[0].privledges=GetPrivledges(strchr(tok,'='));
23552358

@@ -2397,16 +2400,23 @@ dumpACL(FILE *fout, TableInfo tbinfo)
23972400

23982401
/* Revoke Default permissions for PUBLIC */
23992402
fprintf(fout,
2400-
"REVOKE ALL on'%s' from PUBLIC;\n",
2403+
"REVOKE ALL on\"%s\" from PUBLIC;\n",
24012404
tbinfo.relname);
24022405

24032406
for (k=0;k<l;k++)
24042407
{
24052408
if (ACLlist[k].privledges!= (char*)NULL)
2406-
fprintf(fout,
2407-
"GRANT %s on \"%s\" to \"%s\";\n",
2408-
ACLlist[k].privledges,tbinfo.relname,
2409-
ACLlist[k].user);
2409+
{
2410+
if (ACLlist[k].user== (char*)NULL)
2411+
fprintf(fout,
2412+
"GRANT %s on \"%s\" to PUBLIC;\n",
2413+
ACLlist[k].privledges,tbinfo.relname);
2414+
else
2415+
fprintf(fout,
2416+
"GRANT %s on \"%s\" to \"%s\";\n",
2417+
ACLlist[k].privledges,tbinfo.relname,
2418+
ACLlist[k].user);
2419+
}
24102420
}
24112421
}
24122422

@@ -2437,7 +2447,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
24372447
continue;
24382448
if (!tablename|| (!strcmp(tblinfo[i].relname,tablename)))
24392449
{
2440-
fprintf(fout,"\\connect - %s\n",tblinfo[i].usename);
2450+
becomeUser(fout,tblinfo[i].usename);
24412451
dumpSequence(fout,tblinfo[i]);
24422452
if (acls)
24432453
dumpACL(fout,tblinfo[i]);
@@ -2459,7 +2469,7 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
24592469
parentRels=tblinfo[i].parentRels;
24602470
numParents=tblinfo[i].numParents;
24612471

2462-
fprintf(fout,"\\connect - %s\n",tblinfo[i].usename);
2472+
becomeUser(fout,tblinfo[i].usename);
24632473

24642474
sprintf(q,"CREATE TABLE \"%s\" (",fmtId(tblinfo[i].relname));
24652475
actual_atts=0;
@@ -2954,8 +2964,30 @@ dumpTriggers(FILE *fout, const char *tablename,
29542964
continue;
29552965
for (j=0;j<tblinfo[i].ntrig;j++)
29562966
{
2957-
fprintf(fout,"\\connect - %s\n",tblinfo[i].usename);
2967+
becomeUser(fout,tblinfo[i].usename);
29582968
fputs(tblinfo[i].triggers[j],fout);
29592969
}
29602970
}
29612971
}
2972+
2973+
2974+
/* Issue a psql \connect command to become the specified user.
2975+
* We want to do this only if we are dumping ACLs,
2976+
* and only if the new username is different from the last one
2977+
* (to avoid the overhead of useless backend launches).
2978+
*/
2979+
2980+
staticvoidbecomeUser(FILE*fout,constchar*username)
2981+
{
2982+
staticconstchar*lastusername="";
2983+
2984+
if (!aclsOption)
2985+
return;
2986+
2987+
if (strcmp(lastusername,username)==0)
2988+
return;
2989+
2990+
fprintf(fout,"\\connect - %s\n",username);
2991+
2992+
lastusername=username;
2993+
}

‎src/man/pg_dump.1

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.\" This is -*-nroff-*-
22
.\" XXX standard disclaimer belongs here....
3-
.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.11 1998/06/24 13:21:28 momjian Exp $
4-
.TH PG_DUMP UNIX1/20/96 PostgreSQL PostgreSQL
3+
.\" $Header: /cvsroot/pgsql/src/man/Attic/pg_dump.1,v 1.12 1998/07/19 05:24:51 momjian Exp $
4+
.TH PG_DUMP UNIX7/15/98 PostgreSQL PostgreSQL
55
.SH NAME
66
pg_dump - dumps out a Postgres database into a script file
77
.SH SYNOPSIS
@@ -36,23 +36,27 @@ port]
3636
.BR"-t"
3737
table]
3838
[\c
39+
.BR"-u"
40+
]
41+
[\c
3942
.BR"-v"
4043
]
4144
[\c
42-
.BR"-u"]
45+
.BR"-z"
46+
]
4347
dbname
4448
.in-5n
4549
.SH DESCRIPTION
4650
.IR"pg_dump"
4751
is a utility for dumping out a
4852
Postgres database into a script file containing query commands. The script
49-
files are inaASCII format and can be used to reconstruct the database,
53+
files are in ASCII format and can be used to reconstruct the database,
5054
even on other machines and other architectures.
5155
.IR"pg_dump"
5256
will produce the queries necessary to re-generate all
5357
user-defined types, functions, tables, indices, aggregates, and
5458
operators. In addition, all the data is copied out in ASCII format so
55-
that it can be readily copied in again, as well, as imported into tools
59+
that it can be readily copied in again, as well as imported into tools
5660
for textual editing.
5761
.PP
5862
.IR"pg_dump"
@@ -92,10 +96,13 @@ Dump out only the schema, no data
9296
Dump for this table only
9397
.TP
9498
.BR"-u"
95-
Use password authentication. Prompts for username and password.
99+
Use password authentication. Prompts for username and password
96100
.TP
97101
.BR"-v"""
98102
Specifies verbose mode
103+
.TP
104+
.BR"-z"""
105+
Include ACLs (grant/revoke commands) and table ownership information
99106
.PP
100107
If dbname is not supplied, then the DATABASE environment variable value is used.
101108
.SH "CAVEATS AND LIMITATIONS"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp