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

Commit f7ea4ee

Browse files
author
Neil Conway
committed
Document how to use psql's --single-transaction option to rollback restoring
an SQL dump if an error occurs. Along the way, make some improvements and copy-edits to the surrounding text. Patch from Simon Riggs, additional fixes by Neil Conway.
1 parent 85fca79 commit f7ea4ee

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

doc/src/sgml/backup.sgml

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/backup.sgml,v 2.87 2006/09/19 15:18:41 neilc Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/backup.sgml,v 2.88 2006/09/19 19:04:51 neilc Exp $ -->
22

33
<chapter id="backup">
44
<title>Backup and Restore</title>
@@ -84,7 +84,7 @@ pg_dump <replaceable class="parameter">dbname</replaceable> &gt; <replaceable cl
8484

8585
<important>
8686
<para>
87-
When your database schema relies on OIDs (for instance as foreign
87+
If your database schema relies on OIDs (for instance as foreign
8888
keys) you must instruct <application>pg_dump</> to dump the OIDs
8989
as well. To do this, use the <option>-o</option> command line
9090
option.
@@ -105,30 +105,42 @@ psql <replaceable class="parameter">dbname</replaceable> &lt; <replaceable class
105105
you used as <replaceable class="parameter">outfile</replaceable>
106106
for the <application>pg_dump</> command. The database <replaceable
107107
class="parameter">dbname</replaceable> will not be created by this
108-
command, you must create it yourself from <literal>template0</> before executing
109-
<application>psql</> (e.g., with <literal>createdb -T template0
110-
<replaceable class="parameter">dbname</></literal>).
111-
<application>psql</> supports options similar to <application>pg_dump</>
112-
for controlling the database server location and the user name. See
113-
<xref linkend="app-psql">'s reference page for more information.
108+
command, so you must create it yourself from <literal>template0</>
109+
before executing <application>psql</> (e.g., with
110+
<literal>createdb -T template0 <replaceable
111+
class="parameter">dbname</></literal>). <application>psql</>
112+
supports similar options to <application>pg_dump</> for specifying
113+
the database server to connect to and the user name to use. See
114+
the <xref linkend="app-psql"> reference page for more information.
114115
</para>
115116

116117
<para>
117-
Not only must the target database already exist before starting to
118-
run the restore, but so must all the users who own objects in the
119-
dumped database or were granted permissions on the objects. If they
120-
do not, then the restore will fail to recreate the objects with the
121-
original ownership and/or permissions. (Sometimes this is what you want,
122-
but usually it is not.)
118+
Before restoring a SQL dump, all the users who own objects or were
119+
granted permissions on objects in the dumped database must already
120+
exist. If they do not, then the restore will fail to recreate the
121+
objects with the original ownership and/or permissions.
122+
(Sometimes this is what you want, but usually it is not.)
123123
</para>
124124

125125
<para>
126-
Once restored, it is wise to run <xref linkend="sql-analyze"
127-
endterm="sql-analyze-title"> on each database so the optimizer has
128-
useful statistics. An easy way to do this is to run
129-
<command>vacuumdb -a -z</> to
130-
<command>VACUUM ANALYZE</> all databases; this is equivalent to
131-
running <command>VACUUM ANALYZE</command> manually.
126+
By default, the <application>psql</> script will continue to
127+
execute after an SQL error is encountered. You may wish to use the
128+
following command at the top of the script to alter that
129+
behaviour and have <application>psql</application> exit with an
130+
exit status of 3 if an SQL error occurs:
131+
<programlisting>
132+
\set ON_ERROR_STOP
133+
</programlisting>
134+
Either way, you will only have a partially restored
135+
dump. Alternatively, you can specify that the whole dump should be
136+
restored as a single transaction, so the restore is either fully
137+
completed or fully rolled back. This mode can be specified by
138+
passing the <option>-1</> or <option>--single-transaction</>
139+
command-line options to <application>psql</>. When using this
140+
mode, be aware that even the smallest of errors can rollback a
141+
restore that has already run for many hours. However, that may
142+
still be preferable to manually cleaning up a complex database
143+
after a partially restored dump.
132144
</para>
133145

134146
<para>
@@ -153,8 +165,13 @@ pg_dump -h <replaceable>host1</> <replaceable>dbname</> | psql -h <replaceable>h
153165
</important>
154166

155167
<para>
156-
For advice on how to load large amounts of data into
157-
<productname>PostgreSQL</productname> efficiently, refer to <xref
168+
After restoring a backup, it is wise to run <xref
169+
linkend="sql-analyze" endterm="sql-analyze-title"> on each
170+
database so the query optimizer has useful statistics. An easy way
171+
to do this is to run <command>vacuumdb -a -z</>; this is
172+
equivalent to running <command>VACUUM ANALYZE</> on each database
173+
manually. For more advice on how to load large amounts of data
174+
into <productname>PostgreSQL</> efficiently, refer to <xref
158175
linkend="populate">.
159176
</para>
160177
</sect2>

0 commit comments

Comments
 (0)