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
Real Estate IT Economics with Financial Precision: Enteros’ Cost Attribution Intelligence
- 9 February 2026
- Database Performance Management
Introduction Real estate has always been an asset‑heavy, capital‑intensive industry. From commercial portfolios and residential developments to REITs and PropTech platforms, profitability depends on precise financial control. Yet while real estate organizations apply rigorous financial discipline to assets, leases, and investments, their IT and data environments often lack the same level of cost transparency. Modern … Continue reading “Real Estate IT Economics with Financial Precision: Enteros’ Cost Attribution Intelligence”
Managing Database Growth with Financial Precision: Enteros for Tech Leaders
Introduction For technology enterprises, databases are no longer just systems of record—they are engines of innovation. SaaS platforms, AI applications, digital marketplaces, analytics products, and customer-facing services all depend on rapidly growing databases that must scale continuously, remain highly performant, and stay available around the clock. But as database environments grow, so do costs. Cloud … Continue reading “Managing Database Growth with Financial Precision: Enteros for Tech Leaders”
From Performance to Profitability: Enteros Database Intelligence for Real Estate Enterprises
- 8 February 2026
- Database Performance Management
Introduction The real estate sector has undergone a dramatic transformation over the past decade. What was once an asset-heavy, relationship-driven industry is now deeply digital, data-intensive, and platform-centric. Property listing portals, smart building platforms, tenant experience apps, valuation engines, AI-driven pricing models, IoT-enabled facilities management systems, and digital transaction platforms all rely on complex, always-on … Continue reading “From Performance to Profitability: Enteros Database Intelligence for Real Estate Enterprises”
Running Retail on Data: How Enteros Transforms Database Performance Management
Introduction Retail has evolved far beyond physical stores and point-of-sale systems. Today’s retail enterprises operate complex, always-on digital ecosystems that span e-commerce platforms, mobile apps, omnichannel order management, supply chain systems, loyalty programs, personalization engines, and real-time analytics. Every product search, cart update, inventory check, price change, promotion, and payment depends on high-performing databases working … Continue reading “Running Retail on Data: How Enteros Transforms Database Performance Management”