Skip to content

Commit 9860814

Browse files
committed
Updates
1 parent 819288d commit 9860814

1 file changed

Lines changed: 17 additions & 34 deletions

File tree

docs/workshop/07_macro_overview_part_2.md

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Macro Query Optimization Part 2
22
In this section, we'll begin to work with multiple PostgreSQL instances.
3+
- [Rails + Postgres Diagram #2 Primary + Replica](https://github.com/andyatkinson/rideshare/blob/main/docs/rideshare_rails_postgres_architecture_2.svg)
34

45
Remember this hierarchy:
56
```
@@ -19,26 +20,11 @@ __________________________________________________________________
1920
We'll run these using Docker. Start up Docker.
2021

2122
## Part 1: Docker PostgreSQL Containers
22-
- Boot up Docker. There may be zero containers running. (`docker ps`)
23+
- `docker ps -a` (check if db01 and db02 are running)
2324
- Docker containers are in `docker` directory. Read README: <https://github.com/andyatkinson/rideshare/blob/main/docker/README.md>
24-
- Create a docker network (`rideshare-net`) the containers can use
25-
- Run the script to start the `db01` container
26-
- Run the script to start the `db02` container
27-
- Verify they're running with `docker ps`
28-
29-
```sh
30-
# Clean-up from past runs:
31-
cd docker
32-
rm postgresql.conf
33-
rm -rf postgres-docker/
34-
35-
# Starting point:
36-
docker network create rideshare-net
37-
sh run_db_db01_primary.sh
38-
sh run_db_db02_replica.sh
39-
```
40-
41-
Let's configure them.
25+
- (created by script) Create a docker network (`rideshare-net`) the containers can use
26+
- (created by script) Run the script to start the `db01` container
27+
- (created by script) Run the script to start the `db02` container
4228

4329
## Part 2: Enabling Physical Replication
4430
Prep: Remove annoying Docker messages:
@@ -57,28 +43,16 @@ docker ps -a
5743
Go to the `docker` directory and run `sh reset_docker_instances.sh`.
5844

5945
If you're missing `postgresql.conf`, you'll be prompted to create it.
60-
61-
6246
```sh
6347
cd rideshare
6448
cd docker
6549
sh reset_docker_instances.sh
6650
```
6751

68-
Follow the commands to copy down `postgresql.conf`.
69-
70-
Edit the settings `wal_level = logical` and save the changes. The script copies postgresql.conf to db01.
71-
72-
Run the command again to do that:
73-
74-
```sh
75-
sh reset_docker_instances.sh
76-
```
77-
7852
Let's walk through the highlights:
7953
- Replaced `postgresql.conf` config file on db01
8054
- Created replication slot on primary db01
81-
- Created `replication_user` user on db01 with a unique password and permissions
55+
- Created `replication_user` user on db01 with a unique password (scram encryption) and permissions
8256
- Created `pg_hba.conf` on db01 to allow access
8357
- Placed password in `.pgpass` and copied to db01 and db02 for `replication_user`
8458
- Restarted db01
@@ -208,10 +182,19 @@ And on db02, something like this:
208182
2024-05-01 02:07:18.648 UTC [31] LOG: started streaming WAL from primary at 0/7000000 on timeline 1
209183
```
210184

185+
Now the system identifiers will be the same. db02 is a byte-for-byte-copy of db01.
186+
```sh
187+
docker exec --user postgres -it db01 \
188+
psql -c "SELECT system_identifier FROM pg_control_system();"
189+
190+
docker exec --user postgres -it db02 \
191+
psql -c "SELECT system_identifier FROM pg_control_system();"
192+
```
193+
211194
## Conclusion
212-
That concludes the basics of setting up a replica instance.
195+
That concludes the basics of setting up a replica instance. You're now working with two instances, one as a primary, and the other in "ready only" "recovery" mode replaying changes from the primary.
213196

214-
In the next section we'll continue with adding content, then layer on application-level configuration.
197+
In the next section we'll continue with using more instances, adding application-level configuration.
215198

216199

217200
## Appendix: Debugging and Troubleshooting

0 commit comments

Comments
 (0)