Skip to content

Commit 8bfe93c

Browse files
committed
pg_dump and pg_restore were stripping quotes and downcasing some but
not all SQL identifiers taken from command line arguments. We decided years ago that that was a bad idea: identifiers taken from the command line should be treated as literally correct. Remove the inconsistent code that has crept in recently. Also fix pg_dump so that the combination of --schema and --table does what you'd expect, namely dump exactly one table from exactly one schema. Per gripe from Deepak Bhole of Red Hat.
1 parent 7481b7d commit 8bfe93c

File tree

4 files changed

+37
-158
lines changed

4 files changed

+37
-158
lines changed

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.61 2003/04/17 15:34:37 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_dump.sgml,v 1.62 2003/06/11 16:29:42 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -287,9 +287,9 @@ PostgreSQL documentation
287287
<note>
288288
<para>
289289
In this mode, <application>pg_dump</application> makes no
290-
attempt to dump any other database objects that may depend
291-
upon objects in the selected schema. Therefore, there is no
292-
guarantee that the results of a single schema dump can be
290+
attempt to dump any other database objects that objects in the
291+
selected schema may depend upon. Therefore, there is no
292+
guarantee that the results of a single-schema dump can be
293293
successfully restored by themselves into a clean database.
294294
</para>
295295
</note>
@@ -394,18 +394,18 @@ PostgreSQL documentation
394394
<listitem>
395395
<para>
396396
Dump data for <replaceable class="parameter">table</replaceable>
397-
only. If <literal>*</literal> is specified, all tables in the
398-
specified database will be dumped. It is possible for there to be
397+
only. It is possible for there to be
399398
multiple tables with the same name in different schemas; if that
400-
is the case, all matching tables will be dumped.
399+
is the case, all matching tables will be dumped. Specify both
400+
<option>--schema</> and <option>--table</> to select just one table.
401401
</para>
402402

403403
<note>
404404
<para>
405405
In this mode, <application>pg_dump</application> makes no
406-
attempt to dump any other database objects that may depend
407-
upon the selected table. Therefore, there is no guarantee
408-
that the results of a single table dump can be successfully
406+
attempt to dump any other database objects that the selected table
407+
may depend upon. Therefore, there is no guarantee
408+
that the results of a single-table dump can be successfully
409409
restored by themselves into a clean database.
410410
</para>
411411
</note>
@@ -652,7 +652,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
652652

653653
<para>
654654
Once restored, it is wise to run <command>ANALYZE</> on each
655-
restored object so the optimizer has useful statistics.
655+
restored table so the optimizer has useful statistics.
656656
</para>
657657

658658
</refsect1>

doc/src/sgml/ref/pg_restore.sgml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.38 2003/03/25 16:15:43 petere Exp $ -->
1+
<!-- $Header: /cvsroot/pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.39 2003/06/11 16:29:42 tgl Exp $ -->
22

33
<refentry id="APP-PGRESTORE">
44
<refmeta>
@@ -269,7 +269,9 @@
269269
<term><option>--function=<replaceable class="parameter">function-name(argtype [, ...])</replaceable></option></term>
270270
<listitem>
271271
<para>
272-
Restore the named function only.
272+
Restore the named function only. Be careful to spell the function
273+
name and arguments exactly as they appear in the dump file's table
274+
of contents.
273275
</para>
274276
</listitem>
275277
</varlistentry>
@@ -318,7 +320,7 @@
318320
<term><option>--schema-only</option></term>
319321
<listitem>
320322
<para>
321-
Restore only the schema (data defintions), not the data.
323+
Restore only the schema (data definitions), not the data.
322324
Sequence values will be reset.
323325
</para>
324326
</listitem>
@@ -557,7 +559,7 @@ CREATE DATABASE foo WITH TEMPLATE template0;
557559

558560
<para>
559561
Once restored, it is wise to run <command>ANALYZE</> on each
560-
restored object so the optimizer has useful statistics.
562+
restored table so the optimizer has useful statistics.
561563
</para>
562564

563565
</refsect1>

src/bin/pg_dump/pg_dump.c

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.332 2003/06/11 05:13:08 momjian Exp $
15+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.333 2003/06/11 16:29:42 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -72,7 +72,6 @@ typedef struct _dumpContext
7272
} DumpContext;
7373

7474
static void help(const char *progname);
75-
static void formatIdentifierArg(char *identifier);
7675
static NamespaceInfo *findNamespace(const char *nsoid, const char *objoid);
7776
static void dumpClasses(const TableInfo *tblinfo, const int numTables,
7877
Archive *fout, const bool oids);
@@ -311,7 +310,6 @@ main(int argc, char **argv)
311310

