{"id":2761,"date":"2020-02-07T15:57:14","date_gmt":"2020-02-07T12:57:14","guid":{"rendered":"https:\/\/www.fatihacar.com\/blog\/?p=2761"},"modified":"2020-02-07T16:18:20","modified_gmt":"2020-02-07T13:18:20","slug":"how-to-use-pg_rewind-in-postgresql-12","status":"publish","type":"post","link":"https:\/\/www.fatihacar.com\/blog\/how-to-use-pg_rewind-in-postgresql-12\/","title":{"rendered":"How to Use pg_rewind in PostgreSQL 12"},"content":{"rendered":"<p>pg_rewind is a PostgreSQL tool since 9.5 version. It provides synchronization between standby and old primary. Especially, pg_rewind provides advantage for big databases. You can sync databases without using pg_basebackup.<\/p>\n<p>pg_rewind scans the old primary database&#8217;s pgdata folder and identifies data blocks changed during the switch to standby database, then copies only changed blocks from relation files are copied; all other files are copied in full, including configuration files from promoted standby database. And the old primary database synchronize with applying changed blocks.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.fatihacar.com\/blog\/resimler\/postgresql_pg_rewind.jpg\" alt=\"\" width=\"686\" height=\"236\" class=\"aligncenter size-full wp-image-2762\" srcset=\"https:\/\/www.fatihacar.com\/blog\/resimler\/postgresql_pg_rewind.jpg 686w, https:\/\/www.fatihacar.com\/blog\/resimler\/postgresql_pg_rewind-300x103.jpg 300w\" sizes=\"auto, (max-width: 686px) 100vw, 686px\" \/><\/p>\n<p>pg_rewind requires that the target server either has the <strong>wal_log_hints<\/strong> option enabled in postgresql.conf or <strong>data checksums<\/strong> enabled when database was initialized with initdb. <\/p>\n<p>Also, you should be careful about configuration files. postgresql.conf,pg_hba.conf and postgresql.auto.conf files copied from the promoted standby database. You should configure access list of pg_hba, port information and primary_conninfo parameters.<\/p>\n<p>Finally, you should create standby.signal file in data folder of old primary database before start of old primary database&#8217;s postgresql service.<\/p>\n<p><strong>TEST<\/strong><\/p>\n<p>I applied failover. And insert some test data to promoted standby database. Now I will sync promoted standby database and old primary database with pg_rewind.<\/p>\n<p><strong>Test Systems<\/strong><\/p>\n<blockquote><p>\nPrimary Database : 192.168.9.128 &#8211; 5432<br \/>\nStandby Database : 192.168.9.129 &#8211; 5433\n<\/p><\/blockquote>\n<p><strong>PG_REWIND Steps<\/strong><\/p>\n<p><strong>1 &#8211; Stop Old Primary Database<\/strong><\/p>\n<blockquote><p>systemctl stop postgresql-pri.service<\/p><\/blockquote>\n<p><strong>2- Run pg_rewind on Old Primary Database<\/strong><br \/>\nSource : Promoted Standby Database<br \/>\nTarget : Old Primary Database<\/p>\n<blockquote><p>su &#8211; postgres<br \/>\npg_rewind &#8211;target-pgdata=\/pgdata\/12\/data &#8211;source-server=&#8221;host=192.168.9.129 port=5433 user=postgres&#8221;<br \/>\npg_rewind: servers diverged at WAL location 0\/E0000D8 on timeline 1<br \/>\npg_rewind: rewinding from last common checkpoint at 0\/E000028 on timeline 1<\/p>\n<p>pg_rewind: Done!<\/p><\/blockquote>\n<p>If you get an error about could not found WAL records, you can copy manually wal file from promoted standby database to old primary database&#8217;s pg_wal directory. If you do not want to get error message like above, you should configure restore_command parameter.<\/p>\n<p><strong>3- Create standby.signal File<\/strong><\/p>\n<blockquote><p>touch standby.signal<\/p><\/blockquote>\n<p><strong>4- Configure postgresql.auto.conf File on Old Primary Database<\/strong><br \/>\nChange host and port parameter like below. These are promoted standby database information.<\/p>\n<blockquote><p># Do not edit this file manually!<br \/>\n# It will be overwritten by the ALTER SYSTEM command.<br \/>\nprimary_conninfo = &#8216;user=repuser password=yourpass host=192.168.9.129 port=5433 sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any&#8217;<\/p><\/blockquote>\n<p><strong>5- Configure postgresql.conf File on Old Primary Database<\/strong><\/p>\n<blockquote><p>port = 5432<\/p><\/blockquote>\n<p>Also, If you use, you can configure archive_command and restore_command parameters. <\/p>\n<p><strong>6- Start Old Primary Database Service<\/strong><\/p>\n<blockquote><p>systemctl start postgresql-pri.service<\/p><\/blockquote>\n<p>Finally, your old primary database is standby database now and follow up to promoted standby database. You can test :)<\/p>\n<p>Source : postgresql.org<\/p>\n","protected":false},"excerpt":{"rendered":"<p>pg_rewind is a PostgreSQL tool since 9.5 version. It provides synchronization between standby and old primary. Especially, pg_rewind provides advantage for big databases. You can sync databases without using pg_basebackup. pg_rewind scans the old primary database&#8217;s pgdata folder and identifies data blocks changed during the switch to standby database, then copies only changed blocks from&#8230;<\/p>\n","protected":false},"author":37,"featured_media":2762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10],"tags":[93,88],"class_list":["post-2761","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administration-postgresql","tag-postgresql","tag-postgresql-administration"],"jetpack_featured_media_url":"https:\/\/www.fatihacar.com\/blog\/resimler\/postgresql_pg_rewind.jpg","jetpack_shortlink":"https:\/\/wp.me\/p39NFI-Ix","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/comments?post=2761"}],"version-history":[{"count":7,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2761\/revisions"}],"predecessor-version":[{"id":2769,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/posts\/2761\/revisions\/2769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media\/2762"}],"wp:attachment":[{"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/media?parent=2761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/categories?post=2761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fatihacar.com\/blog\/wp-json\/wp\/v2\/tags?post=2761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}