1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
|
<!--
doc/src/sgml/ref/pgupgrade.sgml
PostgreSQL documentation
-->
<refentry id="pgupgrade">
<indexterm zone="pgupgrade">
<primary>pg_upgrade</primary>
</indexterm>
<refmeta>
<refentrytitle><application>pg_upgrade</application></refentrytitle>
<manvolnum>1</manvolnum>
<refmiscinfo>Application</refmiscinfo>
</refmeta>
<refnamediv>
<refname>pg_upgrade</refname>
<refpurpose>upgrade a <productname>PostgreSQL</productname> server instance</refpurpose>
</refnamediv>
<refsynopsisdiv>
<cmdsynopsis>
<command>pg_upgrade</command>
<arg choice="plain"><option>-b</option></arg>
<arg choice="plain"><replaceable>oldbindir</replaceable></arg>
<arg choice="opt"><option>-B</option> <replaceable>newbindir</replaceable></arg>
<arg choice="plain"><option>-d</option></arg>
<arg choice="plain"><replaceable>oldconfigdir</replaceable></arg>
<arg choice="plain"><option>-D</option></arg>
<arg choice="plain"><replaceable>newconfigdir</replaceable></arg>
<arg rep="repeat"><replaceable>option</replaceable></arg>
</cmdsynopsis>
</refsynopsisdiv>
<refsect1>
<title>Description</title>
<para>
<application>pg_upgrade</application> (formerly called <application>pg_migrator</application>) allows data
stored in <productname>PostgreSQL</productname> data files to be upgraded to a later <productname>PostgreSQL</productname>
major version without the data dump/restore typically required for
major version upgrades, e.g., from 12.14 to 13.10 or from 14.9 to 15.5.
It is not required for minor version upgrades, e.g., from 12.7 to 12.8
or from 14.1 to 14.5.
</para>
<para>
Major PostgreSQL releases regularly add new features that often
change the layout of the system tables, but the internal data storage
format rarely changes. <application>pg_upgrade</application> uses this fact
to perform rapid upgrades by creating new system tables and simply
reusing the old user data files. If a future major release ever
changes the data storage format in a way that makes the old data
format unreadable, <application>pg_upgrade</application> will not be usable
for such upgrades. (The community will attempt to avoid such
situations.)
</para>
<para>
<application>pg_upgrade</application> does its best to
make sure the old and new clusters are binary-compatible, e.g., by
checking for compatible compile-time settings, including 32/64-bit
binaries. It is important that
any external modules are also binary compatible, though this cannot
be checked by <application>pg_upgrade</application>.
</para>
<para>
<application>pg_upgrade</application> supports upgrades from 9.2.X and later to the current
major release of <productname>PostgreSQL</productname>, including snapshot and beta releases.
</para>
</refsect1>
<refsect1>
<title>Options</title>
<para>
<application>pg_upgrade</application> accepts the following command-line arguments:
<variablelist>
<varlistentry>
<term><option>-b</option> <replaceable>bindir</replaceable></term>
<term><option>--old-bindir=</option><replaceable>bindir</replaceable></term>
<listitem><para>the old PostgreSQL executable directory;
environment variable <envar>PGBINOLD</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-B</option> <replaceable>bindir</replaceable></term>
<term><option>--new-bindir=</option><replaceable>bindir</replaceable></term>
<listitem><para>the new PostgreSQL executable directory;
default is the directory where <application>pg_upgrade</application> resides;
environment variable <envar>PGBINNEW</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-c</option></term>
<term><option>--check</option></term>
<listitem><para>check clusters only, don't change any data</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-d</option> <replaceable>configdir</replaceable></term>
<term><option>--old-datadir=</option><replaceable>configdir</replaceable></term>
<listitem><para>the old database cluster configuration directory; environment
variable <envar>PGDATAOLD</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-D</option> <replaceable>configdir</replaceable></term>
<term><option>--new-datadir=</option><replaceable>configdir</replaceable></term>
<listitem><para>the new database cluster configuration directory; environment
variable <envar>PGDATANEW</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-j <replaceable class="parameter">njobs</replaceable></option></term>
<term><option>--jobs=<replaceable class="parameter">njobs</replaceable></option></term>
<listitem><para>number of simultaneous connections and processes/threads to use
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-k</option></term>
<term><option>--link</option></term>
<listitem><para>use hard links instead of copying files to the new
cluster</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-N</option></term>
<term><option>--no-sync</option></term>
<listitem>
<para>
By default, <command>pg_upgrade</command> will wait for all files
of the upgraded cluster to be written safely to disk. This option
causes <command>pg_upgrade</command> to return without waiting, which
is faster, but means that a subsequent operating system crash can leave
the data directory corrupt. Generally, this option is
useful for testing but should not be used on a production
installation.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-o</option> <replaceable class="parameter">options</replaceable></term>
<term><option>--old-options</option> <replaceable class="parameter">options</replaceable></term>
<listitem><para>options to be passed directly to the
old <command>postgres</command> command; multiple
option invocations are appended</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-O</option> <replaceable class="parameter">options</replaceable></term>
<term><option>--new-options</option> <replaceable class="parameter">options</replaceable></term>
<listitem><para>options to be passed directly to the
new <command>postgres</command> command; multiple
option invocations are appended</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-p</option> <replaceable>port</replaceable></term>
<term><option>--old-port=</option><replaceable>port</replaceable></term>
<listitem><para>the old cluster port number; environment
variable <envar>PGPORTOLD</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-P</option> <replaceable>port</replaceable></term>
<term><option>--new-port=</option><replaceable>port</replaceable></term>
<listitem><para>the new cluster port number; environment
variable <envar>PGPORTNEW</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-r</option></term>
<term><option>--retain</option></term>
<listitem><para>retain SQL and log files even after successful completion
</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-s</option> <replaceable>dir</replaceable></term>
<term><option>--socketdir=</option><replaceable>dir</replaceable></term>
<listitem><para>directory to use for postmaster sockets during upgrade;
default is current working directory; environment
variable <envar>PGSOCKETDIR</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-U</option> <replaceable>username</replaceable></term>
<term><option>--username=</option><replaceable>username</replaceable></term>
<listitem><para>cluster's install user name; environment
variable <envar>PGUSER</envar></para></listitem>
</varlistentry>
<varlistentry>
<term><option>-v</option></term>
<term><option>--verbose</option></term>
<listitem><para>enable verbose internal logging</para></listitem>
</varlistentry>
<varlistentry>
<term><option>-V</option></term>
<term><option>--version</option></term>
<listitem><para>display version information, then exit</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--clone</option></term>
<listitem>
<para>
Use efficient file cloning (also known as <quote>reflinks</quote> on
some systems) instead of copying files to the new cluster. This can
result in near-instantaneous copying of the data files, giving the
speed advantages of <option>-k</option>/<option>--link</option> while
leaving the old cluster untouched.
</para>
<para>
File cloning is only supported on some operating systems and file
systems. If it is selected but not supported, the
<application>pg_upgrade</application> run will error. At present, it
is supported on Linux (kernel 4.5 or later) with Btrfs and XFS (on
file systems created with reflink support), and on macOS with APFS.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--copy</option></term>
<listitem>
<para>
Copy files to the new cluster. This is the default. (See also
<option>--link</option>, <option>--clone</option>,
<option>--copy-file-range</option>, and <option>--swap</option>.)
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--copy-file-range</option></term>
<listitem>
<para>
Use the <function>copy_file_range</function> system call for efficient
copying. On some file systems this gives results similar to
<option>--clone</option>, sharing physical disk blocks, while on others
it may still copy blocks, but do so via an optimized path. At present,
it is supported on Linux and FreeBSD.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--no-statistics</option></term>
<listitem>
<para>
Do not restore statistics from the old cluster into the new cluster.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--set-char-signedness=</option><replaceable>option</replaceable></term>
<listitem>
<para>
Manually set the default char signedness of new clusters. Possible values
are <literal>signed</literal> and <literal>unsigned</literal>.
</para>
<para>
In the C language, the default signedness of the <type>char</type> type
(when not explicitly specified) varies across platforms. For example,
<type>char</type> defaults to <type>signed char</type> on x86 CPUs but
to <type>unsigned char</type> on ARM CPUs.
</para>
<para>
Starting from <productname>PostgreSQL</productname> 18, database clusters
maintain their own default char signedness setting, which can be used to
ensure consistent behavior across platforms with different default char
signedness. By default, <application>pg_upgrade</application> preserves
the char signedness setting when upgrading from an existing cluster.
However, when upgrading from <productname>PostgreSQL</productname> 17 or
earlier, <application>pg_upgrade</application> adopts the char signedness
of the platform on which it was built.
</para>
<para>
This option allows you to explicitly set the default char signedness for
the new cluster, overriding any inherited values. There are two specific
scenarios where this option is relevant:
<itemizedlist>
<listitem>
<para>
If you are planning to migrate to a different platform after the upgrade,
you should not use this option. The default behavior is right in this case.
Instead, perform the upgrade on the original platform without this flag,
and then migrate the cluster afterward. This is the recommended and safest
approach.
</para>
</listitem>
<listitem>
<para>
If you have already migrated the cluster to a platform with different
char signedness (for example, from an x86-based system to an ARM-based
system), you should use this option to specify the signedness matching
the original platform's default char signedness. Additionally, it's
essential not to modify any data files between migrating data files and
running <command>pg_upgrade</command>. <command>pg_upgrade</command>
should be the first operation that starts the cluster on the new platform.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--swap</option></term>
<listitem>
<para>
Move the data directories from the old cluster to the new cluster.
Then, replace the catalog files with those generated for the new
cluster. This mode can outperform <option>--link</option>,
<option>--clone</option>, <option>--copy</option>, and
<option>--copy-file-range</option>, especially on clusters with many
relations.
</para>
<para>
However, this mode creates many garbage files in the old cluster, which
can prolong the file synchronization step if
<option>--sync-method=syncfs</option> is used. Therefore, it is
recommended to use <option>--sync-method=fsync</option> with
<option>--swap</option>.
</para>
<para>
Additionally, once the file transfer step begins, the old cluster will
be destructively modified and therefore will no longer be safe to
start. See <xref linkend="pgupgrade-step-revert"/> for details.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>--sync-method=</option><replaceable>method</replaceable></term>
<listitem>
<para>
When set to <literal>fsync</literal>, which is the default,
<command>pg_upgrade</command> will recursively open and synchronize all
files in the upgraded cluster's data directory. The search for files
will follow symbolic links for the WAL directory and each configured
tablespace.
</para>
<para>
On Linux, <literal>syncfs</literal> may be used instead to ask the
operating system to synchronize the whole file systems that contain the
upgraded cluster's data directory, its WAL files, and each tablespace.
See <xref linkend="guc-recovery-init-sync-method"/> for information
about the caveats to be aware of when using <literal>syncfs</literal>.
</para>
<para>
This option has no effect when <option>--no-sync</option> is used.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-?</option></term>
<term><option>--help</option></term>
<listitem><para>show help, then exit</para></listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1>
<title>Usage</title>
<para>
These are the steps to perform an upgrade
with <application>pg_upgrade</application>:
</para>
<note>
<para>
The steps to upgrade <glossterm linkend="glossary-logical-replication-cluster">logical replication clusters</glossterm>
are not covered here;
refer to <xref linkend="logical-replication-upgrade"/> for details.
</para>
</note>
<procedure>
<step performance="optional">
<title>Optionally move the old cluster</title>
<para>
If you are using a version-specific installation directory, e.g.,
<filename>/opt/PostgreSQL/&majorversion;</filename>, you do not need to move the old cluster. The
graphical installers all use version-specific installation directories.
</para>
<para>
If your installation directory is not version-specific, e.g.,
<filename>/usr/local/pgsql</filename>, it is necessary to move the current PostgreSQL install
directory so it does not interfere with the new <productname>PostgreSQL</productname> installation.
Once the current <productname>PostgreSQL</productname> server is shut down, it is safe to rename the
PostgreSQL installation directory; assuming the old directory is
<filename>/usr/local/pgsql</filename>, you can do:
<programlisting>
mv /usr/local/pgsql /usr/local/pgsql.old
</programlisting>
to rename the directory.
</para>
</step>
<step>
<title>For source installs, build the new version</title>
<para>
Build the new PostgreSQL source with <command>configure</command> flags that are compatible
with the old cluster. <application>pg_upgrade</application> will check <command>pg_controldata</command> to make
sure all settings are compatible before starting the upgrade.
</para>
</step>
<step>
<title>Install the new PostgreSQL binaries</title>
<para>
Install the new server's binaries and support
files. <application>pg_upgrade</application> is included in a default installation.
</para>
<para>
For source installs, if you wish to install the new server in a custom
location, use the <literal>prefix</literal> variable:
<programlisting>
make prefix=/usr/local/pgsql.new install
</programlisting></para>
</step>
<step>
<title>Initialize the new PostgreSQL cluster</title>
<para>
Initialize the new cluster using <command>initdb</command>.
Again, use compatible <command>initdb</command>
flags that match the old cluster. Many
prebuilt installers do this step automatically. There is no need to
start the new cluster.
</para>
</step>
<step>
<title>Install extension shared object files</title>
<para>
Many extensions and custom modules, whether from
<filename>contrib</filename> or another source, use shared object
files (or DLLs), e.g., <filename>pgcrypto.so</filename>. If the old
cluster used these, shared object files matching the new server binary
must be installed in the new cluster, usually via operating system
commands. Do not load the schema definitions, e.g., <command>CREATE
EXTENSION pgcrypto</command>, because these will be duplicated from
the old cluster. If extension updates are available,
<application>pg_upgrade</application> will report this and create
a script that can be run later to update them.
</para>
</step>
<step>
<title>Copy custom full-text search files</title>
<para>
Copy any custom full text search files (dictionary, synonym,
thesaurus, stop words) from the old to the new cluster.
</para>
</step>
<step>
<title>Adjust authentication</title>
<para>
<command>pg_upgrade</command> will connect to the old and new servers several
times, so you might want to set authentication to <literal>peer</literal>
in <filename>pg_hba.conf</filename> or use a <filename>~/.pgpass</filename> file
(see <xref linkend="libpq-pgpass"/>).
</para>
</step>
<step>
<title>Stop both servers</title>
<para>
Make sure both database servers are stopped using, on Unix, e.g.:
<programlisting>
pg_ctl -D /opt/PostgreSQL/12 stop
pg_ctl -D /opt/PostgreSQL/&majorversion; stop
</programlisting>
or on Windows, using the proper service names:
<programlisting>
NET STOP postgresql-12
NET STOP postgresql-&majorversion;
</programlisting>
</para>
<para>
Streaming replication and log-shipping standby servers must be
running during this shutdown so they receive all changes.
</para>
</step>
<step>
<title>Prepare for standby server upgrades</title>
<para>
If you are upgrading standby servers using methods outlined in section <xref
linkend="pgupgrade-step-replicas"/>, verify that the old standby
servers are caught up by running <application>pg_controldata</application>
against the old primary and standby clusters. Verify that the
<quote>Latest checkpoint location</quote> values match in all clusters.
Also, make sure <varname>wal_level</varname> is not set to
<literal>minimal</literal> in the <filename>postgresql.conf</filename> file on the
new primary cluster.
</para>
</step>
<step>
<title>Run <application>pg_upgrade</application></title>
<para>
Always run the <application>pg_upgrade</application> binary of the new server, not the old one.
<application>pg_upgrade</application> requires the specification of the old and new cluster's
data and executable (<filename>bin</filename>) directories. You can also specify
user and port values, and whether you want the data files linked, cloned, or swapped
instead of the default copy behavior.
</para>
<para>
If you use link mode, the upgrade will be much faster (no file
copying) and use less disk space, but you will not be able to access
your old cluster
once you start the new cluster after the upgrade. Link mode also
requires that the old and new cluster data directories be in the
same file system. (Tablespaces and <filename>pg_wal</filename> can be on
different file systems.)
Clone mode provides the same speed and disk space advantages but
does not cause the old cluster to be unusable once the new cluster
is started. Clone mode also requires that the old and new data
directories be in the same file system. This mode is only available
on certain operating systems and file systems.
Swap mode may be the fastest if there are many relations, but you will not
be able to access your old cluster once the file transfer step begins.
Swap mode also requires that the old and new cluster data directories be
in the same file system.
</para>
<para>
Setting <option>--jobs</option> to 2 or higher allows pg_upgrade to
process multiple databases and tablespaces in parallel. A good starting
point is the number of CPU cores on the machine. This option can
substantially reduce the upgrade time for multi-database and
multi-tablespace servers.
</para>
<para>
For Windows users, you must be logged into an administrative account,
and then run <application>pg_upgrade</application> with quoted
directories, e.g.:
<programlisting>
pg_upgrade.exe
--old-datadir "C:/Program Files/PostgreSQL/12/data"
--new-datadir "C:/Program Files/PostgreSQL/&majorversion;/data"
--old-bindir "C:/Program Files/PostgreSQL/12/bin"
--new-bindir "C:/Program Files/PostgreSQL/&majorversion;/bin"
</programlisting>
Once started, <command>pg_upgrade</command> will verify the two clusters are compatible
and then do the upgrade. You can use <command>pg_upgrade --check</command>
to perform only the checks, even if the old server is still
running. <command>pg_upgrade --check</command> will also outline any
manual adjustments you will need to make after the upgrade. If you
are going to be using link, clone, copy-file-range, or swap mode, you
should use the option <option>--link</option>, <option>--clone</option>,
<option>--copy-file-range</option>, or <option>--swap</option> with
<option>--check</option> to enable mode-specific checks.
<command>pg_upgrade</command> requires write permission in the current directory.
</para>
<para>
Obviously, no one should be accessing the clusters during the
upgrade. <application>pg_upgrade</application> defaults to running servers
on port 50432 to avoid unintended client connections.
You can use the same port number for both clusters when doing an
upgrade because the old and new clusters will not be running at the
same time. However, when checking an old running server, the old
and new port numbers must be different.
</para>
<para>
If an error occurs while restoring the database schema, <command>pg_upgrade</command> will
exit and you will have to revert to the old cluster as outlined in <xref linkend="pgupgrade-step-revert"/>
below. To try <command>pg_upgrade</command> again, you will need to modify the old
cluster so the pg_upgrade schema restore succeeds. If the problem is a
<filename>contrib</filename> module, you might need to uninstall the <filename>contrib</filename> module from
the old cluster and install it in the new cluster after the upgrade,
assuming the module is not being used to store user data.
</para>
</step>
<step id="pgupgrade-step-replicas">
<title>Upgrade streaming replication and log-shipping standby servers</title>
<para>
If you used link mode and have Streaming Replication (see <xref
linkend="streaming-replication"/>) or Log-Shipping (see <xref
linkend="warm-standby"/>) standby servers, you can follow these steps to
quickly upgrade them. You will not be running <application>pg_upgrade</application> on
the standby servers, but rather <application>rsync</application> on the primary.
Do not start any servers yet.
</para>
<para>
If you did <emphasis>not</emphasis> use link mode, do not have or do not
want to use <application>rsync</application>, or want an easier solution, skip
the instructions in this section and simply recreate the standby
servers once <application>pg_upgrade</application> completes and the new primary
is running.
</para>
<procedure>
<step>
<title>Install the new PostgreSQL binaries on standby servers</title>
<para>
Make sure the new binaries and support files are installed on all
standby servers.
</para>
</step>
<step>
<title>Make sure the new standby data directories do <emphasis>not</emphasis> exist</title>
<para>
Make sure the new standby data directories do <emphasis>not</emphasis>
exist or are empty. If <application>initdb</application> was run, delete
the standby servers' new data directories.
</para>
</step>
<step>
<title>Install extension shared object files</title>
<para>
Install the same extension shared object files on the new standbys
that you installed in the new primary cluster.
</para>
</step>
<step>
<title>Stop standby servers</title>
<para>
If the standby servers are still running, stop them now using the
above instructions.
</para>
</step>
<step>
<title>Save configuration files</title>
<para>
Save any configuration files from the old standbys' configuration
directories you need to keep, e.g., <filename>postgresql.conf</filename>
(and any files included by it), <filename>postgresql.auto.conf</filename>,
<literal>pg_hba.conf</literal>, because these will be overwritten
or removed in the next step.
</para>
</step>
<step>
<title>Run <application>rsync</application></title>
<para>
When using link mode, standby servers can be quickly upgraded using
<application>rsync</application>. To accomplish this, from a directory on
the primary server that is above the old and new database cluster
directories, run this on the <emphasis>primary</emphasis> for each standby
server:
<programlisting>
rsync --archive --delete --hard-links --size-only --no-inc-recursive old_cluster new_cluster remote_dir
</programlisting>
where <option>old_cluster</option> and <option>new_cluster</option> are relative
to the current directory on the primary, and <option>remote_dir</option>
is <emphasis>above</emphasis> the old and new cluster directories
on the standby. The directory structure under the specified
directories on the primary and standbys must match. Consult the
<application>rsync</application> manual page for details on specifying the
remote directory, e.g.,
<programlisting>
rsync --archive --delete --hard-links --size-only --no-inc-recursive /opt/PostgreSQL/12 \
/opt/PostgreSQL/&majorversion; standby.example.com:/opt/PostgreSQL
</programlisting>
You can verify what the command will do using
<application>rsync</application>'s <option>--dry-run</option> option. While
<application>rsync</application> must be run on the primary for at least one
standby, it is possible to run <application>rsync</application> on an upgraded
standby to upgrade other standbys, as long as the upgraded standby
has not been started.
</para>
<para>
What this does is to record the links created by
<application>pg_upgrade</application>'s link mode that connect files in the
old and new clusters on the primary server. It then finds matching
files in the standby's old cluster and creates links for them in the
standby's new cluster. Files that were not linked on the primary
are copied from the primary to the standby. (They are usually
small.) This provides rapid standby upgrades. Unfortunately,
<application>rsync</application> needlessly copies files associated with
temporary and unlogged tables because these files don't normally
exist on standby servers.
</para>
<para>
If you have tablespaces, you will need to run a similar
<application>rsync</application> command for each tablespace directory, e.g.:
<programlisting>
rsync --archive --delete --hard-links --size-only --no-inc-recursive /vol1/pg_tblsp/PG_12_201909212 \
/vol1/pg_tblsp/PG_&majorversion;_202307071 standby.example.com:/vol1/pg_tblsp
</programlisting>
If you have relocated <filename>pg_wal</filename> outside the data
directories, <application>rsync</application> must be run on those directories
too.
</para>
</step>
<step>
<title>Configure streaming replication and log-shipping standby servers</title>
<para>
Configure the servers for log shipping. (You do not need to run
<function>pg_backup_start()</function> and <function>pg_backup_stop()</function>
or take a file system backup as the standbys are still synchronized
with the primary.) If the old primary is prior to version 17.0, then no
slots on the primary are copied to the new standby, so all the slots on
the old standby must be recreated manually. If the old primary is
version 17.0 or later, then only logical slots on the primary are copied
to the new standby, but other slots on the old standby are not copied,
so must be recreated manually.
</para>
</step>
</procedure>
</step>
<step>
<title>Restore <filename>pg_hba.conf</filename></title>
<para>
If you modified <filename>pg_hba.conf</filename>, restore its original settings.
It might also be necessary to adjust other configuration files in the new
cluster to match the old cluster, e.g., <filename>postgresql.conf</filename>
(and any files included by it), <filename>postgresql.auto.conf</filename>.
</para>
</step>
<step>
<title>Start the new server</title>
<para>
The new server can now be safely started, and then any
<application>rsync</application>'ed standby servers.
</para>
</step>
<step>
<title>Post-upgrade processing</title>
<para>
If any post-upgrade processing is required, pg_upgrade will issue
warnings as it completes. It will also generate script files that must
be run by the administrator. The script files will connect to each
database that needs post-upgrade processing. Each script should be
run using:
<programlisting>
psql --username=postgres --file=script.sql postgres
</programlisting>
The scripts can be run in any order and can be deleted once they have
been run.
</para>
<caution>
<para>
In general it is unsafe to access tables referenced in rebuild scripts
until the rebuild scripts have run to completion; doing so could yield
incorrect results or poor performance. Tables not referenced in rebuild
scripts can be accessed immediately.
</para>
</caution>
</step>
<step>
<title>Statistics</title>
<para>
Unless the <option>--no-statistics</option> option is specified,
<command>pg_upgrade</command> will transfer most optimizer statistics
from the old cluster to the new cluster. However, some statistics may
not be transferred, such as those created explicitly with <xref
linkend="sql-createstatistics"/> or custom statistics added by an
extension.
</para>
<para>
Because not all statistics are transferred by
<command>pg_upgrade</command>, you will be instructed to run commands to
regenerate that information at the end of the upgrade. You might need to
set connection parameters to match your new cluster.
</para>
<para>
First, use
<command>vacuumdb --all --analyze-in-stages --missing-stats-only</command>
to quickly generate minimal optimizer statistics for relations without
any. Then, use <command>vacuumdb --all --analyze-only</command> to ensure
all relations have updated cumulative statistics for triggering vacuum and
analyze. For both commands, the use of <option>--jobs</option> can speed
it up.
If <varname>vacuum_cost_delay</varname> is set to a non-zero
value, this can be overridden to speed up statistics generation
using <envar>PGOPTIONS</envar>, e.g., <literal>PGOPTIONS='-c
vacuum_cost_delay=0' vacuumdb ...</literal>.
</para>
</step>
<step>
<title>Delete old cluster</title>
<para>
Once you are satisfied with the upgrade, you can delete the old
cluster's data directories by running the script mentioned when
<command>pg_upgrade</command> completes. (Automatic deletion is not
possible if you have user-defined tablespaces inside the old data
directory.) You can also delete the old installation directories
(e.g., <filename>bin</filename>, <filename>share</filename>).
</para>
</step>
<step id="pgupgrade-step-revert" performance="optional">
<title>Reverting to old cluster</title>
<para>
If, after running <command>pg_upgrade</command>, you wish to revert to the old cluster,
there are several options:
<itemizedlist>
<listitem>
<para>
If the <option>--check</option> option was used, the old cluster
was unmodified; it can be restarted.
</para>
</listitem>
<listitem>
<para>
If neither <option>--link</option> nor <option>--swap</option> was
used, the old cluster was unmodified; it can be restarted.
</para>
</listitem>
<listitem>
<para>
If the <option>--link</option> option was used, the data
files might be shared between the old and new cluster:
<itemizedlist>
<listitem>
<para>
If <command>pg_upgrade</command> aborted before linking started,
the old cluster was unmodified; it can be restarted.
</para>
</listitem>
<listitem>
<para>
If you did <emphasis>not</emphasis> start the new cluster, the old
cluster was unmodified except that, when linking started, a
<literal>.old</literal> suffix was appended to
<filename>$PGDATA/global/pg_control</filename>. To reuse the old
cluster, remove the <filename>.old</filename> suffix from
<filename>$PGDATA/global/pg_control</filename>; you can then restart
the old cluster.
</para>
</listitem>
<listitem>
<para>
If you did start the new cluster, it has written to shared files
and it is unsafe to use the old cluster. The old cluster will
need to be restored from backup in this case.
</para>
</listitem>
</itemizedlist></para>
</listitem>
<listitem>
<para>
If the <option>--swap</option> option was used, the old cluster might
be destructively modified:
<itemizedlist>
<listitem>
<para>
If <command>pg_upgrade</command> aborts before reporting that the
old cluster is no longer safe to start, the old cluster was
unmodified; it can be restarted.
</para>
</listitem>
<listitem>
<para>
If <command>pg_upgrade</command> has reported that the old cluster
is no longer safe to start, the old cluster was destructively
modified. The old cluster will need to be restored from backup in
this case.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</itemizedlist></para>
</step>
</procedure>
</refsect1>
<refsect1>
<title>Environment</title>
<para>
Some environment variables can be used to provide defaults for command-line options:
<variablelist>
<varlistentry>
<term><envar>PGBINOLD</envar></term>
<listitem>
<para>
The old PostgreSQL executable directory; option
<option>-b</option>/<option>--old-bindir</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGBINNEW</envar></term>
<listitem>
<para>
The new PostgreSQL executable directory; option
<option>-B</option>/<option>--new-bindir</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGDATAOLD</envar></term>
<listitem>
<para>
The old database cluster configuration directory; option
<option>-d</option>/<option>--old-datadir</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGDATANEW</envar></term>
<listitem>
<para>
The new database cluster configuration directory; option
<option>-D</option>/<option>--new-datadir</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGPORTOLD</envar></term>
<listitem>
<para>
The old cluster port number; option
<option>-p</option>/<option>--old-port</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGPORTNEW</envar></term>
<listitem>
<para>
The new cluster port number; option
<option>-P</option>/<option>--new-port</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGSOCKETDIR</envar></term>
<listitem>
<para>
Directory to use for postmaster sockets during upgrade; option
<option>-s</option>/<option>--socketdir</option>.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><envar>PGUSER</envar></term>
<listitem>
<para>
Cluster's install user name; option
<option>-U</option>/<option>--username</option>.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1>
<title>Notes</title>
<para>
<application>pg_upgrade</application> creates various working files, such
as schema dumps, stored within <filename>pg_upgrade_output.d</filename> in
the directory of the new cluster. Each run creates a new subdirectory named
with a timestamp formatted as per ISO 8601
(<literal>%Y%m%dT%H%M%S</literal>), where all its generated files are
stored.
<filename>pg_upgrade_output.d</filename> and its contained files will be
removed automatically if <application>pg_upgrade</application> completes
successfully; but in the event of trouble, the files there may provide
useful debugging information.
</para>
<para>
<application>pg_upgrade</application> launches short-lived postmasters in
the old and new data directories. Temporary Unix socket files for
communication with these postmasters are, by default, made in the current
working directory. In some situations the path name for the current
directory might be too long to be a valid socket name. In that case you
can use the <option>-s</option> option to put the socket files in some
directory with a shorter path name. For security, be sure that that
directory is not readable or writable by any other users.
(This is not supported on Windows.)
</para>
<para>
All failure, rebuild, and reindex cases will be reported by
<application>pg_upgrade</application> if they affect your installation;
post-upgrade scripts to rebuild tables and indexes will be
generated automatically. If you are trying to automate the upgrade
of many clusters, you should find that clusters with identical database
schemas require the same post-upgrade steps for all cluster upgrades;
this is because the post-upgrade steps are based on the database
schemas, and not user data.
</para>
<para>
For deployment testing, create a schema-only copy of the old cluster,
insert dummy data, and upgrade that.
</para>
<para>
<application>pg_upgrade</application> does not support upgrading of databases
containing table columns using these <type>reg*</type> OID-referencing system data types:
<simplelist>
<member><type>regcollation</type></member>
<member><type>regconfig</type></member>
<member><type>regdictionary</type></member>
<member><type>regnamespace</type></member>
<member><type>regoper</type></member>
<member><type>regoperator</type></member>
<member><type>regproc</type></member>
<member><type>regprocedure</type></member>
</simplelist>
(<type>regclass</type>, <type>regdatabase</type>, <type>regrole</type>, and
<type>regtype</type> can be upgraded.)
</para>
<para>
If you want to use link mode and you do not want your old cluster
to be modified when the new cluster is started, consider using the clone mode.
If that is not available, make a copy of the
old cluster and upgrade that in link mode. To make a valid copy
of the old cluster, use <command>rsync</command> to create a dirty
copy of the old cluster while the server is running, then shut down
the old server and run <command>rsync --checksum</command> again to update the
copy with any changes to make it consistent. (<option>--checksum</option>
is necessary because <command>rsync</command> only has file modification-time
granularity of one second.) You might want to exclude some
files, e.g., <filename>postmaster.pid</filename>, as documented in <xref
linkend="backup-lowlevel-base-backup"/>. If your file system supports
file system snapshots or copy-on-write file copies, you can use that
to make a backup of the old cluster and tablespaces, though the snapshot
and copies must be created simultaneously or while the database server
is down.
</para>
</refsect1>
<refsect1>
<title>See Also</title>
<simplelist type="inline">
<member><xref linkend="app-initdb"/></member>
<member><xref linkend="app-pg-ctl"/></member>
<member><xref linkend="app-pgdump"/></member>
<member><xref linkend="app-postgres"/></member>
</simplelist>
</refsect1>
</refentry>
|