312311
case 'n': /* Dump data for this schema only */
313312
selectSchemaName = strdup(optarg);
314-
formatIdentifierArg(selectSchemaName);
315313
break;
316314

317315
case 'o': /* Dump oids */
@@ -341,17 +339,6 @@ main(int argc, char **argv)
341339

342340
case 't': /* Dump data for this table only */
343341
selectTableName = strdup(optarg);
344-
345-
/*
346-
* '*' is a special case meaning ALL tables, but
347-
* only if unquoted
348-
*/
349-
if (selectTableName[0] != '"' &&
350-
strcmp(selectTableName, "*") == 0)
351-
selectTableName[0] = '\0';
352-
else
353-
formatIdentifierArg(selectTableName);
354-
355342
break;
356343

357344
case 'u':
@@ -436,10 +423,10 @@ main(int argc, char **argv)
436423
exit(1);
437424
}
438425

439-
if (outputBlobs && selectTableName != NULL && strlen(selectTableName) > 0)
426+
if (outputBlobs && selectTableName != NULL)
440427
{
441428
write_msg(NULL, "Large object output is not supported for a single table.\n");
442-
write_msg(NULL, "Use all tables or a full dump instead.\n");
429+
write_msg(NULL, "Use a full dump instead.\n");
443430
exit(1);
444431
}
445432

@@ -450,13 +437,6 @@ main(int argc, char **argv)
450437
exit(1);
451438
}
452439

