Effortless PostgreSQL Table Partitioning with pg_rewrite
Partitioning PostgreSQL tables has quickly become one of the most popular methods to scale and improve database performance. However, partitioning existing tables without locking your database is often a major pain point.
That’s where pg_rewrite comes in—a simple, elegant tool that allows you to repartition PostgreSQL tables with minimal locking. In this guide, you’ll learn how to install and use it.
🔧 Installing pg_rewrite
To get started, clone the open-source project from GitHub:
Make sure the correct version of pg_config is in your path:
Then build and install the extension:
PostgreSQL Configuration
Update your postgresql.conf with the following settings:
Restart PostgreSQL and verify the extension is loaded:
Then create the extension:
🧪 Creating a Sample Table
Now let’s create a test table that we’ll later partition:
Confirm the data distribution:
Make sure the table has a primary key:
🗂 Setting Up Partitioned Tables
Create your partitioned structure. For this example, we’ll split values into negative and positive ranges:
☝ Make sure the primary key and constraints match the original table, or the rewrite will fail.
🚀 Partitioning with Minimal Locking
Run the rewrite operation:
-
t_number: original table -
t_part_number: new partitioned table -
t_old_number: backup of the original (for rollback, if needed)
Check the resulting structure:
You’ll see:
-
t_number(now a partitioned table) -
t_old_number(backup of the original table) -
t_part_number_neg,t_part_number_pos(partition tables)
🎉 Why Use pg_rewrite?
Unlike traditional PostgreSQL partitioning, pg_rewrite minimizes locking by using a rewrite strategy. It’s ideal for large tables and production systems where downtime is unacceptable.
🌍 Bonus: Prebuilt GIS Data for PostgreSQL
We also offer ready-to-import OpenStreetMap (OSM) data for PostgreSQL on our new GIS site. These dumps are ideal for large PostGIS workloads and take full advantage of partitioning.
Check out the GIS resource page →
👨💼 About Enteros
Enteros is a patented database performance management platform that identifies and resolves complex performance issues across a wide range of environments—including SQL, NoSQL, and ML databases—on any cloud or on-prem infrastructure.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
How to Drive eCommerce Revenue Growth with Enteros Growth Management, RevOps Efficiency, and Cloud FinOps
- 8 April 2026
- Database Performance Management
Introduction The eCommerce sector has witnessed explosive growth over the past decade, fueled by digital transformation, mobile shopping, AI-driven personalization, and global online marketplaces. From startups to enterprise retailers, businesses are scaling rapidly to meet rising customer expectations for speed, convenience, and seamless experiences. However, this rapid growth introduces a critical challenge:how to increase revenue … Continue reading “How to Drive eCommerce Revenue Growth with Enteros Growth Management, RevOps Efficiency, and Cloud FinOps”
How to Drive Healthcare Sector Performance Growth with Enteros Database Management and AI SQL Optimization
Introduction The healthcare sector is undergoing a significant digital transformation driven by electronic health records (EHRs), telemedicine, AI-powered diagnostics, and real-time patient monitoring systems. Healthcare organizations are increasingly relying on data to deliver better patient outcomes, improve operational efficiency, and ensure regulatory compliance. However, with the exponential growth of healthcare data comes a major challenge:how … Continue reading “How to Drive Healthcare Sector Performance Growth with Enteros Database Management and AI SQL Optimization”
What to Know About Enteros Cost Attribution and AI Performance Management for Media Sector Growth with Generative AI
- 7 April 2026
- Database Performance Management
Introduction The media sector is undergoing a massive transformation fueled by digital streaming, real-time content delivery, AI-driven personalization, and data-intensive production workflows. From OTT platforms and digital publishing to gaming and broadcasting, media companies are handling enormous volumes of data while striving to deliver seamless user experiences. However, with this rapid growth comes a pressing … Continue reading “What to Know About Enteros Cost Attribution and AI Performance Management for Media Sector Growth with Generative AI”
How to Transform Financial Sector Operations with Enteros Database Management Platform, AIOps, and Cloud FinOps
Introduction The financial sector is undergoing rapid digital transformation driven by mobile banking, real-time payments, algorithmic trading, and AI-powered services. Financial institutions must deliver seamless, secure, and high-performance digital experiences while managing rising infrastructure costs and strict regulatory requirements. At the center of this transformation lies a critical challenge:how to optimize database performance, control cloud … Continue reading “How to Transform Financial Sector Operations with Enteros Database Management Platform, AIOps, and Cloud FinOps”