<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arvind Singh on Medium]]></title>
        <description><![CDATA[Stories by Arvind Singh on Medium]]></description>
        <link>https://medium.com/@arvindsaket?source=rss-470f00e1c98e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tqvbb0RUQ3HcNTix97PGtQ.jpeg</url>
            <title>Stories by Arvind Singh on Medium</title>
            <link>https://medium.com/@arvindsaket?source=rss-470f00e1c98e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:05:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arvindsaket/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Migrating PostgreSQL to Aurora PostgreSQL with near Zero Downtime]]></title>
            <link>https://medium.com/arvind-blogs/migrating-self-managed-postgresql-9-6-to-aws-aurora-postgresql-12-7-or-13-4-ff8f6d42434d?source=rss-470f00e1c98e------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff8f6d42434d</guid>
            <dc:creator><![CDATA[Arvind Singh]]></dc:creator>
            <pubDate>Mon, 03 Jan 2022 22:22:06 GMT</pubDate>
            <atom:updated>2022-03-11T23:55:08.294Z</atom:updated>
            <content:encoded><![CDATA[<p>We will talk about the migration of self managed PostgreSQL 9.6 databases to AWS Aurora PostgreSQL 12.7.</p><p>We were running and maintaining PostgreSQL 9.6 on an AWS EC2 instance. Since PostgreSQL 9.6 was going End of Life, we have to upgrade the PostgreSQL to higher version i.e. to PostgreSQL 12.7, also we have to migrate the PostgreSQL to Aurora PostgreSQL. We did both the tasks in a single go.</p><h3><strong>Use Cases</strong></h3><p>We have to migrate and upgrade the PostgreSQL cluster with the following use cases:</p><ul><li>PostgreSQL cluster with multiple single schema databases.</li><li>PostgreSQL clusters the high number of schema (in thousands) databases with a high number of tables in each schema.</li><li>PostgreSQL clusters multiple databases with very high transactional activity (write operation).</li></ul><h3><strong>Benefits of Aurora PostgreSQL</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*DcpFXdE_kFlT6eA-.jpg" /></figure><h4>Aurora Connection Endpoints</h4><ul><li>Aurora involves a cluster of DB instances instead of a single instance</li><li>Endpoint refers to an intermediate handler with the hostname and port specified to connect to the cluster</li><li>Aurora uses the endpoint mechanism to abstract these connections</li></ul><blockquote>Cluster endpoint</blockquote><ul><li><strong>Cluster endpoint (or writer endpoint)</strong> for an Aurora DB cluster connects to the current primary DB instance for that DB cluster.</li><li>Cluster endpoint is the only one that can perform write operations such as DDL statements as well as read operations</li><li>Each Aurora DB cluster has one cluster endpoint and one primary DB instance</li><li>Cluster endpoint provides fail-over support for reading/writing connections to the DB cluster. If the current primary DB instance of a DB cluster fails, Aurora automatically fails over to a new primary DB instance. During a fail-over, the DB cluster continues to service connection requests to the cluster endpoint from the new primary DB instance, with minimal interruption of service.</li></ul><blockquote>Reader endpoint</blockquote><ul><li><strong>Reader endpoint </strong>for an Aurora DB cluster provides load-balancing support for read-only connections to the DB cluster.</li><li>Use the reader endpoint for reading operations, such as queries.</li><li>Reader endpoint reduces the overhead on the primary instance by processing the statements on the read-only Aurora Replicas.</li><li>Each Aurora DB cluster has one reader endpoint.</li><li>If the cluster contains one or more Aurora Replicas, the reader endpoint load-balances each connection request among the Aurora Replicas.</li></ul><blockquote>Custom endpoint</blockquote><ul><li><strong>The custom endpoint</strong> for an Aurora cluster represents a set of DB instances that you choose.</li><li>Aurora performs load balancing and chooses one of the instances in the group to handle the connection.</li><li>An Aurora DB cluster has no custom endpoints until one is created and up to five custom endpoints can be created for each provisioned Aurora cluster.</li><li>Aurora Serverless clusters do not support custom endpoints</li></ul><blockquote>Instance endpoint</blockquote><ul><li><strong>An instance endpoint </strong>connects to a specific DB instance within an Aurora cluster and provides direct control over connections to the DB cluster.</li><li>Each DB instance in a DB cluster has its own unique instance endpoint. So there is one instance endpoint for the current primary DB instance of the DB cluster, and there is one instance endpoint for each of the Aurora Replicas in the DB cluster</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/0*32v4AwTfhdSB8ynw.png" /></figure><h4>High Performance and Consistent</h4><ul><li>The performance degrades in time in community edition PostgreSQL if the load increases. This happens because of the synchronous replication between replicas. However, due to its unique storage design, Aurora’s performance remains consistent when the load increases.</li><li>Aurora writes logs directly to the storage without keeping log buffers. The replication to the replicas is asynchronous and for only cached data. Because the replicas also share the same storage cluster, the replica lag is small and consistent over time.</li><li>In Aurora, the main reason for using a read replica is to achieve high-availability and sometimes read scalability. Because, the storage cluster is separate from the DB instances. We don’t need a read replica to replicate the data. The data is durable by design.</li><li>Aurora is designed to offer greater than 99.99% availability</li><li>Aurora provides data durability and reliability<br>* by replicating the database volume six ways across three Availability Zones in a single region<strong><br>* </strong>backing up the data continuously to AWS S3.</li><li>Aurora transparently recovers from physical storage failures; instance fail-over typically takes less than 30 seconds.</li><li>If the primary DB instance fails, Aurora automatically fails over to a new primary DB instance, by either promoting an existing Aurora Replica to a new primary DB instance or creating a new primary DB instance</li><li>Aurora is designed to transparently handle <br>* the loss of up to two copies of data without affecting database write availability and<br>* up to three copies without affecting read availability</li><li>Aurora storage is also self-healing. Data blocks and disks are continuously scanned for errors and repaired automatically.</li><li>To increase database availability, 1 to 15 replicas can be created in any of 3 AZs, and RDS will automatically include them in fail-over primary selection in the event of a database outage.</li></ul><h4>Faster Fail-over</h4><ul><li>Aurora automatically fails over, if the primary instance in a DB cluster fails, in the following order:<br>* If Aurora Read Replicas are available, promote an existing Read Replica to the new primary instance.<br>* If no Read Replicas are available, then create a new primary instance.</li><li>If there are multiple Aurora Read Replicas, the criteria for promotion are based on the priority defined for Read Replicas.<br>* Priority number can vary from 0 to 15 and can be modified any time.<br>* Aurora PostgreSQL promotes the Aurora Replica with the highest priority to the new primary instance.<br>* For Read Replicas with same priority, Aurora PostgreSQL promotes the replica that is largest in size or in an arbitrary manner.</li><li>Aurora does not keep a log buffer on the instances. Instead, Aurora keeps the logs at the storage which is separate from the DB instances.</li><li>During the fail-over, AWS modifies the cluster endpoint to point to the newly created/promoted DB instance.</li><li>Applications experience minimal interruption of service if they connect using the cluster endpoint and implement connection retry logic.</li><li>In case of a Global Database disaster or an outage, one of the cluster in a secondary AWS Regions can be promoted to take full read/write workloads in under a min. Therefore, during a fail-over, only the DNS propagation matters. DNS propagation takes around 30 seconds. So the fail-over is also faster, providing more high availability to your applications</li></ul><h4>Higher Security</h4><ul><li>Using IAM can control who can access Aurora DB cluster.</li><li>At the VPC level — (Aurora cluster should be created within a VPC)</li><li>Encryption — The Encryption is handled at in transit and at rest. However, we cannot encrypt an already created cluster. But we can restore an unencrypted cluster snapshot as an encrypted one. Once we encrypt the cluster, we cannot decrypt it as well</li><li>Aurora uses SSL (AES-256) to secure the connection between the database instance and the application</li><li>Aurora allows database encryption using keys managed through AWS Key Management Service (KMS).</li><li>Encryption and decryption are handled seamlessly.</li><li>With Aurora encryption, data stored at rest in the underlying storage is encrypted, as are its automated backups, snapshots, and replicas in the same cluster.</li><li>Encryption — The Encryption is handled at in transit and at rest. However, we cannot encrypt an already created cluster. But we can restore an unencrypted cluster snapshot as an encrypted one. Once we encrypt the cluster, we cannot decrypt it as well</li><li>Using IAM can control who can access Aurora DB cluster</li><li>At the VPC level — (Aurora cluster should be created within a VPC)</li></ul><h4>Scaling</h4><ul><li>Aurora provides more reliability in terms of storage. Its database storage is separate from the instances.</li><li>The data has 6 copies in Aurora as 10GB chunks distributed to 3 Availability Zones. Hence, even if we have only one Aurora instance, the data will still have 6 copies.</li><li>Aurora storage scaling in built-in and will automatically grow, up to 128 TB (soft limit), in 10GB increments with no impact to database performance. There is no need to provision storage in advance</li><li><strong>Compute Scaling</strong></li><li>Instance scaling<br>* Vertical scaling the master the instance. Memory and CPU resources are modified by changing the DB Instance class.<br>* Scaling the read replica and promoting it to master using forced fail-over which provides a minimal downtime</li><li>Read scaling<br>* Provides horizontal scaling with up to 15 read replicas</li></ul><h4>Backup and Restore</h4><ul><li>Automated backups are always enabled on Aurora DB Instances.</li><li>Backups do not impact database performance.</li><li>Aurora also allows creation of manual snapshots</li><li>If in any case the data is unavailable within Aurora storage,<br>* DB Snapshot can be restored or<br>* Point-in-time restore operation can be performed to a new instance. Latest restorable time for a point-in-time restore operation can be up to 5 minutes in the past.</li><li>Restoring a snapshot creates a new Aurora DB instance</li><li>Deleting Aurora database deletes all the automated backups (with an option to create a final snapshot), but would not remove the manual snapshots.</li><li>Snapshots (including encrypted ones) can be shared with another AWS accounts</li><li>Backups can be retained upto 90 days. Minimum retention period is 1 day.</li></ul><h4>Global Database</h4><ul><li>Aurora global database consists of one primary AWS Region where the data is mastered, and up to five read-only, secondary AWS Regions.</li><li>Aurora cluster in the primary AWS Region where your data is mastered performs both read and write operations. The clusters in the secondary Regions enable low-latency reads.</li><li>Aurora replicates data to the secondary AWS Regions with a typical latency of under a second.</li><li>Secondary clusters can be scaled independently by adding one or more DB instances (Aurora Replicas) to serve read-only workloads.</li><li>Aurora global database uses dedicated infrastructure to replicate the data, leaving database resources available entirely to serve applications.</li><li>Applications with a worldwide footprint can use reader instances in the secondary AWS Regions for low-latency reads.</li><li>In case of a disaster or an outage, one of the cluster in a secondary AWS Regions can be promoted to take full read/write workloads in under a min.</li></ul><h3>Goal</h3><p>Migrate and upgrade self managed PostgreSQL-9.6 to Aurora PostgreSQL 12.7 with minimal or almost zero downtime.</p><p>Our application are discovering the database credential and endpoint from the Vault and Consul.</p><h3>Different POC’s performed</h3><h4>“pg_dump” and “pg_restore”</h4><ul><li>For simple and small databases, this option is the best and safest method to migrate the PostgreSQL to new PostgreSQL cluster or versions.</li><li>This method requires a complete downtime. If production environment ca afford the complete downtime then this is best option.</li><li>This method is not for the PostgreSQL cluster which has high number of schema (number of schemas in 1000s). And SLA is in 4 nines.</li></ul><h4>AWS Data Migration Service (DMS)</h4><ul><li>This option is good for the simple database cluster.</li><li>With DMS we can achieve minimal or almost zero downtime migration (condition applies).</li><li>DMS completely failed for the very high schema database with large number of tables. In our case number os schemas varies from 7000 to 15000 and each schema with 200 tables. So around 3 million DB objects, actual data or number of records are other than the DB objects.</li><li>Two step migration and costlier then the native PG tools.</li></ul><h4>“pg_logical” Extension</h4><ul><li>Suitable for any kind of DB architecture.</li><li>Minimal or zero downtime migration can be achieved.</li><li>Support PG 9.6 migration to any higher version.</li><li>Very cost and times efficient.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*jpgWoyii8quRMZFMzWu0yw.jpeg" /></figure><h3>PG Migration using “pg_logical” Extension</h3><h4>Overview</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*KPTiEmkCyiw4x5RvLJM2Ow.jpeg" /></figure><h4>Advantage</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5jMLlju9dHQWeW0t5SpBiA.png" /></figure><h4>Must Have</h4><p>Each table of each databases and schema should have primary keys.</p><h4>Data Migration Activity</h4><p>Data Migration activity is classified into below section or steps:</p><p><strong>Activity to be performed before cut over or flip or Day minus N</strong></p><p><strong>Infrastructure</strong><br>- Install PG Logical plugin to the source self managed PostgreSQL cluster on both master and slave PostgreSQL nodes.<br>- Configure the source PostgreSQL following parameters and values in the postgresql.conf configuration file:</p><pre><br>wal_level = logical <br>max_replication_slots = 10<br>max_wal_senders = 10<br>wal_sender_timeout = 0<br>max_worker_processes = 10</pre><p>- Reload PostgreSQL configuration on <br>psql -d source -p 5432 -c “select pg_reload_conf();&quot;</p><p>- Restart the source PostgreSQL service. (Considering PostgreSQL deployment is as Primary and Secondary nodes and secondary os replicating with primary PostgreSQL nodes.)<br>- Provision the Aurora PostgreSQL with the desired version.<br>- Configure the following parameter in the Aurora cluster parameter group</p><pre>rds.logical_replication = 1<br>shared_preload_libraries = ‘pglogical’</pre><p>- Verify the PostgreSQL configuration at source and target:</p><pre><strong>Source: <br></strong>psql -d source -p 5432 -c &quot;select name, setting from pg_settings where name in (&#39;wal_level&#39;,&#39;track_commit_timestamp&#39;,&#39;max_worker_processes&#39;,&#39;max_replication_slots&#39;,&#39;max_wal_senders  &#39;,&#39;shared_preload_libraries&#39;);&quot;</pre><pre><strong>Target: <br></strong>psql -h &lt;your_rds_endpoint&gt; -d target -U postgres -W -c &quot;select name, setting from pg_settings where name in (&#39;wal_level&#39;,&#39;track_commit_timestamp&#39;,&#39;max_worker_processes&#39;,&#39;max_replication_slots&#39;,&#39;max_wal_senders  &#39;,&#39;shared_preload_libraries&#39;);&quot;</pre><p><strong>Create Schema dump of each Database</strong></p><p>Before creating the “pg_logical” publisher please take the all databases schema dump using native tool “pg_dump”</p><pre>pg_dump -h &lt;hostname&gt; -j 10 -F d -f &lt;filename&gt; &lt;DB name&gt; -U &lt;username&gt;</pre><p><strong>PG Logical Publisher/Provider creation<br></strong>1. Create pglogical extension for each Database on source. Connect to each databases and create pglogical extension<br><strong>create</strong> extension pglogical;</p><p>2. Verify pglogical extension for each database:</p><pre><br><strong>SELECT</strong> e.extname <strong>AS</strong> &quot;Name&quot;, e.extversion <strong>AS</strong> &quot;Version&quot;, n.nspname <strong>AS</strong> &quot;Schema&quot;, c.description <strong>AS</strong> &quot;Description&quot; <strong>FROM</strong> pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n <strong>ON</strong> n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c <strong>ON</strong> c.objoid = e.oid AND c.classoid = &#39;pg_catalog.pg_extension&#39;::pg_catalog.regclass <strong>where</strong> e.extname=&#39;pglogical&#39; <strong>ORDER</strong> <strong>BY</strong> 1;</pre><p>3. Create pglogical node on the source for each database</p><pre><strong>SELECT</strong> pglogical.create_node( node_name := &#39;provider1&#39;, dsn := &#39;host=&lt;your_ec2_endpoint&gt; port=5432 dbname=source1 user=postgres&#39; );</pre><p>4. Verify pglogical node for each database</p><pre><strong>select</strong> * <strong>from</strong> pglogical.node_interface;</pre><p>5. Create “replication_set”. We can create multiple replication set while creating the publisher or we can use the pre existing “default” replication set. In our case we create multiple replication set to distribute the number of schema within the created replication set.</p><pre>select pglogical.create_replication_set(set_name := ‘&lt;replication_set_name&gt;’);</pre><p>6. Add all tables to the replication set of each of the database.</p><pre><strong>SELECT</strong> pglogical.replication_set_add_all_tables (&#39;&lt;replication_set_name&gt;&#39;, &#39;{&lt;schema_name&gt;}&#39;::text[], synchronize_data := <strong>true</strong>);</pre><p>7. Add all sequences to the replication set of each of the database.</p><pre><strong>select</strong> pglogical.replication_set_add_all_sequences( set_name := &#39;&lt;replication_set_name&gt;&#39;, schema_names := &#39;{&lt;schema_name&gt;}&#39;::text[], synchronize_data := <strong>true</strong> );</pre><p><strong>Restore Schema of databases</strong></p><p>Create all the database with the same permission as present on the source PostgreSQL cluster.</p><p>Restore the schema dump takes as above.</p><pre>psql -h &lt;endpoint&gt; -d &lt;database_name&gt; -U &lt;pg_user_name&gt; -f &lt;pg_dump&gt;.dump</pre><p>1. Create pglogical extension for each Database on target</p><pre>create extension pglogical;</pre><p>2. Create pglogical node on the target for each database</p><pre><strong>SELECT</strong> pglogical.create_node( node_name := &#39;&lt;node_name&gt;&#39;, dsn := &#39;host=&lt;Endpoint name&gt; port=5432 dbname=&lt;db name&gt; user=&lt;user_name&gt; password &lt;pwd name&gt;&#39; );</pre><p>3. Create Subscription on the target cluster for each database:</p><pre>SELECT pglogical.create_subscription (subscription_name := &#39;&lt;subscription_name&gt;&#39;, replication_sets := array[&#39;&lt;source replication set name&gt;&#39;], provider_dsn := &#39;host=&lt;source host name&gt; port=5432 dbname=&lt;source DB name&gt; password=&lt;source PG connection pwd&gt; user=&lt;Source PG user name&gt;&#39;);</pre><p>4. Verify subscription for each database on the target Aurora PostgreSQL. Subscription status should not be in down state. It should be as “initialization” or “replicating” state.</p><pre>select * from pglogical.show_subscription_status(subscription_name := ‘&lt;subscription_name&gt;’);</pre><p><strong>Check Replication status on source</strong></p><p>To check the replication status on the source PG</p><pre>select * from pg_replication_slots;</pre><p>“active” column should be “t” state. And verify the “restart_lsn” and “confirmed_flush_lsn” for each replication slots.</p><p><strong>Activity to be perform at the time of flip</strong></p><p>At this point the target PG cluster will be replicating from the source and it should be in sync with source. Now the time is to flip the application to new target i.e. Aurora POstgreSQL cluster.</p><p><strong>Sync PostgreSQL Sequences</strong></p><p>Just before flipping to the new target synchronize all the PG sequences between source and target.</p><pre>select pglogical.synchronize_sequence( seqoid ) from pglogical.sequence_state;</pre><p><strong>Flip and verify</strong></p><p>After the “sequence” sync immediately perform the flip i.e. point your application to the new target.</p><p>And verify your application is connected to the new target and performing as expected.</p><p><strong>Stop replication</strong></p><p>After the application flip and verification stop the replication between source and target. Drop the “pg_logical” replication on the target i.e. on Aurora PostgreSQL side.</p><pre>SELECT pglogical.drop_subscription ( subscription_name := ‘your_subscription_name’);</pre><h3>Limitations</h3><ol><li>Temporary tables cannot be replicated.</li><li>DDLs replication cannot be done automatically.</li><li>“pg_logical” replication require super user access for its administration.</li></ol><h3>Conclusion</h3><p>PostgreSQL database migration through “pg_logical” is the best and most reliable method despite of multiple stages to be performed for the migration.</p><p>It provide the convenience to reduce the migration downtime. “pg_logical” provides more feasible and easier means to replicate your tables than the standard streaming replication mechanism.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff8f6d42434d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/arvind-blogs/migrating-self-managed-postgresql-9-6-to-aws-aurora-postgresql-12-7-or-13-4-ff8f6d42434d">Migrating PostgreSQL to Aurora PostgreSQL with near Zero Downtime</a> was originally published in <a href="https://medium.com/arvind-blogs">Arvind Singh</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>