Set all sequences to a higher value when promoting a PostgreSQL logical replica
Project description
pg-sequence-increaser
Set all sequences to a higher value when promoting a PostgreSQL logical replica.
Requirements
- Python3
libpq-dev
(for psycopg2). On Mac OS,brew install postgresql
, on Ubuntu, installlibpq-dev
.
Installation
Production
$ pip install pg-sequence-increaser
Development
Using virtualenv,
$ pip install -r requirements.txt
Usage
This tool implements two strategies:
- Getting the sequences and their desired values from the logical replica alone (called "max-id-on-replica"),
- getting the sequences and their desired values from the primary database, i.e. the publisher; this is called "primary-sequences".
Max Id on Replica
We get the sequences from information_schema.default_value
. Any default_value
that has nextval('some_sequence_name')
, we assume that's a sequence and extract the table name, the associated column and the sequence name. Then, we run MAX(column_name)
to get the last value inserted into that table, likely from the sequence; we then move the sequence forward to the MAX(column_name) + increment_by
. This strategy has the advantage of using the replica as the source of truth and not requiring the publisher to be online.
$ pgseqmover --replica-url=postgres://localhost:5432/my_db --queries-only
will print the queries you can then run manually. Those queries will increase all sequences by 1000.
Primary Sequences
We get the sequences from the primary (publisher). After all, that's where we are replicating from in the first place. We use pg_sequences
view and get both the name and the last_value
from there as well. We then set the sequences on the replica to what they are on the primary + increment_by
. This strategy has the advantage of using the primary database as the source of truth in case replica lag is non-zero during promotion which is bad, but possible. This requires the primary to be available during promotion of the replica.
$ pgseqmover --replica-url=postgres://replica-db:5432/my_db --primary-url=postgres://primary-db:5432/my_db --queries-only --strategy=primary-sequences
will print the queries you can then run manually. Those queries will increase all sequences by 1000.
Arguments
This also accepts optional arguments:
--dry-run
which will show the queries it will run but not actually do anything; this is the default behavior,--debug
will show all queries it's running; can be combined with--dry-run
,--increment-by
overrides the default increase by value of 1000 to any value (even negative ones).--strategy
picks the strategy to use for increasing sequences. Your options are:primary-sequences
andmax-id-on-replica
, both explained above.--queries-only
will only print the queries that will move the sequences and not do anything else. Less pretty version of--dry-run
, but good forpgseqmover --queries-only > ~/Desktop/queries_to_run.sql
use case.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pg-sequence-increaser-0.2.tar.gz
.
File metadata
- Download URL: pg-sequence-increaser-0.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a3f0fed8f207bd6597ccaf500eef8078d7712edf22e90b94482e0055cd8b26e |
|
MD5 | 43701620c295ec6860b18a470aaeece2 |
|
BLAKE2b-256 | 360cc9dc2f5e8a22f2b23441fce46108f48f7d3f993387f0383a039f62b54123 |
File details
Details for the file pg_sequence_increaser-0.2-py3-none-any.whl
.
File metadata
- Download URL: pg_sequence_increaser-0.2-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20101a146680885f95de69dfd0979307d5947b79ad3e36fe2144252fba775431 |
|
MD5 | f4b885eaea0cf3eeeab4b550ab438046 |
|
BLAKE2b-256 | e6eb007c70c88bd71a26fa41d7174ec06a79e323c156da12bfe06fe2788fbbb4 |