mirror of
https://git.postgresql.org/git/postgresql.git
synced 2025-01-18 18:44:06 +08:00
Add psql option:
-1 or --single-transaction Simon Riggs
This commit is contained in:
parent
3884ede663
commit
0f57851e31
doc/src/sgml/ref
src/bin
@ -1,4 +1,4 @@
|
|||||||
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.56 2005/11/01 21:09:50 tgl Exp $ -->
|
<!-- $PostgreSQL: pgsql/doc/src/sgml/ref/pg_restore.sgml,v 1.57 2006/02/12 04:04:32 momjian Exp $ -->
|
||||||
|
|
||||||
<refentry id="APP-PGRESTORE">
|
<refentry id="APP-PGRESTORE">
|
||||||
<refmeta>
|
<refmeta>
|
||||||
@ -448,6 +448,19 @@
|
|||||||
</para>
|
</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><option>-1</option></term>
|
||||||
|
<term><option>--single-transaction</option></term>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
Force the restore to execute as a single transaction. Either all
|
||||||
|
SQL statements complete successfully, or no changes are applied. This
|
||||||
|
option also forces --exit-on-error.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
</variablelist>
|
</variablelist>
|
||||||
</para>
|
</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!--
|
<!--
|
||||||
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.158 2006/02/12 02:54:30 momjian Exp $
|
$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.159 2006/02/12 04:04:32 momjian Exp $
|
||||||
PostgreSQL documentation
|
PostgreSQL documentation
|
||||||
-->
|
-->
|
||||||
|
|
||||||
@ -463,6 +463,18 @@ PostgreSQL documentation
|
|||||||
</listitem>
|
</listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
|
<varlistentry>
|
||||||
|
<term><option>-1</option></term>
|
||||||
|
<term><option>--single-transaction</option></term>
|
||||||
|
<listitem>
|
||||||
|
<para>
|
||||||
|
When psql executes a script with the -f option, this additional option
|
||||||
|
will force the script to execute as a single transaction. Either all
|
||||||
|
SQL statements complete successfully, or no changes are applied.
|
||||||
|
</para>
|
||||||
|
</listitem>
|
||||||
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><option>-?</></term>
|
<term><option>-?</></term>
|
||||||
<term><option>--help</></term>
|
<term><option>--help</></term>
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.37 2005/10/15 02:49:38 momjian Exp $
|
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup.h,v 1.38 2006/02/12 04:04:32 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -115,6 +115,8 @@ typedef struct _restoreOptions
|
|||||||
|
|
||||||
int suppressDumpWarnings; /* Suppress output of WARNING entries
|
int suppressDumpWarnings; /* Suppress output of WARNING entries
|
||||||
* to stderr */
|
* to stderr */
|
||||||
|
bool single_txn;
|
||||||
|
|
||||||
} RestoreOptions;
|
} RestoreOptions;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.121 2006/02/09 20:52:13 momjian Exp $
|
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.122 2006/02/12 04:04:32 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -217,6 +217,9 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
|
|||||||
|
|
||||||
AH->stage = STAGE_PROCESSING;
|
AH->stage = STAGE_PROCESSING;
|
||||||
|
|
||||||
|
if (ropt->single_txn)
|
||||||
|
ahprintf(AH, "BEGIN;\n\n");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Drop the items at the start, in reverse order
|
* Drop the items at the start, in reverse order
|
||||||
*/
|
*/
|
||||||
@ -370,6 +373,9 @@ RestoreArchive(Archive *AHX, RestoreOptions *ropt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ropt->single_txn)
|
||||||
|
ahprintf(AH, "COMMIT;\n\n");
|
||||||
|
|
||||||
if (AH->public.verbose)
|
if (AH->public.verbose)
|
||||||
dumpTimestamp(AH, "Completed on", time(NULL));
|
dumpTimestamp(AH, "Completed on", time(NULL));
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.73 2005/10/15 02:49:39 momjian Exp $
|
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.74 2006/02/12 04:04:32 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -111,6 +111,7 @@ main(int argc, char **argv)
|
|||||||
{"use-list", 1, NULL, 'L'},
|
{"use-list", 1, NULL, 'L'},
|
||||||
{"username", 1, NULL, 'U'},
|
{"username", 1, NULL, 'U'},
|
||||||
{"verbose", 0, NULL, 'v'},
|
{"verbose", 0, NULL, 'v'},
|
||||||
|
{"single-transaction", 0, NULL, '1'},
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* the following options don't have an equivalent short option letter,
|
* the following options don't have an equivalent short option letter,
|
||||||
@ -142,7 +143,7 @@ main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:uU:vWxX:",
|
while ((c = getopt_long(argc, argv, "acCd:ef:F:h:iI:lL:n:Op:P:RsS:t:T:uU:vWxX:1",
|
||||||
cmdopts, NULL)) != -1)
|
cmdopts, NULL)) != -1)
|
||||||
{
|
{
|
||||||
switch (c)
|
switch (c)
|
||||||
@ -185,9 +186,15 @@ main(int argc, char **argv)
|
|||||||
opts->tocFile = strdup(optarg);
|
opts->tocFile = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'n': /* Dump data for this schema only */
|
||||||
|
opts->selTypes = 1;
|
||||||
|
opts->schemaNames = strdup(optarg);
|
||||||
|
break;
|
||||||
|
|
||||||
case 'O':
|
case 'O':
|
||||||
opts->noOwner = 1;
|
opts->noOwner = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
if (strlen(optarg) != 0)
|
if (strlen(optarg) != 0)
|
||||||
opts->pgport = strdup(optarg);
|
opts->pgport = strdup(optarg);
|
||||||
@ -223,11 +230,6 @@ main(int argc, char **argv)
|
|||||||
opts->tableNames = strdup(optarg);
|
opts->tableNames = strdup(optarg);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'n': /* Dump data for this schema only */
|
|
||||||
opts->selTypes = 1;
|
|
||||||
opts->schemaNames = strdup(optarg);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'u':
|
case 'u':
|
||||||
opts->requirePassword = true;
|
opts->requirePassword = true;
|
||||||
opts->username = simple_prompt("User name: ", 100, true);
|
opts->username = simple_prompt("User name: ", 100, true);
|
||||||
@ -268,6 +270,11 @@ main(int argc, char **argv)
|
|||||||
case 0:
|
case 0:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '1': /* Restore data in a single transaction */
|
||||||
|
opts->single_txn = true;
|
||||||
|
opts->exit_on_error = true;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
|
||||||
exit(1);
|
exit(1);
|
||||||
@ -394,6 +401,7 @@ usage(const char *progname)
|
|||||||
printf(_(" -X use-set-session-authorization, --use-set-session-authorization\n"
|
printf(_(" -X use-set-session-authorization, --use-set-session-authorization\n"
|
||||||
" use SESSION AUTHORIZATION commands instead of\n"
|
" use SESSION AUTHORIZATION commands instead of\n"
|
||||||
" OWNER TO commands\n"));
|
" OWNER TO commands\n"));
|
||||||
|
printf(_(" -1, --single-transaction restore as a single transaction\n"));
|
||||||
|
|
||||||
printf(_("\nConnection options:\n"));
|
printf(_("\nConnection options:\n"));
|
||||||
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
|
printf(_(" -h, --host=HOSTNAME database server host or socket directory\n"));
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.160 2006/02/12 03:22:19 momjian Exp $
|
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.161 2006/02/12 04:04:32 momjian Exp $
|
||||||
*/
|
*/
|
||||||
#include "postgres_fe.h"
|
#include "postgres_fe.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
@ -563,7 +563,7 @@ exec_command(const char *cmd,
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
expand_tilde(&fname);
|
expand_tilde(&fname);
|
||||||
success = (process_file(fname) == EXIT_SUCCESS);
|
success = (process_file(fname, false) == EXIT_SUCCESS);
|
||||||
free(fname);
|
free(fname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1435,11 +1435,12 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
|
|||||||
* MainLoop() error code.
|
* MainLoop() error code.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
process_file(char *filename)
|
process_file(char *filename, bool single_txn)
|
||||||
{
|
{
|
||||||
FILE *fd;
|
FILE *fd;
|
||||||
int result;
|
int result;
|
||||||
char *oldfilename;
|
char *oldfilename;
|
||||||
|
PGresult *res;
|
||||||
|
|
||||||
if (!filename)
|
if (!filename)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
@ -1455,7 +1456,13 @@ process_file(char *filename)
|
|||||||
|
|
||||||
oldfilename = pset.inputfile;
|
oldfilename = pset.inputfile;
|
||||||
pset.inputfile = filename;
|
pset.inputfile = filename;
|
||||||
|
|
||||||
|
if (single_txn)
|
||||||
|
res = PSQLexec("BEGIN", false);
|
||||||
result = MainLoop(fd);
|
result = MainLoop(fd);
|
||||||
|
if (single_txn)
|
||||||
|
res = PSQLexec("COMMIT", false);
|
||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
pset.inputfile = oldfilename;
|
pset.inputfile = oldfilename;
|
||||||
return result;
|
return result;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/bin/psql/command.h,v 1.23 2005/12/18 02:17:16 petere Exp $
|
* $PostgreSQL: pgsql/src/bin/psql/command.h,v 1.24 2006/02/12 04:04:32 momjian Exp $
|
||||||
*/
|
*/
|
||||||
#ifndef COMMAND_H
|
#ifndef COMMAND_H
|
||||||
#define COMMAND_H
|
#define COMMAND_H
|
||||||
@ -28,7 +28,7 @@ typedef enum _backslashResult
|
|||||||
extern backslashResult HandleSlashCmds(PsqlScanState scan_state,
|
extern backslashResult HandleSlashCmds(PsqlScanState scan_state,
|
||||||
PQExpBuffer query_buf);
|
PQExpBuffer query_buf);
|
||||||
|
|
||||||
extern int process_file(char *filename);
|
extern int process_file(char *filename, bool single_txn);
|
||||||
|
|
||||||
extern bool do_pset(const char *param,
|
extern bool do_pset(const char *param,
|
||||||
const char *value,
|
const char *value,
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.108 2006/02/12 02:54:30 momjian Exp $
|
* $PostgreSQL: pgsql/src/bin/psql/help.c,v 1.109 2006/02/12 04:04:32 momjian Exp $
|
||||||
*/
|
*/
|
||||||
#include "postgres_fe.h"
|
#include "postgres_fe.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
@ -93,6 +93,7 @@ usage(void)
|
|||||||
printf(_(" -d DBNAME specify database name to connect to (default: \"%s\")\n"), env);
|
printf(_(" -d DBNAME specify database name to connect to (default: \"%s\")\n"), env);
|
||||||
puts(_(" -c COMMAND run only single command (SQL or internal) and exit"));
|
puts(_(" -c COMMAND run only single command (SQL or internal) and exit"));
|
||||||
puts(_(" -f FILENAME execute commands from file, then exit"));
|
puts(_(" -f FILENAME execute commands from file, then exit"));
|
||||||
|
puts(_(" -1 (numeral) execute command file as a single transaction"));
|
||||||
puts(_(" -l list available databases, then exit"));
|
puts(_(" -l list available databases, then exit"));
|
||||||
puts(_(" -v NAME=VALUE set psql variable NAME to VALUE"));
|
puts(_(" -v NAME=VALUE set psql variable NAME to VALUE"));
|
||||||
puts(_(" -X do not read startup file (~/.psqlrc)"));
|
puts(_(" -X do not read startup file (~/.psqlrc)"));
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
|
||||||
*
|
*
|
||||||
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.129 2005/12/18 02:17:16 petere Exp $
|
* $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.130 2006/02/12 04:04:32 momjian Exp $
|
||||||
*/
|
*/
|
||||||
#include "postgres_fe.h"
|
#include "postgres_fe.h"
|
||||||
|
|
||||||
@ -76,6 +76,7 @@ struct adhoc_opts
|
|||||||
char *action_string;
|
char *action_string;
|
||||||
bool no_readline;
|
bool no_readline;
|
||||||
bool no_psqlrc;
|
bool no_psqlrc;
|
||||||
|
bool single_txn;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int parse_version(const char *versionString);
|
static int parse_version(const char *versionString);
|
||||||
@ -268,7 +269,7 @@ main(int argc, char *argv[])
|
|||||||
if (!options.no_psqlrc)
|
if (!options.no_psqlrc)
|
||||||
process_psqlrc(argv[0]);
|
process_psqlrc(argv[0]);
|
||||||
|
|
||||||
successResult = process_file(options.action_string);
|
successResult = process_file(options.action_string, options.single_txn);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -425,6 +426,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
|
|||||||
{"list", no_argument, NULL, 'l'},
|
{"list", no_argument, NULL, 'l'},
|
||||||
{"log-file", required_argument, NULL, 'L'},
|
{"log-file", required_argument, NULL, 'L'},
|
||||||
{"no-readline", no_argument, NULL, 'n'},
|
{"no-readline", no_argument, NULL, 'n'},
|
||||||
|
{"single-transaction", no_argument, NULL, '1'},
|
||||||
{"output", required_argument, NULL, 'o'},
|
{"output", required_argument, NULL, 'o'},
|
||||||
{"port", required_argument, NULL, 'p'},
|
{"port", required_argument, NULL, 'p'},
|
||||||
{"pset", required_argument, NULL, 'P'},
|
{"pset", required_argument, NULL, 'P'},
|
||||||
@ -453,7 +455,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
|
|||||||
|
|
||||||
memset(options, 0, sizeof *options);
|
memset(options, 0, sizeof *options);
|
||||||
|
|
||||||
while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?",
|
while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:uU:v:VWxX?1",
|
||||||
long_options, &optindex)) != -1)
|
long_options, &optindex)) != -1)
|
||||||
{
|
{
|
||||||
switch (c)
|
switch (c)
|
||||||
@ -606,6 +608,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
|
|||||||
case 'X':
|
case 'X':
|
||||||
options->no_psqlrc = true;
|
options->no_psqlrc = true;
|
||||||
break;
|
break;
|
||||||
|
case '1':
|
||||||
|
options->single_txn = true;
|
||||||
|
break;
|
||||||
case '?':
|
case '?':
|
||||||
/* Actual help option given */
|
/* Actual help option given */
|
||||||
if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0)
|
if (strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0)
|
||||||
@ -690,9 +695,9 @@ process_psqlrc_file(char *filename)
|
|||||||
sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
|
sprintf(psqlrc, "%s-%s", filename, PG_VERSION);
|
||||||
|
|
||||||
if (access(psqlrc, R_OK) == 0)
|
if (access(psqlrc, R_OK) == 0)
|
||||||
(void) process_file(psqlrc);
|
(void) process_file(psqlrc, false);
|
||||||
else if (access(filename, R_OK) == 0)
|
else if (access(filename, R_OK) == 0)
|
||||||
(void) process_file(filename);
|
(void) process_file(filename, false);
|
||||||
free(psqlrc);
|
free(psqlrc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user