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
Leveraging Enteros and AIOps to Optimize Data Lake Performance in the Manufacturing Industry
- 15 May 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Banking Infrastructure with Enteros: Enhancing Database Performance and Cloud Resource Efficiency through Cloud FinOps
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Driving Tech-Sector Efficiency with Enteros: Cost Allocation, Database Performance, RevOps, and Cloud FinOps Synergy
- 14 May 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enhancing Manufacturing Efficiency with Enteros: Forecasting Big Data Trends Through AIOps and Observability Platforms
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…