13.9 C
Friday, October 27, 2023

Automate the archive and purge information course of for Amazon RDS for PostgreSQL utilizing pg_partman, Amazon S3, and AWS Glue

The submit Archive and Purge Knowledge for Amazon RDS for PostgreSQL and Amazon Aurora with PostgreSQL Compatibility utilizing pg_partman and Amazon S3 proposes information archival as a essential a part of information administration and reveals learn how to effectively use PostgreSQL’s native vary partition to partition present (scorching) information with pg_partman and archive historic (chilly) information in Amazon Easy Storage Service (Amazon S3). Clients want a cloud-native automated answer to archive historic information from their databases. Clients need the enterprise logic to be maintained and run from outdoors the database to cut back the compute load on the database server. This submit proposes an automatic answer by utilizing AWS Glue for automating the PostgreSQL information archiving and restoration course of, thereby streamlining the complete process.

AWS Glue is a serverless information integration service that makes it simpler to find, put together, transfer, and combine information from a number of sources for analytics, machine studying (ML), and software improvement. There is no such thing as a have to pre-provision, configure, or handle infrastructure. It might additionally mechanically scale sources to fulfill the necessities of your information processing job, offering a excessive stage of abstraction and comfort. AWS Glue integrates seamlessly with AWS companies like Amazon S3, Amazon Relational Database Service (Amazon RDS), Amazon Redshift, Amazon DynamoDB, Amazon Kinesis Knowledge Streams, and Amazon DocumentDB (with MongoDB compatibility) to supply a sturdy, cloud-native information integration answer.

The options of AWS Glue, which embody a scheduler for automating duties, code era for ETL (extract, remodel, and cargo) processes, pocket book integration for interactive improvement and debugging, in addition to strong safety and compliance measures, make it a handy and cost-effective answer for archival and restoration wants.

Answer overview

The answer combines PostgreSQL’s native vary partitioning characteristic with pg_partman, the Amazon S3 export and import capabilities in Amazon RDS, and AWS Glue as an automation software.

The answer entails the next steps:

  1. Provision the required AWS companies and workflows utilizing the offered AWS Cloud Growth Package (AWS CDK) challenge.
  2. Arrange your database.
  3. Archive the older desk partitions to Amazon S3 and purge them from the database with AWS Glue.
  4. Restore the archived information from Amazon S3 to the database with AWS Glue when there’s a enterprise have to reload the older desk partitions.

The answer is predicated on AWS Glue, which takes care of archiving and restoring databases with Availability Zone redundancy. The answer is comprised of the next technical elements:

  • An Amazon RDS for PostgreSQL Multi-AZ database runs in two personal subnets.
  • AWS Secrets and techniques Supervisor shops database credentials.
  • An S3 bucket shops Python scripts and database archives.
  • An S3 Gateway endpoint permits Amazon RDS and AWS Glue to speak privately with the Amazon S3.
  • AWS Glue makes use of a Secrets and techniques Supervisor interface endpoint to retrieve database secrets and techniques from Secrets and techniques Supervisor.
  • AWS Glue ETL jobs run in both personal subnet. They use the S3 endpoint to retrieve Python scripts. The AWS Glue jobs learn the database credentials from Secrets and techniques Supervisor to ascertain JDBC connections to the database.

You may create an AWS Cloud9 surroundings in one of many personal subnets obtainable in your AWS account to arrange take a look at information in Amazon RDS. The next diagram illustrates the answer structure.

Solution Architecture


For directions to arrange your surroundings for implementing the answer proposed on this submit, check with Deploy the applying within the GitHub repo.

Provision the required AWS sources utilizing AWS CDK

Full the next steps to provision the required AWS sources:

  1. Clone the repository to a brand new folder in your native desktop.
  2. Create a digital surroundings and set up the challenge dependencies.
  3. Deploy the stacks to your AWS account.

The CDK challenge contains three stacks: vpcstack, dbstack, and gluestack, carried out within the vpc_stack.py, db_stack.py, and glue_stack.py modules, respectively.

These stacks have preconfigured dependencies to simplify the method for you. app.py declares Python modules as a set of nested stacks. It passes a reference from vpcstack to dbstack, and a reference from each vpcstack and dbstack to gluestack.

gluestack reads the next attributes from the mother or father stacks:

  • The S3 bucket, VPC, and subnets from vpcstack
  • The key, safety group, database endpoint, and database title from dbstack

The deployment of the three stacks creates the technical elements listed earlier on this submit.

