PostgreSQL has a feature called data checksum which is used to detect any storage-level corruption of data pages. However, a new PostgreSQL database cluster does not have this feature enabled by default. To activate this feature, you must specify the –data-checksums flag when initializing a new PostgreSQL database cluster with the initdb utility.

So what about clusters that are already running? Is there any way to enable data checksums for an existing cluster? Previously, the only way was to initialize a new cluster with data checksums and dump the database into it using dump-restore or logical replication. Unfortunately, these methods are slow. Starting from PostgreSQL 12, users can utilize a new built-in utility called ‘pg_checksum‘ to enable checksums in a cluster that was not initialized with this feature. However, this utility only works when the database server is offline. For a large production server, it may be difficult to schedule a significant amount of downtime. To avoid this issue, a primary-standby architecture can be created. Below are the steps that can be followed to achieve this.

1. Please create a set-up consisting of a primary and standby server using streaming replication to ensure data redundancy and availability. To set up a primary-standby system using streaming replication, please refer to How to Set Up Streaming Replication in PostgreSQL 12 for detailed instructions. Once you have built this setup, you can use the following statement to verify that the replication is working correctly.

2. To ensure high availability and minimal downtime, configure an automatic failover/switchover for the primary-standby nodes with any HA tool. In this example, Repmgr has been used as a High Availability (HA) tool. However, the steps for setting up Repmgr have not been discussed in this blog to keep it simple. You can refer to this link for more information on Repmgr.

3. After configuring repmgr correctly, both nodes will be visible in the cluster. In this example, repmgr will be used for switchover.

4. Confirm that the checksum is disabled on the standby node.

5. Stop the standby database cluster.

6. Enable checksum on the standby database cluster.

7. Start the standby database cluster.

8. Verify the status of the checksum on the standby node, now this must be enabled.

9. Confirm that standby is fully synchronized with the primary and there is no lag.

10. Once this gets confirmed there is no lag between primary and standby, perform a dry run for the switchover to confirm everything is perfect.

11. Perform actual switchover.

12. Now the old standby server acts like the new primary node.

13. Stop the new standby (ex-primary).

14. Enable checksum on new standby (ex-primary).

15. Start the new standby (ex-primary).

 16. Verify the status of the checksum on new standby (ex-primary). Now this must be enabled.

17. Check the current cluster status in repmgr.

18. Confirm that the standby is fully synchronized with the new primary and there is no lag.

19. Once this has been confirmed there is no lag, perform switchover again to get the original setup of primary-standby.

20. Validate the cluster output to confirm it reflects the original setup.

Before executing the pg_checksum procedure on a production environment, there are some precautions that should be taken care of:

  • If a lower environment is available, it is recommended to execute pg_checksum on that environment first. This will help in estimating the execution time and the lag generated during the activity.
  • If a lower environment is not available, then it is suggested to build a clone of the Production environment and test it first to get the above-mentioned estimations.

In this blog post, we have discussed the procedure for enabling data checksums on an existing running cluster using pg_checksums with minimum downtime. I hope that this article provides you with informative and helpful insights.

Percona Distribution for PostgreSQL provides the best and most critical enterprise components from the open-source community in a single distribution, designed and tested to work together. Run PostgreSQL in your production and mission-critical environments and easily deploy and orchestrate reliable PostgreSQL in Kubernetes.

Download Percona Distribution for PostgreSQL Today!

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments