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 Enteros Enables High-Performance Retail Platforms Using AI SQL and GenAI
- 18 January 2026
- Database Performance Management
Introduction Retail has become one of the most data-intensive and performance-sensitive industries in the digital economy. From omnichannel commerce and real-time inventory visibility to personalized recommendations, dynamic pricing, loyalty platforms, and fraud prevention, modern retail experiences depend on complex software ecosystems powered by high-volume databases. Customers now expect instant search results, seamless checkout, personalized experiences, … Continue reading “How Enteros Enables High-Performance Retail Platforms Using AI SQL and GenAI”
How Enteros Enables High-Performance, Cost-Efficient Real Estate Technology Platforms
Introduction The real estate industry has evolved into a technology-driven business. From digital property listings and virtual tours to CRM systems, valuation models, transaction platforms, tenant portals, and analytics dashboards, modern real estate enterprises rely on complex software ecosystems powered by data-intensive databases. At the heart of these platforms lies a fundamental challenge: how to … Continue reading “How Enteros Enables High-Performance, Cost-Efficient Real Estate Technology Platforms”
Accurate Healthcare Cloud Cost Estimation with Enteros: An AIOps-Driven FinOps Approach
- 15 January 2026
- Database Performance Management
Introduction Healthcare organizations are undergoing rapid digital transformation. Electronic health records (EHRs), telemedicine platforms, AI-driven diagnostics, patient engagement portals, population health analytics, and regulatory reporting systems now form the backbone of modern healthcare delivery. At the center of all these innovations lies a complex, data-intensive cloud infrastructure powered by mission-critical databases. While cloud adoption has … Continue reading “Accurate Healthcare Cloud Cost Estimation with Enteros: An AIOps-Driven FinOps Approach”
Why Traditional Banking Database Optimization Falls Short, and How Enteros Fixes It with GenAI
Introduction Modern banking has become a real-time, always-on digital business. From core banking systems and payment processing to mobile apps, fraud detection, risk analytics, and regulatory reporting—every critical banking function depends on database performance. Yet while banking technology stacks have evolved dramatically, database optimization practices have not. Most banks still rely on traditional database tuning … Continue reading “Why Traditional Banking Database Optimization Falls Short, and How Enteros Fixes It with GenAI”