Arrange your database

Put together the database utilizing the knowledge offered in Populate and configure the take a look at information on GitHub.

Archive the historic desk partition to Amazon S3 and purge it from the database with AWS Glue

The “Keep and Archive” AWS Glue workflow created in step one consists of two jobs: “Partman run upkeep” and “Archive Chilly Tables.”

The “Partman run upkeep” job runs the Partman.run_maintenance_proc() process to create new partitions and detach outdated partitions primarily based on the retention setup within the earlier step for the configured desk. The “Archive Chilly Tables” job identifies the indifferent outdated partitions and exports the historic information to an Amazon S3 vacation spot utilizing aws_s3.query_export_to_s3. In the long run, the job drops the archived partitions from the database, releasing up cupboard space. The next screenshot reveals the outcomes of operating this workflow on demand from the AWS Glue console.

Archive job run result

Moreover, you’ll be able to arrange this AWS Glue workflow to be triggered on a schedule, on demand, or with an Amazon EventBridge occasion. You could use your small business requirement to pick the suitable set off.

Restore archived information from Amazon S3 to the database

The “Restore from S3” Glue workflow created in step one consists of 1 job: “Restore from S3.”

This job initiates the run of the partman.create_partition_time process to create a brand new desk partition primarily based in your specified month. It subsequently calls aws_s3.table_import_from_s3 to revive the matched information from Amazon S3 to the newly created desk partition.

To start out the “Restore from S3” workflow, navigate to the workflow on the AWS Glue console and select Run.

The next screenshot reveals the “Restore from S3” workflow run particulars.

Restore job run result

Validate the outcomes

The answer offered on this submit automated the PostgreSQL information archival and restoration course of utilizing AWS Glue.

You should use the next steps to verify that the historic information within the database is efficiently archived after operating the “Keep and Archive” AWS Glue workflow:

  1. On the Amazon S3 console, navigate to your S3 bucket.
  2. Affirm the archived information is saved in an S3 object as proven within the following screenshot.
    Archived data in S3
  3. From a psql command line software, use the dt command to record the obtainable tables and make sure the archived desk ticket_purchase_hist_p2020_01 doesn’t exist within the database.List table result after post archival

You should use the next steps to verify that the archived information is restored to the database efficiently after operating the “Restore from S3” AWS Glue workflow.

  1. From a psql command line software, use the dt command to record the obtainable tables and make sure the archived desk ticket_history_hist_p2020_01 is restored to the database.List table results after restore

Clear up

Use the knowledge offered in Cleanup to scrub up your take a look at surroundings created for testing the answer proposed on this submit.


This submit confirmed learn how to use AWS Glue workflows to automate the archive and restore course of in RDS for PostgreSQL database desk partitions utilizing Amazon S3 as archive storage. The automation is run on demand however will be set as much as be trigged on a recurring schedule. It lets you outline the sequence and dependencies of jobs, observe the progress of every workflow job, view run logs, and monitor the general well being and efficiency of your duties. Though we used Amazon RDS for PostgreSQL for instance, the identical answer works for Amazon Aurora-PostgreSQL Suitable Version as effectively. Modernize your database cron jobs utilizing AWS Glue by utilizing this submit and the GitHub repo. Acquire a high-level understanding of AWS Glue and its elements by utilizing the next hands-on workshop.

Concerning the Authors

Anand Komandooru is a Senior Cloud Architect at AWS. He joined AWS Skilled Providers group in 2021 and helps clients construct cloud-native functions on AWS cloud. He has over 20 years of expertise constructing software program and his favourite Amazon management precept is “Leaders are proper lots.”

Li Liu is a Senior Database Specialty Architect with the Skilled Providers workforce at Amazon Internet Providers. She helps clients migrate conventional on-premise databases to the AWS Cloud. She focuses on database design, structure, and efficiency tuning.

Neil Potter is a Senior Cloud Software Architect at AWS. He works with AWS clients to assist them migrate their workloads to the AWS Cloud. He focuses on software modernization and cloud-native design and is predicated in New Jersey.

Vivek Shrivastava is a Principal Knowledge Architect, Knowledge Lake in AWS Skilled Providers. He’s a giant information fanatic and holds 14 AWS Certifications. He’s enthusiastic about serving to clients construct scalable and high-performance information analytics options within the cloud. In his spare time, he loves studying and finds areas for house automation.

Latest news
Related news


Please enter your comment!
Please enter your name here