|
| 1 | +import psycopg2 |
| 2 | +import pytest |
| 3 | +from globals import admin |
| 4 | + |
| 5 | +queries = [ |
| 6 | + "SELECT 1", |
| 7 | + "CREATE TABLE IF NOT EXISTS test_conn_reads(id BIGINT)", |
| 8 | + "INSERT INTO test_conn_reads (id) VALUES (1)", |
| 9 | + "INSERT INTO test_conn_reads (id) VALUES (2)", |
| 10 | + "INSERT INTO test_conn_reads (id) VALUES (3)", |
| 11 | + "SELECT * FROM test_conn_reads WHERE id = 1", |
| 12 | + "SELECT * FROM test_conn_reads WHERE id = 2", |
| 13 | + "SELECT * FROM test_conn_reads WHERE id = 3", |
| 14 | + "SET work_mem TO '4MB'", |
| 15 | + "SET work_mem TO '6MB'", |
| 16 | + "SET work_mem TO '8MB'", |
| 17 | +] |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def conn_reads(): |
| 22 | + return psycopg2.connect( |
| 23 | + "host=127.0.0.1 port=6432 user=pgdog password=pgdog " |
| 24 | + "options='-c pgdog.role=replica'" |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def conn_writes(): |
| 30 | + return psycopg2.connect( |
| 31 | + "host=127.0.0.1 port=6432 user=pgdog password=pgdog " |
| 32 | + "options='-c pgdog.role=primary'" |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def conn_default(): |
| 38 | + return psycopg2.connect("host=127.0.0.1 port=6432 user=pgdog password=pgdog") |
| 39 | + |
| 40 | + |
| 41 | +def test_conn_writes(conn_writes): |
| 42 | + admin().execute("SET query_parser TO 'off'") |
| 43 | + for query in queries: |
| 44 | + conn_writes.autocommit = True |
| 45 | + cursor = conn_writes.cursor() |
| 46 | + cursor.execute(query) |
| 47 | + admin().execute("SET query_parser TO 'auto'") |
| 48 | + |
| 49 | + |
| 50 | +def test_conn_reads(conn_reads, conn_writes): |
| 51 | + admin().execute("SET query_parser TO 'off'") |
| 52 | + |
| 53 | + conn_writes.autocommit = True |
| 54 | + conn_reads.autocommit = True |
| 55 | + |
| 56 | + conn_writes.cursor().execute( |
| 57 | + "CREATE TABLE IF NOT EXISTS test_conn_reads(id BIGINT)" |
| 58 | + ) |
| 59 | + |
| 60 | + read = False |
| 61 | + for query in queries: |
| 62 | + cursor = conn_reads.cursor() |
| 63 | + try: |
| 64 | + cursor.execute(query) |
| 65 | + except psycopg2.errors.ReadOnlySqlTransaction: |
| 66 | + # Some will succeed because we allow reads |
| 67 | + # on the primary. |
| 68 | + read = True |
| 69 | + admin().execute("SET query_parser TO 'auto'") |
| 70 | + |
| 71 | + conn_writes.cursor().execute("DROP TABLE IF EXISTS test_conn_reads") |
| 72 | + assert read, "expected some queries to hit replicas and fail" |
| 73 | + |
| 74 | + |
| 75 | +def test_transactions_writes(conn_writes): |
| 76 | + admin().execute("SET query_parser TO 'off'") |
| 77 | + |
| 78 | + for query in queries: |
| 79 | + conn_writes.cursor().execute(query) |
| 80 | + conn_writes.commit() |
| 81 | + |
| 82 | + admin().execute("SET query_parser TO 'auto'") |
| 83 | + |
| 84 | + |
| 85 | +def test_transactions_reads(conn_reads): |
| 86 | + admin().execute("SET query_parser TO 'off'") |
| 87 | + read = False |
| 88 | + |
| 89 | + for query in queries: |
| 90 | + try: |
| 91 | + conn_reads.cursor().execute(query) |
| 92 | + except psycopg2.errors.ReadOnlySqlTransaction: |
| 93 | + # Some will succeed because we allow reads |
| 94 | + # on the primary. |
| 95 | + read = True |
| 96 | + conn_reads.commit() |
| 97 | + |
| 98 | + assert read, "expected some queries to hit replicas and fail" |
| 99 | + admin().execute("SET query_parser TO 'auto'") |
| 100 | + |
| 101 | + |
| 102 | +def test_transaction_reads_explicit(conn_reads, conn_writes): |
| 103 | + conn_reads.autocommit = True |
| 104 | + admin().execute("SET query_parser TO 'off'") |
| 105 | + |
| 106 | + conn_writes.cursor().execute( |
| 107 | + "CREATE TABLE IF NOT EXISTS test_conn_reads(id BIGINT)" |
| 108 | + ) |
| 109 | + conn_writes.commit() |
| 110 | + |
| 111 | + cursor = conn_reads.cursor() |
| 112 | + |
| 113 | + read = False |
| 114 | + |
| 115 | + for _ in range(15): |
| 116 | + cursor.execute("BEGIN") |
| 117 | + try: |
| 118 | + cursor.execute("INSERT INTO test_conn_reads (id) VALUES (1)") |
| 119 | + cursor.execute("COMMIT") |
| 120 | + except psycopg2.errors.ReadOnlySqlTransaction: |
| 121 | + read = True |
| 122 | + cursor.execute("ROLLBACK") |
| 123 | + |
| 124 | + assert read, "expected some queries to hit replicas and fail" |
| 125 | + |
| 126 | + for _ in range(15): |
| 127 | + cursor.execute("BEGIN READ ONLY") # Won't be parsed, doesn't matter to PgDog |
| 128 | + cursor.execute("SELECT 1") |
| 129 | + cursor.execute("ROLLBACK") |
| 130 | + |
| 131 | + admin().execute("SET query_parser TO 'on'") |
0 commit comments