Skip to content

Commit 28af5d0

Browse files
author
SimoneGiusso
committed
Replication Setup
0 parents  commit 28af5d0

File tree

8 files changed

+1046
-0
lines changed

8 files changed

+1046
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Physical Replication/README.md
898 KB
Loading

Physical Replication/demo.sql

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
-- 1. CHECKING IF SETUP IS WORKING
2+
3+
-- Primary Server should have a walsender process
4+
SELECT *
5+
FROM pg_catalog.pg_stat_activity
6+
WHERE backend_type = 'walsender'
7+
8+
-- Standby Server should have a walreceiver process
9+
SELECT *
10+
FROM pg_catalog.pg_stat_activity
11+
WHERE backend_type = 'walreceiver'
12+
13+
-- Finally the standby server should contain the same data of the primary server
14+
SELECT *
15+
FROM authors
16+
17+
-- 2. STREAMING REPLICATION IN ACTION
18+
19+
-- movies table doesn't exist on standby server
20+
SELECT *
21+
FROM movies
22+
23+
-- Create movies table with foreign key on primary server and insert some rows
24+
CREATE TABLE movies (
25+
movie_id SERIAL PRIMARY KEY,
26+
title VARCHAR(255) NOT NULL,
27+
release_year INT,
28+
director VARCHAR(255),
29+
genre VARCHAR(100),
30+
author_id INT,
31+
FOREIGN KEY (author_id) REFERENCES authors(author_id)
32+
);
33+
INSERT INTO movies (title, release_year, director, genre, author_id) VALUES
34+
('The Shawshank Redemption', 1994, 'Frank Darabont', 'Drama', 1),
35+
('The Godfather', 1972, 'Francis Ford Coppola', 'Crime', 2),
36+
('The Dark Knight', 2008, 'Christopher Nolan', 'Action', 3);
37+
38+
-- Data should have been replicated on the standby server
39+
SELECT *
40+
FROM movies
41+
42+
-- 3. READONLY STANDBY AND WRITING QUERIES
43+
44+
-- Although only read queries can be performed on standby, write queries can still run on the standby as result of replication
45+
-- Open a transaction in standby server
46+
BEGIN;
47+
SELECT * FROM movies;
48+
49+
-- Add new column on movies table in primary server
50+
ALTER TABLE movies ADD COLUMN rating int;
51+
52+
-- Open a new connection to the standby. The following query won't run because the ALTER TABLE statement queued, which has priority, is blocked by the opened transaction.
53+
SELECT * FROM movies;
54+
55+
-- Unblock the recently opened connection by running END; on standby
56+
-- If you don't do it a mechanism provided by postgresql, and active by default, will forcibly cancel the standby query that conflict with to-be-applied WAL records. The delay, before the query get cancelled, can be controlled via the max_standby_streaming_delay parameter (30s by default).
57+
-- This error will be then showed to the client in the stanby:
58+
59+
-- SQL Error [40001]: FATAL: terminating connection due to conflict with recovery
60+
-- Detail: User was holding a relation lock for too long.
61+
-- Hint: In a moment you should be able to reconnect to the database and repeat your command.
62+
63+
-- The problem doesn't happen if the opened transaction is on the primary, since the statement are sent after commit
64+
65+
-- Primary server connection 1
66+
BEGIN;
67+
SELECT * FROM movies;
68+
69+
-- Primary server connection 2
70+
ALTER TABLE movies ADD COLUMN budget float;
71+
72+
-- At this point SELECT * FROM movies; on primary server will wait. However it will run on the standby.
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
networks:
2+
default:
3+
name: internal-network
4+
5+
services:
6+
masterdb:
7+
image: postgres:15.6-alpine
8+
container_name: primary-server
9+
ports:
10+
- 5432:5432
11+
environment:
12+
POSTGRES_USER: admin
13+
POSTGRES_PASSWORD: psw
14+
volumes:
15+
- ./primary_config/init.sql:/docker-entrypoint-initdb.d/init.sql
16+
- ./primary_config/pg_hba.conf:/etc/pg_hba.conf
17+
- ./primary_config/postgresql.conf:/etc/postgresql.conf
18+
command: postgres -c config_file=/etc/postgresql.conf
19+
20+
readonlydb:
21+
image: postgres:15.6-alpine
22+
container_name: hot-standby
23+
ports:
24+
- 5430:5432
25+
environment:
26+
POSTGRES_USER: admin
27+
POSTGRES_PASSWORD: psw
28+
PGUSER: replica_role
29+
PGPASSWORD: replica_password
30+
user: postgres
31+
command: |
32+
bash -c "
33+
echo 'Waiting for primary...'
34+
sleep 3s
35+
pg_basebackup --pgdata=/var/lib/postgresql/data --write-recovery-conf --host=masterdb --port=5432
36+
chmod 0700 /var/lib/postgresql/data
37+
postgres
38+
"
39+
depends_on:
40+
- masterdb
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Roles used by the stand by server for replication
2+
CREATE ROLE replica_role WITH LOGIN REPLICATION PASSWORD 'replica_password';
3+
4+
\c postgres;
5+
6+
-- Create authors table
7+
CREATE TABLE authors (
8+
author_id SERIAL PRIMARY KEY,
9+
author_name VARCHAR(255) NOT NULL,
10+
nationality VARCHAR(100),
11+
birth_year INT
12+
);
13+
14+
-- Insert rows into authors table
15+
INSERT INTO authors (author_name, nationality, birth_year) VALUES
16+
('Stephen King', 'American', 1947),
17+
('Mario Puzo', 'American', 1920),
18+
('Christopher Nolan', 'British', 1970);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# PostgreSQL Client Authentication Configuration File
2+
# ===================================================
3+
#
4+
# Refer to the "Client Authentication" section in the PostgreSQL
5+
# documentation for a complete description of this file. A short
6+
# synopsis follows.
7+
#
8+
# This file controls: which hosts are allowed to connect, how clients
9+
# are authenticated, which PostgreSQL user names they can use, which
10+
# databases they can access. Records take one of these forms:
11+
#
12+
# local DATABASE USER METHOD [OPTIONS]
13+
# host DATABASE USER ADDRESS METHOD [OPTIONS]
14+
# hostssl DATABASE USER ADDRESS METHOD [OPTIONS]
15+
# hostnossl DATABASE USER ADDRESS METHOD [OPTIONS]
16+
# hostgssenc DATABASE USER ADDRESS METHOD [OPTIONS]
17+
# hostnogssenc DATABASE USER ADDRESS METHOD [OPTIONS]
18+
#
19+
# (The uppercase items must be replaced by actual values.)
20+
#
21+
# The first field is the connection type:
22+
# - "local" is a Unix-domain socket
23+
# - "host" is a TCP/IP socket (encrypted or not)
24+
# - "hostssl" is a TCP/IP socket that is SSL-encrypted
25+
# - "hostnossl" is a TCP/IP socket that is not SSL-encrypted
26+
# - "hostgssenc" is a TCP/IP socket that is GSSAPI-encrypted
27+
# - "hostnogssenc" is a TCP/IP socket that is not GSSAPI-encrypted
28+
#
29+
# DATABASE can be "all", "sameuser", "samerole", "replication", a
30+
# database name, or a comma-separated list thereof. The "all"
31+
# keyword does not match "replication". Access to replication
32+
# must be enabled in a separate record (see example below).
33+
#
34+
# USER can be "all", a user name, a group name prefixed with "+", or a
35+
# comma-separated list thereof. In both the DATABASE and USER fields
36+
# you can also write a file name prefixed with "@" to include names
37+
# from a separate file.
38+
#
39+
# ADDRESS specifies the set of hosts the record matches. It can be a
40+
# host name, or it is made up of an IP address and a CIDR mask that is
41+
# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that
42+
# specifies the number of significant bits in the mask. A host name
43+
# that starts with a dot (.) matches a suffix of the actual host name.
44+
# Alternatively, you can write an IP address and netmask in separate
45+
# columns to specify the set of hosts. Instead of a CIDR-address, you
46+
# can write "samehost" to match any of the server's own IP addresses,
47+
# or "samenet" to match any address in any subnet that the server is
48+
# directly connected to.
49+
#
50+
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
51+
# "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
52+
# Note that "password" sends passwords in clear text; "md5" or
53+
# "scram-sha-256" are preferred since they send encrypted passwords.
54+
#
55+
# OPTIONS are a set of options for the authentication in the format
56+
# NAME=VALUE. The available options depend on the different
57+
# authentication methods -- refer to the "Client Authentication"
58+
# section in the documentation for a list of which options are
59+
# available for which authentication methods.
60+
#
61+
# Database and user names containing spaces, commas, quotes and other
62+
# special characters must be quoted. Quoting one of the keywords
63+
# "all", "sameuser", "samerole" or "replication" makes the name lose
64+
# its special character, and just match a database or username with
65+
# that name.
66+
#
67+
# This file is read on server startup and when the server receives a
68+
# SIGHUP signal. If you edit the file on a running system, you have to
69+
# SIGHUP the server for the changes to take effect, run "pg_ctl reload",
70+
# or execute "SELECT pg_reload_conf()".
71+
#
72+
# Put your actual configuration here
73+
# ----------------------------------
74+
#
75+
# If you want to allow non-local connections, you need to add more
76+
# "host" records. In that case you will also need to make PostgreSQL
77+
# listen on a non-local interface via the listen_addresses
78+
# configuration parameter, or via the -i or -h command line switches.
79+
80+
# CAUTION: Configuring the system for local "trust" authentication
81+
# allows any local user to connect as any PostgreSQL user, including
82+
# the database superuser. If you do not trust all your local users,
83+
# use another authentication method.
84+
85+
86+
# TYPE DATABASE USER ADDRESS METHOD
87+
88+
# "local" is for Unix domain socket connections only
89+
local all all trust
90+
# IPv4 local connections:
91+
host all all 127.0.0.1/32 trust
92+
# IPv6 local connections:
93+
host all all ::1/128 trust
94+
# Allow replication connections from localhost, by a user with the
95+
# replication privilege.
96+
local replication all trust
97+
host replication all 127.0.0.1/32 trust
98+
host replication all ::1/128 trust
99+
100+
host all all all scram-sha-256
101+
102+
host replication replica_role hot-standby.internal-network trust

0 commit comments

Comments
 (0)