PostgreSQL 12'den 13'e pg_dump İle Versiyon Upgrade

PostgreSQL major versiyonunu birden farklı şekilde upgrade edebilirsiniz. Bunlardan en çok bilineni pg_upgrade adı verilen PostgreSQL toolunu kullanmaktır. Bunun dışında Logical replication yapısı kullanılarak veya dump-restore işlemi ile major versiyon upgrade'i yapılabilir. 

Üç yöntemin artıları ve eksikleri bulunmaktadır. Upgrade konusunda en doğrusu hangisidir diye bir durum söz konusu değildir. Mevcut sisteminize uygun olan yapıyı kullanmak en doğrusu olacaktır.

pg_upgrade Kullanımı

Upgrade için en sık kullanılan yöntemlerdendir. PostgreSQL servisini kapatmak, dolayısıyla kesinti alınması gerekmektedir. Upgrade işlemi ile mevcut data dizini kullanımı sonlanacağından, bu kesinti süresi data dizininin taşınması için gereken süreye eş değerdir. pg_upgrade komutu ile bu süreyi kısaltmanın bir yolu dosyaların taşınması yerine linkleme seçeneğinin kullanılmasından geçer. 

Upgrade işlemi öncesi veritabanınızı mutlaka yedeklemenizi öneririm. Bunu storage bazında data directory'nin tamamen yedeklenesiyle, dump alınmasıyla veya sunucu snapshot'ını alarak gerçekleştirebilirsiniz. İşlem sonrası beklenmeyen sorunla karşılaşıldığında backup planımız olsun. 

En çok kullanılan ve bilinen yöntem olan pg_upgrade ile versiyon yükseltmeyi deneyelim.

Ubuntu 16.04.7 LTS üzerinde koşan PostgreSQL 12 ortamım bulunuyor. Hızlıca benzer ortamı kuralım.

PostgreSQL 12 ve 13 Kurulumu

Basic PostgreSQL kurulum adımlarını PostgreSQL'in download adresinden bulabilirsiniz. Ben de buradan faydalandım. 

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list' wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update sudo apt-get -y install postgresql-12

Sunucu üzerinde PostgreSQL 13 instance'inin oluşturulması gerekir. PostgreSQL12 kurulumu sırasında 13 kurulumu için gereken bazı adımları yukarıdaki komutlarla tamamladık. PostgreSQL 13 kurulumu için aşağıdaki komutun çalıştırılması yeterlidir.
   
sudo apt-get -y install postgresql-13

PostgreSQL 13 instance'i kuralım. Burada dikkat edilmesi gereken şey, eski ve yeni data dizini ayrı tutulmalıdır. Instance kurulumu sonrası bu yeni db içine database, tablo, kullanıcı gibi herhangi yeni database objesi eklenmemelidir. Aksi takdirde upgrade işlemi hata ile sonlanacaktır.

/usr/lib/postgresql/13/bin/initdb -D /yenidatadizini -k

pg_upgrade öncesi 12 ve 13 PostgreSQL servislerinin kapatılması gerekir.

systemctl stop postgresql@12.service systemctl stop postgresql@13.service

pg_upgrade komutu içinde eski ve yeni olmak üzere her iki PostgreSQL instance'ina ait binary directory, data directory, config directory gibi bilgileri vermek gerekir. Bu komutu dry-run modunda olabilecek hataları önceden görmek ve müdehale etmek için --check parametresi ile çalıştırdım.

postgres@test-gunce:~$ /usr/lib/postgresql/13/bin/pg_upgrade \ -b /usr/lib/postgresql/12/bin/ \ -B /usr/lib/postgresql/13/bin/ \ -d /var/lib/postgresql/12/main/ \ -D /var/lib/postgresql/13/main/ \ --check \ --old-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' \ --new-options '-c config_file=/etc/postgresql/13/main/postgresql.conf' \ -p 5412 -P 5413 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 reg* data types in user tables ok Checking for contrib/isn with bigint-passing mismatch 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* postgres@test-gunce:~$

Herhangi bir sorun bulunmadığından aynı komutu --check seçeneği olmadan çalıştırdım.

postgres@test-gunce:~$ /usr/lib/postgresql/13/bin/pg_upgrade -b /usr/lib/postgresql/12/bin/ -B /usr/lib/postgresql/13/bin/ -d /var/lib/postgresql/12/main/ -D /var/lib/postgresql/13/main/ --old-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' --new-options '-c config_file=/etc/postgresql/13/main/postgresql.conf' -p 5412 -P 5413 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 reg* data types in user tables ok Checking for contrib/isn with bigint-passing mismatch 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 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 analyze new cluster ok Creating script to delete old cluster ok Upgrade Complete ---------------- Optimizer statistics are not transferred by pg_upgrade so, once you start the new server, consider running: ./analyze_new_cluster.sh Running this script will delete the old cluster's data files: ./delete_old_cluster.sh

Upgrade işlemi başarılı şekilde sonlandı. Son bir adım kaldı. O da tüm db bazında VACUUM işleminin yapılması için ./analyze_new_cluster.sh shell dosyasının çalıştırılması.

vacuumdb: vacuuming database "postgres" vacuumdb: vacuuming database "template1" vacuumdb: vacuuming database "upgrd"

Logical replication ile online olarak upgrade işlemi için bir sonraki yazıyı bekleyin :)

Comments

Popular posts from this blog

PostgreSQL High Availability - Patroni 2

PostgreSQL Foreign Data Wrappers

PostgreSQL High Availability - Patroni 1