Saturday, October 23, 2021

Upgrade PostgreSQL 13 to PostgreSQL 14 on Oracle Linux 8 using YUM/RPM

 1. Install the PostgreSQL 14 Packages

#  yum install postgresql14-server.x86_64  postgresql14.x86_64  postgresql14-contrib.x86_64

2. Initialize PostgreSQL 14 database

# /usr/pgsql-14/bin/postgresql-14-setup initdb

3. Start PostgreSQL 13 database (required for pre-upgrade check)

 $ /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data/ start

4. Run pre-upgrade check:

/usr/pgsql-14/bin/pg_upgrade \
  --old-bindir=/usr/pgsql-13/bin \
  --new-bindir=/usr/pgsql-14/bin \
  --old-datadir=/var/lib/pgsql/13/data \
  --new-datadir=/var/lib/pgsql/14/data \
  --user=postgres \
  --check

"--check" means check clusters only, don't change any data, additional parameter "--verbose' useful for troubleshoting.

[postgres@ol8 ~]$ /usr/pgsql-14/bin/pg_upgrade \
>   --old-bindir=/usr/pgsql-13/bin \
>   --new-bindir=/usr/pgsql-14/bin \
>   --old-datadir=/var/lib/pgsql/13/data \
>   --new-datadir=/var/lib/pgsql/14/data \
>   --user=postgres \
>   --check
Performing Consistency Checks on Old Live Server
------------------------------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

*Clusters are compatible*

5. Shutdown original PostgreSQL 13
 
$ /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data/ stop

6. Run the actual upgrade

/usr/pgsql-14/bin/pg_upgrade \
  --old-bindir=/usr/pgsql-13/bin \
  --new-bindir=/usr/pgsql-14/bin \
  --old-datadir=/var/lib/pgsql/13/data \
  --new-datadir=/var/lib/pgsql/14/data \
  --user=postgres 

Additional useful parameters are:
  -j, --jobs=NUM         number of simultaneous processes or threads to use
  -k, --link                    link instead of copying files to new cluster 

[postgres@ol8 ~]$ /usr/pgsql-14/bin/pg_upgrade \
>   --old-bindir=/usr/pgsql-13/bin \
>   --new-bindir=/usr/pgsql-14/bin \
>   --old-datadir=/var/lib/pgsql/13/data \
>   --new-datadir=/var/lib/pgsql/14/data \
>   --user=postgres 
Performing Consistency Checks
-----------------------------
Checking cluster versions                                   ok
Checking database user is the install user                  ok
Checking database connection settings                       ok
Checking for prepared transactions                          ok
Checking for system-defined composite types in user tables  ok
Checking for reg* data types in user tables                 ok
Checking for contrib/isn with bigint-passing mismatch       ok
Checking for user-defined encoding conversions              ok
Checking for user-defined postfix operators                 ok
Creating dump of global objects                             ok
Creating dump of database schemas
                                                            ok
Checking for presence of required libraries                 ok
Checking database user is the install user                  ok
Checking for prepared transactions                          ok
Checking for new cluster tablespace directories             ok

If pg_upgrade fails after this point, you must re-initdb the
new cluster before continuing.

Performing Upgrade
------------------
Analyzing all rows in the new cluster                       ok
Freezing all rows in the new cluster                        ok
Deleting files from new pg_xact                             ok
Copying old pg_xact to new server                           ok
Setting oldest XID for new cluster                          ok
Setting next transaction ID and epoch for new cluster       ok
Deleting files from new pg_multixact/offsets                ok
Copying old pg_multixact/offsets to new server              ok
Deleting files from new pg_multixact/members                ok
Copying old pg_multixact/members to new server              ok
Setting next multixact ID and offset for new cluster        ok
Resetting WAL archives                                      ok
Setting frozenxid and minmxid counters in new cluster       ok
Restoring global objects in the new cluster                 ok
Restoring database schemas in the new cluster
                                                            ok
Copying user relation files
                                                            ok
Setting next OID for new cluster                            ok
Sync data directory to disk                                 ok
Creating script to delete old cluster                       ok
Checking for extension updates                              notice

Your installation contains extensions that should be updated
with the ALTER EXTENSION command.  The file
    update_extensions.sql
when executed by psql by the database superuser will update
these extensions.


Upgrade Complete
----------------
Optimizer statistics are not transferred by pg_upgrade.
Once you start the new server, consider running:
    /usr/pgsql-14/bin/vacuumdb -U postgres --all --analyze-in-stages

Running this script will delete the old cluster's data files:
    ./delete_old_cluster.sh

7. Start PostgreSQL 14 cluster

[root@ol8 ~]# systemctl start postgresql-14.service

[root@ol8 ~]# systemctl status postgresql-14.service

● postgresql-14.service - PostgreSQL 14 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-14.service; enabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-10-23 14:41:05 +08; 6s ago
     Docs: https://www.postgresql.org/docs/14/static/
  Process: 62779 ExecStartPre=/usr/pgsql-14/bin/postgresql-14-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 62787 (postmaster)
    Tasks: 8 (limit: 48578)
   Memory: 17.0M
   CGroup: /system.slice/postgresql-14.service
           ├─62787 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
           ├─62788 postgres: logger 
           ├─62790 postgres: checkpointer 
           ├─62791 postgres: background writer 
           ├─62792 postgres: walwriter 
           ├─62793 postgres: autovacuum launcher 
           ├─62794 postgres: stats collector 
           └─62795 postgres: logical replication launcher 

Oct 23 14:41:05 ol8.lab.net systemd[1]: Starting PostgreSQL 14 database server...
Oct 23 14:41:05 ol8.lab.net postmaster[62787]: 2021-10-23 14:41:05.238 +08 [62787] LOG:  redirecting log output to logging collector process
Oct 23 14:41:05 ol8.lab.net postmaster[62787]: 2021-10-23 14:41:05.238 +08 [62787] HINT:  Future log output will appear in directory "log".
Oct 23 14:41:05 ol8.lab.net systemd[1]: Started PostgreSQL 14 database server.

8. Upgrade the extension

[postgres@ol8 ~]$ cat update_extensions.sql
\connect test
ALTER EXTENSION "pageinspect" UPDATE;

[postgres@ol8 ~]$ psql -U postgres
psql (14.0)
Type "help" for help.

postgres=# \i update_extensions.sql
You are now connected to database "test" as user "postgres".
ALTER EXTENSION
test=# 


9. Update optimiser statistics

[postgres@ol8 ~]$  /usr/pgsql-14/bin/vacuumdb -U postgres --all --analyze-in-stages
vacuumdb: processing database "postgres": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "template1": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "test": Generating minimal optimizer statistics (1 target)
vacuumdb: processing database "postgres": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "template1": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "test": Generating medium optimizer statistics (10 targets)
vacuumdb: processing database "postgres": Generating default (full) optimizer statistics
vacuumdb: processing database "template1": Generating default (full) optimizer statistics
vacuumdb: processing database "test": Generating default (full) optimizer statistics

10. Clean up

# yum remove postgresql13-13.4-1PGDG.rhel8.x86_64 postgresql13-contrib-13.4-1PGDG.rhel8.x86_64 postgresql13-libs-13.4-1PGDG.rhel8.x86_64 postgresql13-server-13.4-1PGDG.rhel8.x86_64
# rm -rf '/var/lib/pgsql/13/data'




No comments:

Post a Comment