Skip to content

Instantly share code, notes, and snippets.

@mkraft
Last active February 22, 2023 12:53
Show Gist options
  • Save mkraft/a14a3a924773c6b8714d2236b799e7c8 to your computer and use it in GitHub Desktop.
Save mkraft/a14a3a924773c6b8714d2236b799e7c8 to your computer and use it in GitHub Desktop.
Bitnami Postgresql synchronous commit test setup

startup:

docker-compose up

psql in each a node, if desired (nodes are pg-source, pg-replica1, and pg-replica2):

docker exec -it pg-source bash -c 'PGPASSWORD=my_password psql -U my_user -d my_database -P pager=off -P expanded=auto'

read replication log sequence number (LSN) position:

select pg_last_wal_replay_lsn();

parse the return value in Go:

func lsnToInt64(lsn string) (int64, error) {
	var pos int64
	parts := strings.Split(lsn, "/")
	for _, part := range parts {
		val, err := strconv.ParseInt(part, 16, 64)
		if err != nil {
			return 0, err
		}
		pos = pos + val
	}
	return pos, nil
}
version: '2'
services:
postgresql-source:
image: 'bitnami/postgresql:latest'
container_name: pg-source
ports:
- '5432'
volumes:
- './postgresql_source_data:/bitnami/postgresql'
environment:
- POSTGRESQL_REPLICATION_MODE=master
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
- POSTGRESQL_USERNAME=my_user
- POSTGRESQL_PASSWORD=my_password
- POSTGRESQL_DATABASE=my_database
- POSTGRESQL_SYNCHRONOUS_COMMIT_MODE=on
- POSTGRESQL_NUM_SYNCHRONOUS_REPLICAS=1
postgresql-replica1:
image: 'bitnami/postgresql:latest'
container_name: pg-replica1
ports:
- '5433'
depends_on:
- postgresql-source
environment:
- POSTGRESQL_REPLICATION_MODE=slave
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
- POSTGRESQL_MASTER_HOST=postgresql-source
- POSTGRESQL_MASTER_PORT_NUMBER=5432
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- './postgresql_replica1_data:/bitnami/postgresql'
postgresql-replica2:
image: 'bitnami/postgresql:latest'
container_name: pg-replica2
ports:
- '5434'
depends_on:
- postgresql-source
environment:
- POSTGRESQL_REPLICATION_MODE=slave
- POSTGRESQL_REPLICATION_USER=repl_user
- POSTGRESQL_REPLICATION_PASSWORD=repl_password
- POSTGRESQL_MASTER_HOST=postgresql-source
- POSTGRESQL_MASTER_PORT_NUMBER=5432
- ALLOW_EMPTY_PASSWORD=yes
volumes:
- './postgresql_replica2_data:/bitnami/postgresql'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment