From 5ef643e25ab13859526036d97bb4584397884092 Mon Sep 17 00:00:00 2001 From: Ronald Bradford Date: Thu, 4 Jun 2026 14:34:13 -0400 Subject: [PATCH] Revisions to align better with offical docs (hostgroups), updated config, added more queries and configuration --- .../APP-ONLY-BENCHMARK.md | 44 ++++++++++--- .../ha-proxysql-benchmark/Dockerfile.proxysql | 15 ++--- .../READ-LOAD-BALANCING-BENCHMARK.md | 36 ++++++----- .../READ-WRITE-SPLIT-BENCHMARK.md | 32 +++++----- postgresql/ha-proxysql-benchmark/README.md | 14 ++--- .../ha-proxysql-benchmark/SIMPLE-BENCHMARK.md | 14 ----- postgresql/ha-proxysql-benchmark/SQL.md | 62 +++++++++---------- .../WRITE-ONLY-BENCHMARK.md | 18 +++--- .../ha-proxysql-benchmark/cnf/proxysql.cnf | 8 +-- .../ha-proxysql-benchmark/docker-compose.yml | 24 +++---- postgresql/ha-proxysql-benchmark/run.sh | 10 ++- .../ha-proxysql-benchmark/scripts/summary.sh | 24 +++---- .../sql/init-primary.sql | 2 + 13 files changed, 165 insertions(+), 138 deletions(-) delete mode 100644 postgresql/ha-proxysql-benchmark/SIMPLE-BENCHMARK.md diff --git a/postgresql/ha-proxysql-benchmark/APP-ONLY-BENCHMARK.md b/postgresql/ha-proxysql-benchmark/APP-ONLY-BENCHMARK.md index 1fbff83..7181b92 100644 --- a/postgresql/ha-proxysql-benchmark/APP-ONLY-BENCHMARK.md +++ b/postgresql/ha-proxysql-benchmark/APP-ONLY-BENCHMARK.md @@ -1,6 +1,6 @@ ## Simulating Application Load (No Proxy) -To emulate some random load from the three application servers to the primary DB run the benchmark. ProxySQL is already pre-configured via [proxysql.cnf](cnf/proxysql.cnf) to have defined the primary server in hostgroup `10`. +To emulate some random load from the three application servers to the primary DB run the benchmark. ProxySQL is already pre-configured via [proxysql.cnf](cnf/proxysql.cnf) to have defined the primary server in hostgroup `1`. ``` $ ./run-benchmark.sh @@ -15,20 +15,22 @@ App Server 3: PID=7205 RATE=200 THREADS=10 ``` -Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `100` originating from application servers `.6`, `.16`, `.26`. +Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `.1` originating from application servers `.20`, `.21`, `.22`. ``` $ scripts/summary.sh hosts-per-db ##### For DB 'primary' ##### - client_addr | count ----------------+------- - 172.113.0.16 | 11 - 172.113.0.26 | 5 - 172.113.0.6 | 5 - 172.113.0.101 | 1 - 172.113.0.102 | 1 + client_addr | count +-------------+------- + 10.0.0.20 | 11 + 10.0.0.21 | 10 + 10.0.0.222 | 9 + 10.0.0.22 | 7 + 10.0.0.3 | 1 + 10.0.0.2 | 1 +(6 rows) ``` You will find no connections to the replicas. @@ -46,3 +48,27 @@ You will find no connections to the replicas. -------------+------- (0 rows) ``` + +You can view SQL traffic using where for a benchmark you will find primary benchmark traffic, but no replica benchmark traffic. + +``` +scripts/summary.sh queries-per-db + +##### For DB 'primary' ##### + + calls | total_ms | avg_ms | stddev_ms | rows | query +-------+----------+--------+-----------+-------+----------------------------------------------------------------------- + 227 | 43.82 | 0.19 | 0.09 | 22700 | SELECT DISTINCT c FROM sbtest5 WHERE id BETWEEN $1 AND $2 ORDER BY c + 215 | 41.95 | 0.20 | 0.08 | 21500 | SELECT DISTINCT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c + 207 | 37.27 | 0.18 | 0.07 | 20700 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c + 210 | 36.33 | 0.17 | 0.07 | 21000 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c + 195 | 36.02 | 0.18 | 0.08 | 19500 | SELECT DISTINCT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c + 193 | 36.02 | 0.19 | 0.06 | 19300 | SELECT DISTINCT c FROM sbtest7 WHERE id BETWEEN $1 AND $2 ORDER BY c + 219 | 35.40 | 0.16 | 0.05 | 21900 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 ORDER BY c + 191 | 35.29 | 0.18 | 0.07 | 19100 | SELECT DISTINCT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c + 192 | 35.21 | 0.18 | 0.08 | 19200 | SELECT DISTINCT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 ORDER BY c + 188 | 34.94 | 0.19 | 0.07 | 18800 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c + +``` + +Next, checkout the [Simulating Application Load (With Proxy)](WRITE-ONLY-BENCHMARK.md) benchmark. diff --git a/postgresql/ha-proxysql-benchmark/Dockerfile.proxysql b/postgresql/ha-proxysql-benchmark/Dockerfile.proxysql index e9b8cfe..fe8fc98 100644 --- a/postgresql/ha-proxysql-benchmark/Dockerfile.proxysql +++ b/postgresql/ha-proxysql-benchmark/Dockerfile.proxysql @@ -7,17 +7,12 @@ RUN apt-get update && \ && apt-get clean && \ rm -rf /var/lib/apt/lists/* -# Create proxysql user and data directory -RUN useradd -r -s /bin/false proxysql && \ - mkdir -p /var/lib/proxysql && \ - chown -R proxysql:proxysql /var/lib/proxysql +ARG PROXYSQL_VERSION=3.0.8 -# Download ProxySQL binary -RUN curl -fSL "https://repo.proxysql.com/ProxySQL/tmp/demo/proxysql.ubuntu22" -o /usr/bin/proxysql && \ - chmod +x /usr/bin/proxysql - -# Download web interface plugin -RUN curl -fSL "https://repo.proxysql.com/ProxySQL/tmp/demo/Web_Interface_plugin.so.ubuntu22" -o /var/lib/proxysql/Web_Interface_plugin.so +RUN ARCH=$(dpkg --print-architecture) && \ + curl -fSL "https://repo.proxysql.com/ProxySQL/proxysql-3.0.x/jammy/proxysql_${PROXYSQL_VERSION}-ubuntu22_${ARCH}.deb" -o /tmp/proxysql.deb && \ + dpkg -i /tmp/proxysql.deb && \ + rm /tmp/proxysql.deb # Configure supervisor to run ProxySQL COPY cnf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf diff --git a/postgresql/ha-proxysql-benchmark/READ-LOAD-BALANCING-BENCHMARK.md b/postgresql/ha-proxysql-benchmark/READ-LOAD-BALANCING-BENCHMARK.md index 500875e..c0bd95d 100644 --- a/postgresql/ha-proxysql-benchmark/READ-LOAD-BALANCING-BENCHMARK.md +++ b/postgresql/ha-proxysql-benchmark/READ-LOAD-BALANCING-BENCHMARK.md @@ -7,26 +7,28 @@ If you have been running other tests, you may need to cleanup database connectio To emulate load from the three application servers to the Proxy Layer that splits Writes and Reads between the Primary DB and a replica DB we first configure ProxySQL. -1. Remove any existing hostgroup `20` servers. -2. Add the second replica as a known server to ProxySQL using hostgroup `20`. NOTE: hostgroup `10` defines the primary. +1. Remove any existing hostgroup `2` servers. +2. Add the second replica as a known server to ProxySQL using hostgroup `2`. NOTE: hostgroup `1` defines the primary. 3. Set the `weight` of each server equally. To emulate a percentage, we make the two servers 50, so this mirrors 50%, however any equal value will produce equal load. ``` -$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost +$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost -v PROMPT1='ProxySQLAdmin> ' ``` ``` DELETE FROM pgsql_servers WHERE hostgroup_id=20; INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight) -VALUES (20, 'replica1', 5432, 'ONLINE', 50, 50); +VALUES (2, 'replica1', 5432, 'ONLINE', 50, 50); INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight) -VALUES (20, 'replica2', 5432, 'ONLINE', 50, 50); +VALUES (2, 'replica2', 5432, 'ONLINE', 50, 50); LOAD PGSQL SERVERS TO RUNTIME; SELECT * FROM pgsql_servers; ``` +NOTE: If you would like to persist these changes then also run `SAVE PGSQL SERVERS TO DISK;`. + ## Run Benchmark To emulate some random load from the three application servers to the databases run the benchmark. @@ -42,26 +44,28 @@ $ scripts/summary.sh hosts-per-db ##### For DB 'primary' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 7 - 172.113.0.101 | 1 - 172.113.0.102 | 1 + client_addr | count +-------------+------- + 10.0.0.222 | 11 + 10.0.0.3 | 1 + 10.0.0.2 | 1 (3 rows) ##### For DB 'replica1' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 34 + client_addr | count +-------------+------- + 10.0.0.222 | 2 (1 row) ##### For DB 'replica2' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 26 + client_addr | count +-------------+------- + 10.0.0.222 | 2 (1 row) ``` + +Review the ProxySQL stats with commands in [SQL.md](SQL.md). diff --git a/postgresql/ha-proxysql-benchmark/READ-WRITE-SPLIT-BENCHMARK.md b/postgresql/ha-proxysql-benchmark/READ-WRITE-SPLIT-BENCHMARK.md index 9a02777..8a82127 100644 --- a/postgresql/ha-proxysql-benchmark/READ-WRITE-SPLIT-BENCHMARK.md +++ b/postgresql/ha-proxysql-benchmark/READ-WRITE-SPLIT-BENCHMARK.md @@ -7,30 +7,32 @@ If you have been running other tests, you may need to cleanup database connectio To emulate load from the three application servers to the Proxy Layer that splits Writes and Reads between the Primary DB and a replica DB we first configure ProxySQL. -1. Add the replica as a known server to ProxySQL using hostgroup `20`. NOTE: hostgroup `10` defines the primary. -2. Add a query rule that directs `SELECT` statements to hostgroup `20`. Queries that do not match will goto the primary. +1. Add the replica as a known server to ProxySQL using hostgroup `2`. NOTE: hostgroup `1` defines the primary. +2. Add a query rule that directs `SELECT` statements to hostgroup `2`. Queries that do not match will goto the primary. NOTE: This is not advised in production. You should use advanced routing rules with a query digest or more specific query pattern. ``` -$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost +$ docker exec -it proxysql psql -U radmin -p 6132 -hlocalhost -v PROMPT1='ProxySQLAdmin> ' ``` ``` INSERT INTO pgsql_servers (hostgroup_id, hostname, port, status, max_connections, weight) -VALUES (20, 'replica1', 5432, 'ONLINE', 50, 100); +VALUES (2, 'replica1', 5432, 'ONLINE', 50, 100); LOAD PGSQL SERVERS TO RUNTIME; SELECT * FROM pgsql_servers; INSERT INTO pgsql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) -VALUES (10, 1, '^SELECT.*FOR UPDATE', 10, 1), -(20, 1, '^SELECT', 20, 1); +VALUES (10, 1, '^SELECT.*FOR UPDATE', 1, 1), +(20, 1, '^SELECT', 2, 1); LOAD PGSQL QUERY RULES TO RUNTIME; SELECT * FROM pgsql_query_rules; ``` +NOTE: These updates are to `RUNTIME` only and will not be persisted between restarts. You would use `TO DISK` in a customer environment. + ## Run Benchmark To emulate some random load from the three application servers to the databases run the benchmark. @@ -46,19 +48,19 @@ $ scripts/summary.sh hosts-per-db ##### For DB 'primary' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 9 - 172.113.0.101 | 1 - 172.113.0.102 | 1 + client_addr | count +-------------+------- + 10.0.0.222 | 8 + 10.0.0.3 | 1 + 10.0.0.2 | 1 (3 rows) ##### For DB 'replica1' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 12 + client_addr | count +-------------+------- + 10.0.0.222 | 9 (1 row) @@ -68,3 +70,5 @@ $ scripts/summary.sh hosts-per-db -------------+------- (0 rows) ``` + +Be sure to [CLEANUP.md](CLEANUP.md) before commencing benchmark before running the [Application Read Load Balancing Benchmark](READ-LOAD-BALANCING-BENCHMARK.md). diff --git a/postgresql/ha-proxysql-benchmark/README.md b/postgresql/ha-proxysql-benchmark/README.md index 67cc491..a2ae9e6 100644 --- a/postgresql/ha-proxysql-benchmark/README.md +++ b/postgresql/ha-proxysql-benchmark/README.md @@ -14,15 +14,15 @@ This tutorial will launch a three PostgreSQL servers (one primary with two repli running [sysbench](https://github.com/akopytov/sysbench), an open-source, multi-threaded, and modular benchmarking tool used to evaluate system performance, and a Proxy running [proxysql](https://proxysql.com), an open-source, transparent database proxy. ### Container Topology -For this setup we will use the `172.113.0/24` network with: +For this setup we will use the `10.0.0.0/16` network with: - Databases - - Primary `.100` - - Replica 1 `.101` - - Replica 2 `.102` + - Primary `.1` + - Replica 1 `.2` + - Replica 2 `.3` - Application - - Server 1 `.6` - - Server 2 `.16` - - Server 3 `.26` + - Server 1 `.20` + - Server 2 `.21` + - Server 3 `.22` - Proxy - ProxySQL `.222` diff --git a/postgresql/ha-proxysql-benchmark/SIMPLE-BENCHMARK.md b/postgresql/ha-proxysql-benchmark/SIMPLE-BENCHMARK.md deleted file mode 100644 index 77a8c83..0000000 --- a/postgresql/ha-proxysql-benchmark/SIMPLE-BENCHMARK.md +++ /dev/null @@ -1,14 +0,0 @@ -## Simulating Application Load (No Proxy) - -To emulate some random load from the three application servers to the primary DB. -``` -$ ./run-benchmark.sh -``` - -This command will launch three benchmarks running for 10 minutes, with a random number of threads (5-15) and a random rate (50 - 250 in 50 increments). This will give feedback such as: - -``` -App Server 1: PID=7203 RATE=200 THREADS=10 -App Server 2: PID=7204 RATE=50 THREADS=8 -App Server 3: PID=7205 RATE=200 THREADS=10 -``` diff --git a/postgresql/ha-proxysql-benchmark/SQL.md b/postgresql/ha-proxysql-benchmark/SQL.md index 2bcaf42..786e363 100644 --- a/postgresql/ha-proxysql-benchmark/SQL.md +++ b/postgresql/ha-proxysql-benchmark/SQL.md @@ -4,15 +4,15 @@ You can view monitoring of ProxySQL via the ``` . .envrc -docker exec -it proxysql psql -U ${PROXYSQL_ADMIN_USER} -h ${PROXYSQL_DB_HOST} -p ${PROXYSQL_ADMIN_PORT} +docker exec -it proxysql psql -U ${PROXYSQL_ADMIN_USER} -h ${PROXYSQL_DB_HOST} -p ${PROXYSQL_ADMIN_PORT} -v PROMPT1='ProxySQLAdmin> ' ``` ## Insert Query Rules for SELECT traffic ```sql DELETE FROM pgsql_query_rules; INSERT INTO pgsql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) -VALUES (10, 1, '^SELECT.*FOR UPDATE', 10, 1), - (20, 1, '^SELECT', 20, 1); +VALUES (10, 1, '^SELECT.*FOR UPDATE', 1, 1), + (20, 1, '^SELECT', 2, 1); LOAD PGSQL QUERY RULES TO RUNTIME; ``` @@ -42,26 +42,26 @@ For Example ``` hostgroup | digest_text | count_star | sum_time -----------+----------------------------------------------------------------------+------------+---------- -20 | SELECT c FROM sbtest2 WHERE id=$1 | 1519 | 380617 -20 | SELECT c FROM sbtest3 WHERE id=$1 | 1493 | 497395 -20 | SELECT c FROM sbtest10 WHERE id=$1 | 1403 | 805586 -20 | SELECT c FROM sbtest6 WHERE id=$1 | 1354 | 460381 -20 | SELECT c FROM sbtest9 WHERE id=$1 | 1333 | 459937 -20 | SELECT c FROM sbtest5 WHERE id=$1 | 1283 | 402680 -20 | SELECT c FROM sbtest7 WHERE id=$1 | 1231 | 373084 -20 | SELECT c FROM sbtest4 WHERE id=$1 | 1203 | 495676 -20 | SELECT c FROM sbtest1 WHERE id=$1 | 1073 | 476558 -20 | SELECT c FROM sbtest8 WHERE id=$1 | 1033 | 364307 -20 | SELECT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c | 180 | 80777 -20 | SELECT c FROM sbtest6 WHERE id BETWEEN $1 AND $2 ORDER BY c | 178 | 65076 -20 | SELECT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c | 177 | 58879 -20 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 | 174 | 48598 -20 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c | 172 | 64982 -20 | SELECT SUM(k) FROM sbtest6 WHERE id BETWEEN $1 AND $2 | 170 | 48491 -20 | SELECT c FROM sbtest2 WHERE id BETWEEN $1 AND $2 | 169 | 31988 -20 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c | 167 | 84833 -20 | SELECT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 | 166 | 62944 -20 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c | 166 | 42421 +1 | SELECT c FROM sbtest2 WHERE id=$1 | 1519 | 380617 +1 | SELECT c FROM sbtest3 WHERE id=$1 | 1493 | 497395 +1 | SELECT c FROM sbtest10 WHERE id=$1 | 1403 | 805586 +1 | SELECT c FROM sbtest6 WHERE id=$1 | 1354 | 460381 +1 | SELECT c FROM sbtest9 WHERE id=$1 | 1333 | 459937 +1 | SELECT c FROM sbtest5 WHERE id=$1 | 1283 | 402680 +1 | SELECT c FROM sbtest7 WHERE id=$1 | 1231 | 373084 +1 | SELECT c FROM sbtest4 WHERE id=$1 | 1203 | 495676 +1 | SELECT c FROM sbtest1 WHERE id=$1 | 1073 | 476558 +1 | SELECT c FROM sbtest8 WHERE id=$1 | 1033 | 364307 +1 | SELECT c FROM sbtest4 WHERE id BETWEEN $1 AND $2 ORDER BY c | 180 | 80777 +1 | SELECT c FROM sbtest6 WHERE id BETWEEN $1 AND $2 ORDER BY c | 178 | 65076 +1 | SELECT c FROM sbtest9 WHERE id BETWEEN $1 AND $2 ORDER BY c | 177 | 58879 +1 | SELECT c FROM sbtest10 WHERE id BETWEEN $1 AND $2 | 174 | 48598 +1 | SELECT c FROM sbtest8 WHERE id BETWEEN $1 AND $2 ORDER BY c | 172 | 64982 +1 | SELECT SUM(k) FROM sbtest6 WHERE id BETWEEN $1 AND $2 | 170 | 48491 +1 | SELECT c FROM sbtest2 WHERE id BETWEEN $1 AND $2 | 169 | 31988 +1 | SELECT DISTINCT c FROM sbtest1 WHERE id BETWEEN $1 AND $2 ORDER BY c | 167 | 84833 +1 | SELECT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 | 166 | 62944 +1 | SELECT DISTINCT c FROM sbtest3 WHERE id BETWEEN $1 AND $2 ORDER BY c | 166 | 42421 ``` ## Select Traffic Balancing @@ -80,18 +80,18 @@ In the example where we are sending different 25% of traffic to one replica, and ``` hostgroup | srv_host | Queries | pct -----------+----------+---------+------- -20 | replica2 | 17194 | 74.79 -20 | replica1 | 5797 | 25.21 -10 | primary | 0 | +2 | replica2 | 17194 | 74.79 +2 | replica1 | 5797 | 25.21 +1 | primary | 0 | -radmin=# select hostgroup_id, hostname, status, weight from pgsql_servers; +radmin=# SELECT hostgroup_id, hostname, status, weight FROM pgsql_servers; hostgroup_id | hostname | status | weight --------------+----------+--------+-------- - 10 | primary | ONLINE | 1 - 20 | replica1 | ONLINE | 1 - 20 | replica2 | ONLINE | 3 + 1 | primary | ONLINE | 1 + 2 | replica1 | ONLINE | 1 + 2 | replica2 | ONLINE | 3 ``` ``` -select * from stats_pgsql_query_rules; +SELECT * FROM stats_pgsql_query_rules; ``` diff --git a/postgresql/ha-proxysql-benchmark/WRITE-ONLY-BENCHMARK.md b/postgresql/ha-proxysql-benchmark/WRITE-ONLY-BENCHMARK.md index 2ab5b53..3151755 100644 --- a/postgresql/ha-proxysql-benchmark/WRITE-ONLY-BENCHMARK.md +++ b/postgresql/ha-proxysql-benchmark/WRITE-ONLY-BENCHMARK.md @@ -8,7 +8,7 @@ If you have been running other tests, you may need to cleanup database connectio To emulate some random load from the three application servers to the primary DB run the benchmark. ProxySQL is already pre-configured via [proxysql.cnf](cnf/proxysql.cnf) to have defined the primary server in hostgroup `10`. ``` -$ ./run-proxy-benchmark.sh +$ ./run-proxysql-benchmark.sh ``` This command will launch three benchmarks running for 10 minutes, with a random number of threads (5-15) and a random rate (50 - 250 in 50 increments). This will give feedback such as: @@ -20,20 +20,20 @@ App Server 3: PID=7205 RATE=200 THREADS=10 ``` -Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `100` originating from the Proxy `.222`. There will be no connections from `.6`, `.16`, `.26`. +Using the `summary.sh` script to review the host connections per database you will find connections to the primary database `.1` originating from the Proxy `.222`. There will be no new connections from `.20`, `.21`, `.22`. -Any connections remaining from a prior tests application servers `.6`, `.16`, `.26` will show up. +Any connections remaining from a prior tests application servers `.20`, `.21`, `.22` will show up. See [CLEANUP.md](CLEANUP.md) ``` $ scripts/summary.sh hosts-per-db ##### For DB 'primary' ##### - client_addr | count ----------------+------- - 172.113.0.222 | 18 - 172.113.0.101 | 1 - 172.113.0.102 | 1 + client_addr | count +-------------+------- + 10.0.0.222 | 9 + 10.0.0.3 | 1 + 10.0.0.2 | 1 (3 rows) ``` @@ -53,3 +53,5 @@ You will find no connections to the replicas. -------------+------- (0 rows) ``` + +You can also look at SQL queries in PostgreSQL with the `scripts/summary.sh` command. See [SQL.md](SQL.md) for example ProxySQL statements, demonstrated in more detail in following tests. diff --git a/postgresql/ha-proxysql-benchmark/cnf/proxysql.cnf b/postgresql/ha-proxysql-benchmark/cnf/proxysql.cnf index 69057f8..5c38bf2 100644 --- a/postgresql/ha-proxysql-benchmark/cnf/proxysql.cnf +++ b/postgresql/ha-proxysql-benchmark/cnf/proxysql.cnf @@ -1,12 +1,10 @@ datadir="/var/lib/proxysql" -web_interface_plugin="/var/lib/proxysql/Web_Interface_plugin.so" admin_variables= { # IMPORTANT. These are sample defaults and should be changed for your configuration. admin_credentials="admin:admin;radmin:radmin" pgsql_ifaces="0.0.0.0:6132" - admin-web_enabled="true" } pgsql_variables= @@ -32,13 +30,13 @@ pgsql_variables= pgsql_servers = ( - { hostgroup=10, hostname="primary", port=5432, max_connections=50, weight=100, comment="primary" } + { hostgroup=1, hostname="primary", port=5432, max_connections=50, weight=100, comment="primary" } ) pgsql_users = ( - { username="appuser", password="appuser", default_hostgroup=10 }, - { username="postgres", password="changeme", default_hostgroup=10 } + { username="appuser", password="appuser", default_hostgroup=1 }, + { username="postgres", password="changeme", default_hostgroup=1 } ) pgsql_query_rules = diff --git a/postgresql/ha-proxysql-benchmark/docker-compose.yml b/postgresql/ha-proxysql-benchmark/docker-compose.yml index c5b0a2e..90f01e5 100644 --- a/postgresql/ha-proxysql-benchmark/docker-compose.yml +++ b/postgresql/ha-proxysql-benchmark/docker-compose.yml @@ -22,7 +22,7 @@ services: - 15432:5432 networks: pg-demo: - ipv4_address: 172.113.0.100 + ipv4_address: 10.0.0.1 command: > postgres -c wal_level=replica @@ -33,6 +33,7 @@ services: -c max_connections=50 -c shared_buffers=256MB -c work_mem=4MB + -c shared_preload_libraries='pg_stat_statements' replica1: image: postgres:17 @@ -53,7 +54,7 @@ services: - 25433:5432 networks: pg-demo: - ipv4_address: 172.113.0.101 + ipv4_address: 10.0.0.2 command: | bash -c " rm -rf /var/lib/postgresql/data/* @@ -64,7 +65,7 @@ services: done echo 'Backup done, starting replica...' chmod 0700 /var/lib/postgresql/data - postgres -c max_connections=50 + postgres -c max_connections=50 -c shared_preload_libraries='pg_stat_statements' " depends_on: primary: @@ -89,7 +90,7 @@ services: - 35433:5432 networks: pg-demo: - ipv4_address: 172.113.0.102 + ipv4_address: 10.0.0.3 command: | bash -c " rm -rf /var/lib/postgresql/data/* @@ -100,7 +101,7 @@ services: done echo 'Backup done, starting replica...' chmod 0700 /var/lib/postgresql/data - postgres -c max_connections=50 + postgres -c max_connections=50 -c shared_preload_libraries='pg_stat_statements' " depends_on: primary: @@ -127,7 +128,7 @@ services: - ./scripts/monitor-db-connections.sh:/usr/local/bin/monitor-db-connections.sh networks: pg-demo: - ipv4_address: 172.113.0.6 + ipv4_address: 10.0.0.20 sysbench2: build: @@ -150,7 +151,7 @@ services: - ./scripts/monitor-db-connections.sh:/usr/local/bin/monitor-db-connections.sh networks: pg-demo: - ipv4_address: 172.113.0.16 + ipv4_address: 10.0.0.21 sysbench3: build: @@ -173,7 +174,7 @@ services: - ./scripts/monitor-db-connections.sh:/usr/local/bin/monitor-db-connections.sh networks: pg-demo: - ipv4_address: 172.113.0.26 + ipv4_address: 10.0.0.22 proxysql: build: @@ -181,7 +182,7 @@ services: dockerfile: Dockerfile.proxysql container_name: proxysql depends_on: - primary: + replica1: condition: service_healthy environment: DB_USER: ${DB_USER} @@ -200,7 +201,7 @@ services: - proxysql-data:/var/lib/proxysql networks: pg-demo: - ipv4_address: 172.113.0.222 + ipv4_address: 10.0.0.222 volumes: primary-data: @@ -216,4 +217,5 @@ networks: driver: bridge ipam: config: - - subnet: 172.113.0.0/16 + - subnet: 10.0.0.0/16 + gateway: 10.0.0.254 diff --git a/postgresql/ha-proxysql-benchmark/run.sh b/postgresql/ha-proxysql-benchmark/run.sh index 4235c50..6eb72dd 100755 --- a/postgresql/ha-proxysql-benchmark/run.sh +++ b/postgresql/ha-proxysql-benchmark/run.sh @@ -2,11 +2,17 @@ . .envrc +if [ -n "${REBUILD:-}" ]; then + BUILD_ARGS="--no-cache" + COMPOSE_ARGS="--force-recreate" + echo "Hard rebuild with ${BUILD_ARGS} and compose with ${COMPOSE_ARGS}" +fi + echo "Building docker container" -docker compose build # --no-cache +docker compose build ${BUILD_ARGS} echo "Launching docker container(s)" -docker compose up -d # --force-recreate +docker compose up -d ${COMPOSE_ARGS} echo "Verifying docker processes" sleep 5 diff --git a/postgresql/ha-proxysql-benchmark/scripts/summary.sh b/postgresql/ha-proxysql-benchmark/scripts/summary.sh index 701c867..1d99b3b 100755 --- a/postgresql/ha-proxysql-benchmark/scripts/summary.sh +++ b/postgresql/ha-proxysql-benchmark/scripts/summary.sh @@ -2,21 +2,29 @@ set -euo pipefail [[ -n "${TRACE:-}" ]] && set -x - DBS=${DBS:-primary replica1 replica2} usage() { - echo "Usage: $0 {hosts-per-db|xxx}" + echo "Usage: $0 {hosts-per-db | queries-per-db}" exit 1 } hosts-per-db() { + for DB in ${DBS}; do + echo "" + echo "##### For DB '${DB}' #####" + echo "" + docker exec -it ${DB} psql -U ${DB_USER} -d ${DB_NAME} -P pager=off -c "SELECT client_addr, COUNT(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND client_addr IS NOT NULL GROUP BY client_addr ORDER BY 2 DESC" + done +} +queries-per-db() { for DB in ${DBS}; do echo "" echo "##### For DB '${DB}' #####" echo "" - docker exec -it ${DB} psql -U ${DB_USER} -d ${DB_NAME} -c "SELECT client_addr, COUNT(*) FROM pg_stat_activity WHERE pid <> pg_backend_pid() AND client_addr IS NOT NULL GROUP BY client_addr ORDER BY 2 DESC" + docker exec -it ${DB} psql -U ${DB_USER} -d ${DB_NAME} -P pager=off -c "SELECT calls, round(total_exec_time::numeric, 2) AS total_ms, round(mean_exec_time::numeric, 2) AS avg_ms, round(stddev_exec_time::numeric, 2) AS stddev_ms, rows, query FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;" + #docker exec -it ${DB} psql -U ${DB_USER} -d ${DB_NAME} -c "SELECT pid, now() - query_start AS duration, state, wait_event_type, wait_event, left(query, 120) AS query FROM pg_stat_activity WHERE state != 'idle' AND query_start IS NOT NULL ORDER BY query_start ASC;" done } @@ -29,14 +37,8 @@ main() { hosts-per-db) hosts-per-db ;; - stop) - do_stop - ;; - status) - do_status - ;; - restart) - do_restart + queries-per-db) + queries-per-db ;; *) echo "Unknown command: $1" diff --git a/postgresql/ha-proxysql-benchmark/sql/init-primary.sql b/postgresql/ha-proxysql-benchmark/sql/init-primary.sql index 033480e..f89791d 100644 --- a/postgresql/ha-proxysql-benchmark/sql/init-primary.sql +++ b/postgresql/ha-proxysql-benchmark/sql/init-primary.sql @@ -1,3 +1,5 @@ +CREATE EXTENSION IF NOT EXISTS pg_stat_statements; + -- WAL Replication CREATE USER repluser WITH replication encrypted password 'replpass'; SELECT pg_create_physical_replication_slot('replication_slot_1');