doc: ALTER DEFAULT PRIVILEGES does not affect inherited roles
[pgsql.git] / doc / src / sgml / ref / pgupgrade.sgml
blob46e8a0b7467e17c7bcd964e64681d6df2b1c9eee
1 <!--
2 doc/src/sgml/ref/pgupgrade.sgml
3 PostgreSQL documentation
4 -->
6 <refentry id="pgupgrade">
7 <indexterm zone="pgupgrade">
8 <primary>pg_upgrade</primary>
9 </indexterm>
11 <refmeta>
12 <refentrytitle><application>pg_upgrade</application></refentrytitle>
13 <manvolnum>1</manvolnum>
14 <refmiscinfo>Application</refmiscinfo>
15 </refmeta>
17 <refnamediv>
18 <refname>pg_upgrade</refname>
19 <refpurpose>upgrade a <productname>PostgreSQL</productname> server instance</refpurpose>
20 </refnamediv>
22 <refsynopsisdiv>
23 <cmdsynopsis>
24 <command>pg_upgrade</command>
25 <arg choice="plain"><option>-b</option></arg>
26 <arg choice="plain"><replaceable>oldbindir</replaceable></arg>
27 <arg choice="opt"><option>-B</option> <replaceable>newbindir</replaceable></arg>
28 <arg choice="plain"><option>-d</option></arg>
29 <arg choice="plain"><replaceable>oldconfigdir</replaceable></arg>
30 <arg choice="plain"><option>-D</option></arg>
31 <arg choice="plain"><replaceable>newconfigdir</replaceable></arg>
32 <arg rep="repeat"><replaceable>option</replaceable></arg>
33 </cmdsynopsis>
34 </refsynopsisdiv>
36 <refsect1>
37 <title>Description</title>
39 <para>
40 <application>pg_upgrade</application> (formerly called <application>pg_migrator</application>) allows data
41 stored in <productname>PostgreSQL</productname> data files to be upgraded to a later <productname>PostgreSQL</productname>
42 major version without the data dump/restore typically required for
43 major version upgrades, e.g., from 12.14 to 13.10 or from 14.9 to 15.5.
44 It is not required for minor version upgrades, e.g., from 12.7 to 12.8
45 or from 14.1 to 14.5.
46 </para>
48 <para>
49 Major PostgreSQL releases regularly add new features that often
50 change the layout of the system tables, but the internal data storage
51 format rarely changes. <application>pg_upgrade</application> uses this fact
52 to perform rapid upgrades by creating new system tables and simply
53 reusing the old user data files. If a future major release ever
54 changes the data storage format in a way that makes the old data
55 format unreadable, <application>pg_upgrade</application> will not be usable
56 for such upgrades. (The community will attempt to avoid such
57 situations.)
58 </para>
60 <para>
61 <application>pg_upgrade</application> does its best to
62 make sure the old and new clusters are binary-compatible, e.g., by
63 checking for compatible compile-time settings, including 32/64-bit
64 binaries. It is important that
65 any external modules are also binary compatible, though this cannot
66 be checked by <application>pg_upgrade</application>.
67 </para>
69 <para>
70 pg_upgrade supports upgrades from 9.2.X and later to the current
71 major release of <productname>PostgreSQL</productname>, including snapshot and beta releases.
72 </para>
73 </refsect1>
75 <refsect1>
76 <title>Options</title>
78 <para>
79 <application>pg_upgrade</application> accepts the following command-line arguments:
81 <variablelist>
83 <varlistentry>
84 <term><option>-b</option> <replaceable>bindir</replaceable></term>
85 <term><option>--old-bindir=</option><replaceable>bindir</replaceable></term>
86 <listitem><para>the old PostgreSQL executable directory;
87 environment variable <envar>PGBINOLD</envar></para></listitem>
88 </varlistentry>
90 <varlistentry>
91 <term><option>-B</option> <replaceable>bindir</replaceable></term>
92 <term><option>--new-bindir=</option><replaceable>bindir</replaceable></term>
93 <listitem><para>the new PostgreSQL executable directory;
94 default is the directory where <application>pg_upgrade</application> resides;
95 environment variable <envar>PGBINNEW</envar></para></listitem>
96 </varlistentry>
98 <varlistentry>
99 <term><option>-c</option></term>
100 <term><option>--check</option></term>
101 <listitem><para>check clusters only, don't change any data</para></listitem>
102 </varlistentry>
104 <varlistentry>
105 <term><option>-d</option> <replaceable>configdir</replaceable></term>
106 <term><option>--old-datadir=</option><replaceable>configdir</replaceable></term>
107 <listitem><para>the old database cluster configuration directory; environment
108 variable <envar>PGDATAOLD</envar></para></listitem>
109 </varlistentry>
111 <varlistentry>
112 <term><option>-D</option> <replaceable>configdir</replaceable></term>
113 <term><option>--new-datadir=</option><replaceable>configdir</replaceable></term>
114 <listitem><para>the new database cluster configuration directory; environment
115 variable <envar>PGDATANEW</envar></para></listitem>
116 </varlistentry>
118 <varlistentry>
119 <term><option>-j <replaceable class="parameter">njobs</replaceable></option></term>
120 <term><option>--jobs=<replaceable class="parameter">njobs</replaceable></option></term>
121 <listitem><para>number of simultaneous processes or threads to use
122 </para></listitem>
123 </varlistentry>
125 <varlistentry>
126 <term><option>-k</option></term>
127 <term><option>--link</option></term>
128 <listitem><para>use hard links instead of copying files to the new
129 cluster</para></listitem>
130 </varlistentry>
132 <varlistentry>
133 <term><option>-N</option></term>
134 <term><option>--no-sync</option></term>
135 <listitem>
136 <para>
137 By default, <command>pg_upgrade</command> will wait for all files
138 of the upgraded cluster to be written safely to disk. This option
139 causes <command>pg_upgrade</command> to return without waiting, which
140 is faster, but means that a subsequent operating system crash can leave
141 the data directory corrupt. Generally, this option is
142 useful for testing but should not be used on a production
143 installation.
144 </para>
145 </listitem>
146 </varlistentry>
148 <varlistentry>
149 <term><option>-o</option> <replaceable class="parameter">options</replaceable></term>
150 <term><option>--old-options</option> <replaceable class="parameter">options</replaceable></term>
151 <listitem><para>options to be passed directly to the
152 old <command>postgres</command> command; multiple
153 option invocations are appended</para></listitem>
154 </varlistentry>
156 <varlistentry>
157 <term><option>-O</option> <replaceable class="parameter">options</replaceable></term>
158 <term><option>--new-options</option> <replaceable class="parameter">options</replaceable></term>
159 <listitem><para>options to be passed directly to the
160 new <command>postgres</command> command; multiple
161 option invocations are appended</para></listitem>
162 </varlistentry>
164 <varlistentry>
165 <term><option>-p</option> <replaceable>port</replaceable></term>
166 <term><option>--old-port=</option><replaceable>port</replaceable></term>
167 <listitem><para>the old cluster port number; environment
168 variable <envar>PGPORTOLD</envar></para></listitem>
169 </varlistentry>
171 <varlistentry>
172 <term><option>-P</option> <replaceable>port</replaceable></term>
173 <term><option>--new-port=</option><replaceable>port</replaceable></term>
174 <listitem><para>the new cluster port number; environment
175 variable <envar>PGPORTNEW</envar></para></listitem>
176 </varlistentry>
178 <varlistentry>
179 <term><option>-r</option></term>
180 <term><option>--retain</option></term>
181 <listitem><para>retain SQL and log files even after successful completion
182 </para></listitem>
183 </varlistentry>
185 <varlistentry>
186 <term><option>-s</option> <replaceable>dir</replaceable></term>
187 <term><option>--socketdir=</option><replaceable>dir</replaceable></term>
188 <listitem><para>directory to use for postmaster sockets during upgrade;
189 default is current working directory; environment
190 variable <envar>PGSOCKETDIR</envar></para></listitem>
191 </varlistentry>
193 <varlistentry>
194 <term><option>--sync-method=</option><replaceable>method</replaceable></term>
195 <listitem>
196 <para>
197 When set to <literal>fsync</literal>, which is the default,
198 <command>pg_upgrade</command> will recursively open and synchronize all
199 files in the upgraded cluster's data directory. The search for files
200 will follow symbolic links for the WAL directory and each configured
201 tablespace.
202 </para>
203 <para>
204 On Linux, <literal>syncfs</literal> may be used instead to ask the
205 operating system to synchronize the whole file systems that contain the
206 upgraded cluster's data directory, its WAL files, and each tablespace.
207 See <xref linkend="syncfs"/> for more information about using
208 <function>syncfs()</function>.
209 </para>
210 <para>
211 This option has no effect when <option>--no-sync</option> is used.
212 </para>
213 </listitem>
214 </varlistentry>
216 <varlistentry>
217 <term><option>-U</option> <replaceable>username</replaceable></term>
218 <term><option>--username=</option><replaceable>username</replaceable></term>
219 <listitem><para>cluster's install user name; environment
220 variable <envar>PGUSER</envar></para></listitem>
221 </varlistentry>
223 <varlistentry>
224 <term><option>-v</option></term>
225 <term><option>--verbose</option></term>
226 <listitem><para>enable verbose internal logging</para></listitem>
227 </varlistentry>
229 <varlistentry>
230 <term><option>-V</option></term>
231 <term><option>--version</option></term>
232 <listitem><para>display version information, then exit</para></listitem>
233 </varlistentry>
235 <varlistentry>
236 <term><option>--clone</option></term>
237 <listitem>
238 <para>
239 Use efficient file cloning (also known as <quote>reflinks</quote> on
240 some systems) instead of copying files to the new cluster. This can
241 result in near-instantaneous copying of the data files, giving the
242 speed advantages of <option>-k</option>/<option>--link</option> while
243 leaving the old cluster untouched.
244 </para>
246 <para>
247 File cloning is only supported on some operating systems and file
248 systems. If it is selected but not supported, the
249 <application>pg_upgrade</application> run will error. At present, it
250 is supported on Linux (kernel 4.5 or later) with Btrfs and XFS (on
251 file systems created with reflink support), and on macOS with APFS.
252 </para>
253 </listitem>
254 </varlistentry>
256 <varlistentry>
257 <term><option>--copy</option></term>
258 <listitem>
259 <para>
260 Copy files to the new cluster. This is the default. (See also
261 <option>--link</option> and <option>--clone</option>.)
262 </para>
263 </listitem>
264 </varlistentry>
266 <varlistentry>
267 <term><option>-?</option></term>
268 <term><option>--help</option></term>
269 <listitem><para>show help, then exit</para></listitem>
270 </varlistentry>
272 </variablelist>
273 </para>
275 </refsect1>
277 <refsect1>
278 <title>Usage</title>
280 <para>
281 These are the steps to perform an upgrade
282 with <application>pg_upgrade</application>:
283 </para>
285 <procedure>
286 <step performance="optional">
287 <title>Optionally move the old cluster</title>
289 <para>
290 If you are using a version-specific installation directory, e.g.,
291 <filename>/opt/PostgreSQL/&majorversion;</filename>, you do not need to move the old cluster. The
292 graphical installers all use version-specific installation directories.
293 </para>
295 <para>
296 If your installation directory is not version-specific, e.g.,
297 <filename>/usr/local/pgsql</filename>, it is necessary to move the current PostgreSQL install
298 directory so it does not interfere with the new <productname>PostgreSQL</productname> installation.
299 Once the current <productname>PostgreSQL</productname> server is shut down, it is safe to rename the
300 PostgreSQL installation directory; assuming the old directory is
301 <filename>/usr/local/pgsql</filename>, you can do:
303 <programlisting>
304 mv /usr/local/pgsql /usr/local/pgsql.old
305 </programlisting>
306 to rename the directory.
307 </para>
308 </step>
310 <step>
311 <title>For source installs, build the new version</title>
313 <para>
314 Build the new PostgreSQL source with <command>configure</command> flags that are compatible
315 with the old cluster. <application>pg_upgrade</application> will check <command>pg_controldata</command> to make
316 sure all settings are compatible before starting the upgrade.
317 </para>
318 </step>
320 <step>
321 <title>Install the new PostgreSQL binaries</title>
323 <para>
324 Install the new server's binaries and support
325 files. <application>pg_upgrade</application> is included in a default installation.
326 </para>
328 <para>
329 For source installs, if you wish to install the new server in a custom
330 location, use the <literal>prefix</literal> variable:
332 <programlisting>
333 make prefix=/usr/local/pgsql.new install
334 </programlisting></para>
335 </step>
337 <step>
338 <title>Initialize the new PostgreSQL cluster</title>
340 <para>
341 Initialize the new cluster using <command>initdb</command>.
342 Again, use compatible <command>initdb</command>
343 flags that match the old cluster. Many
344 prebuilt installers do this step automatically. There is no need to
345 start the new cluster.
346 </para>
347 </step>
349 <step>
350 <title>Install extension shared object files</title>
352 <para>
353 Many extensions and custom modules, whether from
354 <filename>contrib</filename> or another source, use shared object
355 files (or DLLs), e.g., <filename>pgcrypto.so</filename>. If the old
356 cluster used these, shared object files matching the new server binary
357 must be installed in the new cluster, usually via operating system
358 commands. Do not load the schema definitions, e.g., <command>CREATE
359 EXTENSION pgcrypto</command>, because these will be duplicated from
360 the old cluster. If extension updates are available,
361 <application>pg_upgrade</application> will report this and create
362 a script that can be run later to update them.
363 </para>
364 </step>
366 <step>
367 <title>Copy custom full-text search files</title>
369 <para>
370 Copy any custom full text search files (dictionary, synonym,
371 thesaurus, stop words) from the old to the new cluster.
372 </para>
373 </step>
375 <step>
376 <title>Adjust authentication</title>
378 <para>
379 <command>pg_upgrade</command> will connect to the old and new servers several
380 times, so you might want to set authentication to <literal>peer</literal>
381 in <filename>pg_hba.conf</filename> or use a <filename>~/.pgpass</filename> file
382 (see <xref linkend="libpq-pgpass"/>).
383 </para>
384 </step>
386 <step>
387 <title>Prepare for publisher upgrades</title>
389 <para>
390 <application>pg_upgrade</application> attempts to migrate logical
391 slots. This helps avoid the need for manually defining the same
392 logical slots on the new publisher. Migration of logical slots is
393 only supported when the old cluster is version 17.0 or later.
394 Logical slots on clusters before version 17.0 will silently be
395 ignored.
396 </para>
398 <para>
399 Before you start upgrading the publisher cluster, ensure that the
400 subscription is temporarily disabled, by executing
401 <link linkend="sql-altersubscription"><command>ALTER SUBSCRIPTION ... DISABLE</command></link>.
402 Re-enable the subscription after the upgrade.
403 </para>
405 <para>
406 There are some prerequisites for <application>pg_upgrade</application> to
407 be able to upgrade the logical slots. If these are not met an error
408 will be reported.
409 </para>
411 <itemizedlist>
412 <listitem>
413 <para>
414 The new cluster must have
415 <link linkend="guc-wal-level"><varname>wal_level</varname></link> as
416 <literal>logical</literal>.
417 </para>
418 </listitem>
419 <listitem>
420 <para>
421 The new cluster must have
422 <link linkend="guc-max-replication-slots"><varname>max_replication_slots</varname></link>
423 configured to a value greater than or equal to the number of slots
424 present in the old cluster.
425 </para>
426 </listitem>
427 <listitem>
428 <para>
429 The output plugins referenced by the slots on the old cluster must be
430 installed in the new PostgreSQL executable directory.
431 </para>
432 </listitem>
433 <listitem>
434 <para>
435 The old cluster has replicated all the transactions and logical decoding
436 messages to subscribers.
437 </para>
438 </listitem>
439 <listitem>
440 <para>
441 All slots on the old cluster must be usable, i.e., there are no slots
442 whose
443 <link linkend="view-pg-replication-slots">pg_replication_slots</link>.<structfield>conflicting</structfield>
444 is <literal>true</literal>.
445 </para>
446 </listitem>
447 <listitem>
448 <para>
449 The new cluster must not have permanent logical slots, i.e.,
450 there must be no slots where
451 <link linkend="view-pg-replication-slots">pg_replication_slots</link>.<structfield>temporary</structfield>
452 is <literal>false</literal>.
453 </para>
454 </listitem>
455 </itemizedlist>
457 </step>
459 <step>
460 <title>Stop both servers</title>
462 <para>
463 Make sure both database servers are stopped using, on Unix, e.g.:
465 <programlisting>
466 pg_ctl -D /opt/PostgreSQL/12 stop
467 pg_ctl -D /opt/PostgreSQL/&majorversion; stop
468 </programlisting>
470 or on Windows, using the proper service names:
472 <programlisting>
473 NET STOP postgresql-12
474 NET STOP postgresql-&majorversion;
475 </programlisting>
476 </para>
478 <para>
479 Streaming replication and log-shipping standby servers must be
480 running during this shutdown so they receive all changes.
481 </para>
482 </step>
484 <step>
485 <title>Prepare for standby server upgrades</title>
487 <para>
488 If you are upgrading standby servers using methods outlined in section <xref
489 linkend="pgupgrade-step-replicas"/>, verify that the old standby
490 servers are caught up by running <application>pg_controldata</application>
491 against the old primary and standby clusters. Verify that the
492 <quote>Latest checkpoint location</quote> values match in all clusters.
493 Also, make sure <varname>wal_level</varname> is not set to
494 <literal>minimal</literal> in the <filename>postgresql.conf</filename> file on the
495 new primary cluster.
496 </para>
497 </step>
499 <step>
500 <title>Run <application>pg_upgrade</application></title>
502 <para>
503 Always run the <application>pg_upgrade</application> binary of the new server, not the old one.
504 <application>pg_upgrade</application> requires the specification of the old and new cluster's
505 data and executable (<filename>bin</filename>) directories. You can also specify
506 user and port values, and whether you want the data files linked or cloned
507 instead of the default copy behavior.
508 </para>
510 <para>
511 If you use link mode, the upgrade will be much faster (no file
512 copying) and use less disk space, but you will not be able to access
513 your old cluster
514 once you start the new cluster after the upgrade. Link mode also
515 requires that the old and new cluster data directories be in the
516 same file system. (Tablespaces and <filename>pg_wal</filename> can be on
517 different file systems.)
518 Clone mode provides the same speed and disk space advantages but
519 does not cause the old cluster to be unusable once the new cluster
520 is started. Clone mode also requires that the old and new data
521 directories be in the same file system. This mode is only available
522 on certain operating systems and file systems.
523 </para>
525 <para>
526 The <option>--jobs</option> option allows multiple CPU cores to be used
527 for copying/linking of files and to dump and restore database schemas
528 in parallel; a good place to start is the maximum of the number of
529 CPU cores and tablespaces. This option can dramatically reduce the
530 time to upgrade a multi-database server running on a multiprocessor
531 machine.
532 </para>
534 <para>
535 For Windows users, you must be logged into an administrative account, and
536 then start a shell as the <literal>postgres</literal> user and set the proper path:
538 <programlisting>
539 RUNAS /USER:postgres "CMD.EXE"
540 SET PATH=%PATH%;C:\Program Files\PostgreSQL\&majorversion;\bin;
541 </programlisting>
543 and then run <application>pg_upgrade</application> with quoted directories, e.g.:
545 <programlisting>
546 pg_upgrade.exe
547 --old-datadir "C:/Program Files/PostgreSQL/12/data"
548 --new-datadir "C:/Program Files/PostgreSQL/&majorversion;/data"
549 --old-bindir "C:/Program Files/PostgreSQL/12/bin"
550 --new-bindir "C:/Program Files/PostgreSQL/&majorversion;/bin"
551 </programlisting>
553 Once started, <command>pg_upgrade</command> will verify the two clusters are compatible
554 and then do the upgrade. You can use <command>pg_upgrade --check</command>
555 to perform only the checks, even if the old server is still
556 running. <command>pg_upgrade --check</command> will also outline any
557 manual adjustments you will need to make after the upgrade. If you
558 are going to be using link or clone mode, you should use the option
559 <option>--link</option> or <option>--clone</option> with
560 <option>--check</option> to enable mode-specific checks.
561 <command>pg_upgrade</command> requires write permission in the current directory.
562 </para>
564 <para>
565 Obviously, no one should be accessing the clusters during the
566 upgrade. <application>pg_upgrade</application> defaults to running servers
567 on port 50432 to avoid unintended client connections.
568 You can use the same port number for both clusters when doing an
569 upgrade because the old and new clusters will not be running at the
570 same time. However, when checking an old running server, the old
571 and new port numbers must be different.
572 </para>
574 <para>
575 If an error occurs while restoring the database schema, <command>pg_upgrade</command> will
576 exit and you will have to revert to the old cluster as outlined in <xref linkend="pgupgrade-step-revert"/>
577 below. To try <command>pg_upgrade</command> again, you will need to modify the old
578 cluster so the pg_upgrade schema restore succeeds. If the problem is a
579 <filename>contrib</filename> module, you might need to uninstall the <filename>contrib</filename> module from
580 the old cluster and install it in the new cluster after the upgrade,
581 assuming the module is not being used to store user data.
582 </para>
583 </step>
585 <step id="pgupgrade-step-replicas">
586 <title>Upgrade streaming replication and log-shipping standby servers</title>
588 <para>
589 If you used link mode and have Streaming Replication (see <xref
590 linkend="streaming-replication"/>) or Log-Shipping (see <xref
591 linkend="warm-standby"/>) standby servers, you can follow these steps to
592 quickly upgrade them. You will not be running <application>pg_upgrade</application> on
593 the standby servers, but rather <application>rsync</application> on the primary.
594 Do not start any servers yet.
595 </para>
597 <para>
598 If you did <emphasis>not</emphasis> use link mode, do not have or do not
599 want to use <application>rsync</application>, or want an easier solution, skip
600 the instructions in this section and simply recreate the standby
601 servers once <application>pg_upgrade</application> completes and the new primary
602 is running.
603 </para>
605 <procedure>
607 <step>
608 <title>Install the new PostgreSQL binaries on standby servers</title>
610 <para>
611 Make sure the new binaries and support files are installed on all
612 standby servers.
613 </para>
614 </step>
616 <step>
617 <title>Make sure the new standby data directories do <emphasis>not</emphasis> exist</title>
619 <para>
620 Make sure the new standby data directories do <emphasis>not</emphasis>
621 exist or are empty. If <application>initdb</application> was run, delete
622 the standby servers' new data directories.
623 </para>
624 </step>
626 <step>
627 <title>Install extension shared object files</title>
629 <para>
630 Install the same extension shared object files on the new standbys
631 that you installed in the new primary cluster.
632 </para>
633 </step>
635 <step>
636 <title>Stop standby servers</title>
638 <para>
639 If the standby servers are still running, stop them now using the
640 above instructions.
641 </para>
642 </step>
644 <step>
645 <title>Save configuration files</title>
647 <para>
648 Save any configuration files from the old standbys' configuration
649 directories you need to keep, e.g., <filename>postgresql.conf</filename>
650 (and any files included by it), <filename>postgresql.auto.conf</filename>,
651 <literal>pg_hba.conf</literal>, because these will be overwritten
652 or removed in the next step.
653 </para>
654 </step>
656 <step>
657 <title>Run <application>rsync</application></title>
659 <para>
660 When using link mode, standby servers can be quickly upgraded using
661 <application>rsync</application>. To accomplish this, from a directory on
662 the primary server that is above the old and new database cluster
663 directories, run this on the <emphasis>primary</emphasis> for each standby
664 server:
666 <programlisting>
667 rsync --archive --delete --hard-links --size-only --no-inc-recursive old_cluster new_cluster remote_dir
668 </programlisting>
670 where <option>old_cluster</option> and <option>new_cluster</option> are relative
671 to the current directory on the primary, and <option>remote_dir</option>
672 is <emphasis>above</emphasis> the old and new cluster directories
673 on the standby. The directory structure under the specified
674 directories on the primary and standbys must match. Consult the
675 <application>rsync</application> manual page for details on specifying the
676 remote directory, e.g.,
678 <programlisting>
679 rsync --archive --delete --hard-links --size-only --no-inc-recursive /opt/PostgreSQL/12 \
680 /opt/PostgreSQL/&majorversion; standby.example.com:/opt/PostgreSQL
681 </programlisting>
683 You can verify what the command will do using
684 <application>rsync</application>'s <option>--dry-run</option> option. While
685 <application>rsync</application> must be run on the primary for at least one
686 standby, it is possible to run <application>rsync</application> on an upgraded
687 standby to upgrade other standbys, as long as the upgraded standby
688 has not been started.
689 </para>
691 <para>
692 What this does is to record the links created by
693 <application>pg_upgrade</application>'s link mode that connect files in the
694 old and new clusters on the primary server. It then finds matching
695 files in the standby's old cluster and creates links for them in the
696 standby's new cluster. Files that were not linked on the primary
697 are copied from the primary to the standby. (They are usually
698 small.) This provides rapid standby upgrades. Unfortunately,
699 <application>rsync</application> needlessly copies files associated with
700 temporary and unlogged tables because these files don't normally
701 exist on standby servers.
702 </para>
704 <para>
705 If you have tablespaces, you will need to run a similar
706 <application>rsync</application> command for each tablespace directory, e.g.:
708 <programlisting>
709 rsync --archive --delete --hard-links --size-only --no-inc-recursive /vol1/pg_tblsp/PG_12_201909212 \
710 /vol1/pg_tblsp/PG_&majorversion;_202307071 standby.example.com:/vol1/pg_tblsp
711 </programlisting>
713 If you have relocated <filename>pg_wal</filename> outside the data
714 directories, <application>rsync</application> must be run on those directories
715 too.
716 </para>
717 </step>
719 <step>
720 <title>Configure streaming replication and log-shipping standby servers</title>
722 <para>
723 Configure the servers for log shipping. (You do not need to run
724 <function>pg_backup_start()</function> and <function>pg_backup_stop()</function>
725 or take a file system backup as the standbys are still synchronized
726 with the primary.) Only logical slots on the primary are copied to the
727 new standby, but other slots on the old standby are not copied so must
728 be recreated manually.
729 </para>
730 </step>
732 </procedure>
734 </step>
736 <step>
737 <title>Restore <filename>pg_hba.conf</filename></title>
739 <para>
740 If you modified <filename>pg_hba.conf</filename>, restore its original settings.
741 It might also be necessary to adjust other configuration files in the new
742 cluster to match the old cluster, e.g., <filename>postgresql.conf</filename>
743 (and any files included by it), <filename>postgresql.auto.conf</filename>.
744 </para>
745 </step>
747 <step>
748 <title>Start the new server</title>
750 <para>
751 The new server can now be safely started, and then any
752 <application>rsync</application>'ed standby servers.
753 </para>
754 </step>
756 <step>
757 <title>Post-upgrade processing</title>
759 <para>
760 If any post-upgrade processing is required, pg_upgrade will issue
761 warnings as it completes. It will also generate script files that must
762 be run by the administrator. The script files will connect to each
763 database that needs post-upgrade processing. Each script should be
764 run using:
766 <programlisting>
767 psql --username=postgres --file=script.sql postgres
768 </programlisting>
770 The scripts can be run in any order and can be deleted once they have
771 been run.
772 </para>
774 <caution>
775 <para>
776 In general it is unsafe to access tables referenced in rebuild scripts
777 until the rebuild scripts have run to completion; doing so could yield
778 incorrect results or poor performance. Tables not referenced in rebuild
779 scripts can be accessed immediately.
780 </para>
781 </caution>
782 </step>
784 <step>
785 <title>Statistics</title>
787 <para>
788 Because optimizer statistics are not transferred by <command>pg_upgrade</command>, you will
789 be instructed to run a command to regenerate that information at the end
790 of the upgrade. You might need to set connection parameters to
791 match your new cluster.
792 </para>
793 </step>
795 <step>
796 <title>Delete old cluster</title>
798 <para>
799 Once you are satisfied with the upgrade, you can delete the old
800 cluster's data directories by running the script mentioned when
801 <command>pg_upgrade</command> completes. (Automatic deletion is not
802 possible if you have user-defined tablespaces inside the old data
803 directory.) You can also delete the old installation directories
804 (e.g., <filename>bin</filename>, <filename>share</filename>).
805 </para>
806 </step>
808 <step id="pgupgrade-step-revert" performance="optional">
809 <title>Reverting to old cluster</title>
811 <para>
812 If, after running <command>pg_upgrade</command>, you wish to revert to the old cluster,
813 there are several options:
815 <itemizedlist>
816 <listitem>
817 <para>
818 If the <option>--check</option> option was used, the old cluster
819 was unmodified; it can be restarted.
820 </para>
821 </listitem>
823 <listitem>
824 <para>
825 If the <option>--link</option> option was <emphasis>not</emphasis>
826 used, the old cluster was unmodified; it can be restarted.
827 </para>
828 </listitem>
830 <listitem>
831 <para>
832 If the <option>--link</option> option was used, the data
833 files might be shared between the old and new cluster:
835 <itemizedlist>
836 <listitem>
837 <para>
838 If <command>pg_upgrade</command> aborted before linking started,
839 the old cluster was unmodified; it can be restarted.
840 </para>
841 </listitem>
843 <listitem>
844 <para>
845 If you did <emphasis>not</emphasis> start the new cluster, the old
846 cluster was unmodified except that, when linking started, a
847 <literal>.old</literal> suffix was appended to
848 <filename>$PGDATA/global/pg_control</filename>. To reuse the old
849 cluster, remove the <filename>.old</filename> suffix from
850 <filename>$PGDATA/global/pg_control</filename>; you can then restart
851 the old cluster.
852 </para>
853 </listitem>
855 <listitem>
856 <para>
857 If you did start the new cluster, it has written to shared files
858 and it is unsafe to use the old cluster. The old cluster will
859 need to be restored from backup in this case.
860 </para>
861 </listitem>
863 </itemizedlist></para>
864 </listitem>
865 </itemizedlist></para>
866 </step>
867 </procedure>
869 </refsect1>
871 <refsect1>
872 <title>Notes</title>
874 <para>
875 <application>pg_upgrade</application> creates various working files, such
876 as schema dumps, stored within <filename>pg_upgrade_output.d</filename> in
877 the directory of the new cluster. Each run creates a new subdirectory named
878 with a timestamp formatted as per ISO 8601
879 (<literal>%Y%m%dT%H%M%S</literal>), where all its generated files are
880 stored.
881 <filename>pg_upgrade_output.d</filename> and its contained files will be
882 removed automatically if <application>pg_upgrade</application> completes
883 successfully; but in the event of trouble, the files there may provide
884 useful debugging information.
885 </para>
887 <para>
888 <application>pg_upgrade</application> launches short-lived postmasters in
889 the old and new data directories. Temporary Unix socket files for
890 communication with these postmasters are, by default, made in the current
891 working directory. In some situations the path name for the current
892 directory might be too long to be a valid socket name. In that case you
893 can use the <option>-s</option> option to put the socket files in some
894 directory with a shorter path name. For security, be sure that that
895 directory is not readable or writable by any other users.
896 (This is not supported on Windows.)
897 </para>
899 <para>
900 All failure, rebuild, and reindex cases will be reported by
901 <application>pg_upgrade</application> if they affect your installation;
902 post-upgrade scripts to rebuild tables and indexes will be
903 generated automatically. If you are trying to automate the upgrade
904 of many clusters, you should find that clusters with identical database
905 schemas require the same post-upgrade steps for all cluster upgrades;
906 this is because the post-upgrade steps are based on the database
907 schemas, and not user data.
908 </para>
910 <para>
911 For deployment testing, create a schema-only copy of the old cluster,
912 insert dummy data, and upgrade that.
913 </para>
915 <para>
916 <application>pg_upgrade</application> does not support upgrading of databases
917 containing table columns using these <type>reg*</type> OID-referencing system data types:
918 <simplelist>
919 <member><type>regcollation</type></member>
920 <member><type>regconfig</type></member>
921 <member><type>regdictionary</type></member>
922 <member><type>regnamespace</type></member>
923 <member><type>regoper</type></member>
924 <member><type>regoperator</type></member>
925 <member><type>regproc</type></member>
926 <member><type>regprocedure</type></member>
927 </simplelist>
928 (<type>regclass</type>, <type>regrole</type>, and <type>regtype</type> can be upgraded.)
929 </para>
931 <para>
932 If you want to use link mode and you do not want your old cluster
933 to be modified when the new cluster is started, consider using the clone mode.
934 If that is not available, make a copy of the
935 old cluster and upgrade that in link mode. To make a valid copy
936 of the old cluster, use <command>rsync</command> to create a dirty
937 copy of the old cluster while the server is running, then shut down
938 the old server and run <command>rsync --checksum</command> again to update the
939 copy with any changes to make it consistent. (<option>--checksum</option>
940 is necessary because <command>rsync</command> only has file modification-time
941 granularity of one second.) You might want to exclude some
942 files, e.g., <filename>postmaster.pid</filename>, as documented in <xref
943 linkend="backup-lowlevel-base-backup"/>. If your file system supports
944 file system snapshots or copy-on-write file copies, you can use that
945 to make a backup of the old cluster and tablespaces, though the snapshot
946 and copies must be created simultaneously or while the database server
947 is down.
948 </para>
950 </refsect1>
952 <refsect1>
953 <title>See Also</title>
955 <simplelist type="inline">
956 <member><xref linkend="app-initdb"/></member>
957 <member><xref linkend="app-pg-ctl"/></member>
958 <member><xref linkend="app-pgdump"/></member>
959 <member><xref linkend="app-postgres"/></member>
960 </simplelist>
961 </refsect1>
962 </refentry>