453-
if (selectTableName != NULL && selectSchemaName != NULL)
454-
{
455-
write_msg(NULL, "Single table and single schema dumps cannot be used simultaneously.\n");
456-
write_msg(NULL, "Use one option or the other, not both.\n");
457-
exit(1);
458-
}
459-
460440
if (dumpData == true && oids == true)
461441
{
462442
write_msg(NULL, "INSERT (-d, -D) and OID (-o) options cannot be used together.\n");
@@ -676,7 +656,7 @@ help(const char *progname)
676656
printf(_(" -C, --create include commands to create database in dump\n"));
677657
printf(_(" -d, --inserts dump data as INSERT, rather than COPY, commands\n"));
678658
printf(_(" -D, --column-inserts dump data as INSERT commands with column names\n"));
679-
printf(_(" -n, --schema=SCHEMA dump this schema only\n"));
659+
printf(_(" -n, --schema=SCHEMA dump the named schema only\n"));
680660
printf(_(" -o, --oids include OIDs in dump\n"));
681661
printf(_(" -O, --no-owner do not output \\connect commands in plain\n"
682662
" text format\n"));
@@ -685,7 +665,7 @@ help(const char *progname)
685665
printf(_(" -s, --schema-only dump only the schema, no data\n"));
686666
printf(_(" -S, --superuser=NAME specify the superuser user name to use in\n"
687667
" plain text format\n"));
688-
printf(_(" -t, --table=TABLE dump this table only (* for all)\n"));
668+
printf(_(" -t, --table=TABLE dump the named table only\n"));
689669
printf(_(" -x, --no-privileges do not dump privileges (grant/revoke)\n"));
690670
printf(_(" -X use-set-session-authorization, --use-set-session-authorization\n"
691671
" output SET SESSION AUTHORIZATION commands rather\n"
@@ -704,38 +684,6 @@ help(const char *progname)
704684
printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
705685
}
706686

707-
/*
708-
* Accepts an identifier as specified as a command-line argument, and
709-
* converts it into a form acceptable to the PostgreSQL backend. The
710-
* input string is modified in-place.
711-
*/
712-
static void
713-
formatIdentifierArg(char *identifier)
714-
{
715-
/*
716-
* quoted string? Then strip quotes and preserve
717-
* case...
718-
*/
719-
if (identifier[0] == '"')
720-
{
721-
char *endptr;
722-
723-
endptr = identifier + strlen(identifier) - 1;
724-
if (*endptr == '"')
725-
*endptr = '\0';
726-
strcpy(identifier, &identifier[1]);
727-
}
728-
else
729-
{
730-
int i;
731-
732-
/* otherwise, convert identifier name to lowercase... */
733-
for (i = 0; identifier[i]; i++)
734-
if (isupper((unsigned char) identifier[i]))
735-
identifier[i] = tolower((unsigned char) identifier[i]);
736-
}
737-
}
738-
739687
void
740688
exit_nicely(void)
741689
{
@@ -785,12 +733,18 @@ selectDumpableTable(TableInfo *tbinfo)
785733
* tablename has been specified, dump matching table name; else, do
786734
* not dump.
787735
*/
736+
tbinfo->dump = false;
788737
if (tbinfo->relnamespace->dump)
789738
tbinfo->dump = true;
790-
else if (selectTableName != NULL)
791-
tbinfo->dump = (strcmp(tbinfo->relname, selectTableName) == 0);
792-
else
793-
tbinfo->dump = false;
739+
else if (selectTableName != NULL &&
740+
strcmp(tbinfo->relname, selectTableName) == 0)
741+
{
742+
/* If both -s and -t specified, must match both to dump */
743+
if (selectSchemaName == NULL)
744+
tbinfo->dump = true;
745+
else if (strcmp(tbinfo->relnamespace->nspname, selectSchemaName) == 0)
746+
tbinfo->dump = true;
747+
}
794748
}
795749

796750
/*

src/bin/pg_dump/pg_restore.c

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
*
3636
* IDENTIFICATION
37-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.46 2003/06/11 05:13:11 momjian Exp $
37+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.47 2003/06/11 16:29:42 tgl Exp $
3838
*
3939
*-------------------------------------------------------------------------
4040
*/
@@ -72,12 +72,9 @@ int optreset;
7272

7373
/* Forward decls */
7474
static void usage(const char *progname);
75-
static char *_cleanupName(char *name);
76-
static char *_cleanupFuncName(char *name);
7775

7876
typedef struct option optType;
7977

80-
8178
int
8279
main(int argc, char **argv)
8380
{
@@ -220,17 +217,17 @@ main(int argc, char **argv)
220217
case 'P': /* Function */
221218
opts->selTypes = 1;
222219
opts->selFunction = 1;
223-
opts->functionNames = _cleanupFuncName(optarg);
220+
opts->functionNames = strdup(optarg);
224221
break;
225222
case 'I': /* Index */
226223
opts->selTypes = 1;
227224
opts->selIndex = 1;
228-
opts->indexNames = _cleanupName(optarg);
225+
opts->indexNames = strdup(optarg);
229226
break;
230227
case 'T': /* Trigger */
231228
opts->selTypes = 1;
232229
opts->selTrigger = 1;
233-
opts->triggerNames = _cleanupName(optarg);
230+
opts->triggerNames = strdup(optarg);
234231
break;
235232
case 's': /* dump schema only */
236233
opts->schemaOnly = 1;
@@ -242,7 +239,7 @@ main(int argc, char **argv)
242239
case 't': /* Dump data for this table only */
243240
opts->selTypes = 1;
244241
opts->selTable = 1;
245-
opts->tableNames = _cleanupName(optarg);
242+
opts->tableNames = strdup(optarg);
246243
break;
247244

248245
case 'u':
@@ -417,77 +414,3 @@ usage(const char *progname)
417414
printf(_("\nIf no input file name is supplied, then standard input is used.\n\n"));
418415
printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
419416
}
420-
421-
422-
static char *
423-
_cleanupName(char *name)
424-
{
425-
int i;
426-
427-
if (!name || !name[0])
428-
return NULL;
429-
430-
name = strdup(name);
431-
432-
if (name[0] == '"')
433-
{
434-
strcpy(name, &name[1]);
435-
if (name[0] && *(name + strlen(name) - 1) == '"')
436-
*(name + strlen(name) - 1) = '\0';
437-
}
438-
/* otherwise, convert table name to lowercase... */
439-
else
440-
{
441-
for (i = 0; name[i]; i++)
442-
if (isupper((unsigned char) name[i]))
443-
name[i] = tolower((unsigned char) name[i]);
444-
}
445-
return name;
446-
}
447-
448-
449-
static char *
450-
_cleanupFuncName(char *name)
451-
{
452-
int i;
453-
char *ch;
454-
455-
if (!name || !name[0])
456-
return NULL;
457-
458-
name = strdup(name);
459-
460-
if (name[0] == '"')
461-
{
462-
strcpy(name, &name[1]);
463-
if (strchr(name, '"') != NULL)
464-
strcpy(strchr(name, '"'), strchr(name, '"') + 1);
465-
}
466-
/* otherwise, convert function name to lowercase... */
467-
else
468-
{
469-
for (i = 0; name[i]; i++)
470-
if (isupper((unsigned char) name[i]))
471-
name[i] = tolower((unsigned char) name[i]);
472-
}
473-
474-
/* strip out any space before paren */
475-
ch = strchr(name, '(');
476-
while (ch && ch > name && *(ch - 1) == ' ')
477-
{
478-
strcpy(ch - 1, ch);
479-
ch--;
480-
}
481-
482-
/*
483-
* Strip out spaces after commas in parameter list. We can't remove
484-
* all spaces because some types, like 'double precision' have spaces.
485-
*/
486-
if ((ch = strchr(name, '(')) != NULL)
487-
{
488-
while ((ch = strstr(ch, ", ")) != NULL)
489-
strcpy(ch + 1, ch + 2);
490-
}
491-
492-
return name;
493-
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy