Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

pgslice

> 编程语言
Open source

Postgres partitioning as easy as pie

1.2K stars0 likes3 views
WebsiteGitHub

About

Postgres partitioning as easy as pie

pgslice

Postgres partitioning as easy as pie. Works great for both new and existing tables, with zero downtime and minimal app changes. No need to install anything on your database server. Archive older data on a rolling basis to keep your database size under control.

:tangerine: Battle-tested at Instacart

Install

pgslice is a command line tool. To install, run:

bash
gem install pgslice

This will give you the pgslice command. If installation fails, you may need to install dependencies.

You can also install it with Homebrew or Docker.

Steps

  1. Ensure the table you want to partition has been created. We’ll refer to this as ``.

  2. Specify your database credentials

bash
export PGSLICE_URL=postgres://localhost/myapp_development
  1. Create an intermediate table
bash
pgslice prep   

The column should be a timestamp, timestamptz, or date column and period can be day, month, or year.

This creates a partitioned table named _intermediate using range partitioning.

  1. Add partitions to the intermediate table
bash
pgslice add_partitions  --intermediate --past 3 --future 3

Use the --past and --future options to control the number of partitions.

  1. Optional, for tables with data - Fill the partitions in batches with data from the original table
bash
pgslice fill 

Use the --batch-size and --sleep options to control the speed (defaults to 10000 and 0 respectively)

To sync data across different databases, check out pgsync.

  1. Analyze tables
bash
pgslice analyze 
  1. Swap the intermediate table with the original table
bash
pgslice swap 

The original table is renamed _retired and the intermediate table is renamed ``.

  1. Fill the rest (rows inserted between the first fill and the swap)
bash
pgslice fill  --swapped
  1. Back up the retired table with a tool like pg_dump and drop it
sql
pg_dump -c -Fc -t _retired $PGSLICE_URL > _retired.dump
psql -c "DROP TABLE _retired" $PGSLICE_URL

Sample Output

pgslice prints the SQL commands that were executed on the server. To print without executing, use the --dry-run option.

bash
pgslice prep visits created_at month
sql
BEGIN;

CREATE TABLE "public"."visits_intermediate" (LIKE "public"."visits" INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING STORAGE INCLUDING COMMENTS INCLUDING STATISTICS INCLUDING GENERATED INCLUDING COMPRESSION) PARTITION BY RANGE ("created_at");

CREATE INDEX ON "public"."visits_intermediate" USING btree ("created_at");

COMMENT ON TABLE "public"."visits_intermediate" is 'column:created_at,period:month,cast:date,version:3';

COMMIT;
bash
pgslice add_partitions visits --intermediate --past 1 --future 1
…
bash
pgslice fill visits
sql
/* 1 of 3 */
INSERT INTO "public"."visits_intermediate" ("id", "user_id", "ip", "created_at")
    SELECT "id", "user_id", "ip", "created_at" FROM "public"."visits"
    WHERE "id" > 0 AND "id" = '2026-01-01'::date AND "created_at"  10000 AND "id" = '2026-01-01'::date AND "created_at"  20000 AND "id" = '2026-01-01'::date AND "created_at"  _202601.dump
psql -c "DROP TABLE _202601" $PGSLICE_URL

If you use Amazon S3 for backups, s3cmd is a nice tool.

bash
s3cmd put _202601.dump s3:///_202601.dump

Schema Updates

Once a table is partitioned, make schema updates on the master table only (not partitions). This includes adding, removing, and modifying columns, as well as adding and removing indexes and foreign keys.

Additional Commands

To undo prep (which will delete partitions), use:

bash
pgslice unprep 

To undo swap, use:

bash
pgslice unswap 

Additional Options

Set the tablespace when adding partitions

bash
pgslice add_partitions  --tablespace fastspace

App Considerations

This set up allows you to read and write with the original table name with no knowledge it’s partitioned. However, there are a few things to be aware of.

Reads

When possible, queries should include the column you partition on to limit the number of partitions the database needs to check. For instance, if you partition on created_at, try to include it in queries:

sql
SELECT * FROM
    visits
WHERE
    user_id = 123 AND
    -- for performance
    created_at >= '2026-01-01' AND created_at  1000" # use any conditions
pgslice analyze 
pgslice swap 
pgslice fill  --where "id > 1000" --swapped

Triggers

Triggers aren’t copied from the original table. You can set up triggers on the intermediate table if needed.

Data Protection

Always make sure your connection is secure when connecting to a database over a network you don’t fully trust. Your best option is to connect over SSH or a VPN. Another option is to use sslmode=verify-full. If you don’t do this, your database credentials can be compromised.

Additional Installation Methods

Homebrew

With Homebrew, you can use:

bash
brew install pgslice

Docker

Get the Docker image with:

bash
docker pull ankane/pgslice
alias pgslice="docker run --rm -e PGSLICE_URL ankane/pgslice"

This will give you the pgslice command.

Dependencies

If installation fails, your system may be missing Ruby or libpq.

On Mac, run:

bash
brew install libpq

On Ubuntu, run:

bash
sudo apt-get install ruby-dev libpq-dev build-essential

Upgrading

Run:

bash
gem install pgslice

To use master, run:

bash
gem install specific_install
gem specific_install https://github.com/ankane/pgslice.git

Reference

  • PostgreSQL Manual

Related Projects

Also check out:

  • Dexter - The automatic indexer for Postgres
  • PgHero - A performance dashboard for Postgres
  • pgsync - Sync Postgres data to your local machine

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

  • Report bugs
  • Fix bugs and submit pull requests
  • Write, clarify, or fix documentation
  • Suggest or add new features

To get started with development:

bash
git clone https://github.com/ankane/pgslice.git
cd pgslice
bundle install
createdb pgslice_test
bundle exec rake test

To test against different versions of Postgres with Docker, use:

bash
docker run -p=8000:5432 postgres:16
TZ=Etc/UTC PGSLICE_URL=postgres://postgres@localhost:8000/postgres bundle exec rake

On Mac, you must use Docker Desktop for the port mapping to localhost to work.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言