Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 4153af8

Browse files
committed
feat: add start-postgres-replica
1 parent 569fbb4 commit 4153af8

File tree

6 files changed

+107
-4
lines changed

6 files changed

+107
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ The binary distributions have many extensions enabled; these include:
8686
- pg_tle
8787
- wrappers
8888
- supautils
89+
- citus
8990

9091
You can just use `CREATE EXTENSION` to enable most of these. Some may require
9192
tweaks to [postgresql.conf](./tests/postgresql.conf) to enable.

docs/start-client-server.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Running the server
2+
13
If you want to run a postgres server, just do this from the root of the
24
repository:
35

@@ -18,7 +20,7 @@ valid "flake reference":
1820
nix run github:supabase/nix-postgres#start-server 14
1921
```
2022

21-
## Arbitrary versions at arbitrary git revisions
23+
### Arbitrary versions at arbitrary git revisions
2224

2325
Let's say you want to use a PostgreSQL build from a specific version of the
2426
repository. You can change the syntax of the above to use _any_ version of the
@@ -39,3 +41,53 @@ nix run github:supabase/nix-postgres#start-server 14 &
3941
sleep 5
4042
nix run github:supabase/nix-postgres#start-client 15
4143
```
44+
45+
## Running a server replica
46+
47+
To start a replica you can use the `start-postgres-replica` command.
48+
49+
- first argument: the master version
50+
- second argument: the master port
51+
- third argument: the replica server port
52+
53+
First start a server and a couple of replicas:
54+
55+
```
56+
$ start-postgres-server 15 5435
57+
58+
$ start-postgres-replica 15 5439
59+
60+
$ start-postgres-replica 15 5440
61+
```
62+
63+
Now check the master server:
64+
65+
```
66+
$ start-postgres-client 15 5435
67+
```
68+
69+
```sql
70+
SELECT client_addr, state
71+
FROM pg_stat_replication;
72+
client_addr | state
73+
-------------+-----------
74+
::1 | streaming
75+
::1 | streaming
76+
(2 rows)
77+
78+
create table items as select x::int from generate_series(1,100) x;
79+
```
80+
81+
And a replica:
82+
83+
```
84+
$ start-postgres-client 15 5439
85+
```
86+
87+
```sql
88+
select count(*) from items;
89+
count
90+
-------
91+
100
92+
(1 row)
93+
```

flake.nix

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@
301301
302302
chmod +x $out/bin/migrate-postgres
303303
'';
304+
305+
start-replica = pkgs.runCommand "start-postgres-replica" {} ''
306+
mkdir -p $out/bin
307+
substitute ${./tools/run-replica.sh.in} $out/bin/start-postgres-replica \
308+
--subst-var-by 'PGSQL_SUPERUSER' '${pgsqlSuperuser}' \
309+
--subst-var-by 'PSQL14_BINDIR' '${basePackages.psql_14.bin}' \
310+
--subst-var-by 'PSQL15_BINDIR' '${basePackages.psql_15.bin}'
311+
chmod +x $out/bin/start-postgres-replica
312+
'';
304313
};
305314

306315
# Create a testing harness for a PostgreSQL package. This is used for
@@ -361,6 +370,7 @@
361370
in {
362371
start-server = mkApp "start-server" "start-postgres-server";
363372
start-client = mkApp "start-client" "start-postgres-client";
373+
start-replica = mkApp "start-replica" "start-postgres-replica";
364374
migration-test = mkApp "migrate-tool" "migrate-postgres";
365375
};
366376

@@ -375,6 +385,7 @@
375385

376386
basePackages.start-server
377387
basePackages.start-client
388+
basePackages.start-replica
378389
basePackages.migrate-tool
379390
];
380391
shellHook = ''

tests/postgresql.conf.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ dynamic_shared_memory_type = posix # the default is the first option
199199

200200
# - Settings -
201201

202-
#wal_level = replica # minimal, replica, or logical
202+
wal_level = logical # minimal, replica, or logical
203203
# (change requires restart)
204204
#fsync = on # flush data to disk for crash safety
205205
# (turning this off can cause
@@ -214,7 +214,7 @@ dynamic_shared_memory_type = posix # the default is the first option
214214
# fsync_writethrough
215215
# open_sync
216216
#full_page_writes = on # recover from partial page writes
217-
#wal_log_hints = off # also do full page writes of non-critical updates
217+
wal_log_hints = on # also do full page writes of non-critical updates
218218
# (change requires restart)
219219
#wal_compression = off # enable compression of full-page writes
220220
#wal_init_zero = on # zero-fill new WAL files

tools/run-replica.sh.in

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
# shellcheck shell=bash
3+
4+
[ ! -z "$DEBUG" ] && set -x
5+
6+
# first argument should be '14' or '15' for the version
7+
if [ "$1" == "14" ]; then
8+
echo "Starting server for PSQL 14"
9+
PSQL14=@PSQL14_BINDIR@
10+
BINDIR="$PSQL14"
11+
elif [ "$1" == "15" ]; then
12+
echo "Starting server for PSQL 15"
13+
PSQL15=@PSQL15_BINDIR@
14+
BINDIR="$PSQL15"
15+
else
16+
echo "Please provide a valid Postgres version (14 or 15)"
17+
exit 1
18+
fi
19+
20+
export PATH=$BINDIR/bin:$PATH
21+
22+
PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
23+
MASTER_PORTNO="$2"
24+
REPLICA_PORTNO="$3"
25+
REPLICA_SLOT="replica_$RANDOM"
26+
DATDIR=$(mktemp -d)
27+
mkdir -p "$DATDIR"
28+
29+
echo "NOTE: runing pg_basebackup for server on port $MASTER_PORTNO"
30+
echo "NOTE: using replica slot $REPLICA_SLOT"
31+
32+
pg_basebackup -p "$MASTER_PORTNO" -h localhost -U "${PGSQL_SUPERUSER}" -X stream -C -S "$REPLICA_SLOT" -v -R -D "$DATDIR"
33+
34+
echo "NOTE: using port $REPLICA_PORTNO for replica"
35+
echo "NOTE: using temporary directory $DATDIR for data, which will not be removed"
36+
echo "NOTE: you are free to re-use this data directory at will"
37+
echo
38+
39+
exec postgres -p "$REPLICA_PORTNO" -D "$DATDIR" -k /tmp

tools/run-server.sh.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env bash
1+
l!/usr/bin/env bash
22
# shellcheck shell=bash
33

44
[ ! -z "$DEBUG" ] && set -x

0 commit comments

Comments
 (0)