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
Reinventing Retail Efficiency: How Enteros Transforms Database Performance Optimization Through Advanced Observability Platforms
- 16 November 2025
- Database Performance Management
Introduction The retail industry has undergone a dramatic digital acceleration over the past decade. From omnichannel commerce and real-time inventory visibility to personalized marketing and dynamic pricing, data now dictates every major decision in retail. Retailers no longer compete only on product or price—they compete on speed, accuracy, personalization, and operational efficiency, all of which … Continue reading “Reinventing Retail Efficiency: How Enteros Transforms Database Performance Optimization Through Advanced Observability Platforms”
Revolutionizing Manufacturing RevOps Efficiency: How Enteros Combines Generative AI and Cloud FinOps to Transform Performance Management
Introduction The manufacturing industry is undergoing a major transformation driven by digitalization, Industry 4.0, automation, and data intelligence. From smart factories and IoT-enabled equipment to cloud-based production planning and real-time supply chain analytics, every aspect of modern manufacturing is powered by data. This shift has created unprecedented opportunities—but has also increased operational complexity, making performance … Continue reading “Revolutionizing Manufacturing RevOps Efficiency: How Enteros Combines Generative AI and Cloud FinOps to Transform Performance Management”
Transforming BFSI Cloud Efficiency: How Enteros Enhances Budgeting and Governance Through the Cloud Center of Excellence
- 13 November 2025
- Database Performance Management
Introduction In the fast-evolving world of Banking, Financial Services, and Insurance (BFSI), cloud transformation has become more than a technological upgrade—it’s a strategic imperative. As financial institutions adopt multi-cloud and hybrid ecosystems, managing cost efficiency, compliance, and performance governance becomes increasingly complex. To meet these challenges, many organizations are establishing Cloud Centers of Excellence (CCoE)—dedicated … Continue reading “Transforming BFSI Cloud Efficiency: How Enteros Enhances Budgeting and Governance Through the Cloud Center of Excellence”
Revolutionizing Healthcare Efficiency: How Enteros Integrates AIOps and Cloud FinOps to Transform Database Performance Management
Introduction In the modern era of digital healthcare, data drives everything—from clinical decision-making and patient outcomes to financial operations and regulatory compliance. As healthcare organizations increasingly adopt cloud technologies, electronic health records (EHRs), and AI-based analytics, the volume and complexity of data have grown exponentially. This digital transformation has brought immense potential for innovation but … Continue reading “Revolutionizing Healthcare Efficiency: How Enteros Integrates AIOps and Cloud FinOps to Transform Database Performance Management”