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
Boosting Digital Transformation Success with Intelligent Database Performance Analytics
- 5 June 2026
- Database Performance Management
Introduction Digital transformation has become a strategic priority for organizations across every industry. From financial services and healthcare to retail, manufacturing, and technology, businesses are investing heavily in cloud computing, data-driven decision-making, artificial intelligence (AI), automation, and customer-centric digital experiences. The goal is clear: improve agility, enhance operational efficiency, increase competitiveness, and deliver exceptional customer … Continue reading “Boosting Digital Transformation Success with Intelligent Database Performance Analytics”
How AI-Driven AIOps Helps Enterprises Detect and Resolve Database Anomalies Faster
Introduction Modern enterprises depend on databases to power virtually every critical business function, from customer-facing applications and e-commerce platforms to financial systems, healthcare services, and cloud-native workloads. As digital transformation accelerates, organizations are generating unprecedented volumes of data and managing increasingly complex database environments spread across on-premises infrastructure, public clouds, hybrid architectures, and distributed systems. … Continue reading “How AI-Driven AIOps Helps Enterprises Detect and Resolve Database Anomalies Faster”
The Future of Autonomous Database Performance Management in Cloud-Native Environments
- 4 June 2026
- Database Performance Management
Introduction As organizations accelerate their digital transformation initiatives, cloud-native architectures have become the foundation of modern application development. Businesses increasingly rely on microservices, containers, Kubernetes, hybrid cloud deployments, and distributed databases to deliver scalable, resilient, and high-performing digital experiences. While cloud-native environments offer tremendous flexibility and scalability, they also introduce unprecedented complexity. Modern applications generate … Continue reading “The Future of Autonomous Database Performance Management in Cloud-Native Environments”
Improving Enterprise Application Reliability with Proactive Database Observability
Introduction Enterprise applications are the backbone of modern organizations. Whether supporting customer-facing digital services, financial transactions, e-commerce operations, supply chain management, healthcare systems, or internal business processes, application performance directly impacts customer satisfaction, operational efficiency, and revenue generation. However, as enterprise environments become increasingly complex, maintaining application reliability has become a significant challenge. Modern applications … Continue reading “Improving Enterprise Application Reliability with Proactive Database Observability”