fix: implement libpq semantic for target_session_attrs=prefer-standby#1022
Merged
fix: implement libpq semantic for target_session_attrs=prefer-standby#1022
Conversation
First attempt all the servers in standby mode, then fall back to any mode. Fix #1021.
Member
Author
|
Note: there are both connection tests to verify that the libpq filters the right server (within reason: we only test with a primary server) and unittest level to verify that the attamps made in |
Only one test fails with the currently "oldest" libpq, which is 10.2, however the docs in libpq 13 are not explicit in which modes are supported and, afaics, 'replica' shouldn't work either. https://www.postgresql.org/docs/13/libpq-connect.html
|
This is looking good to me so far. Using the below code to test. Have not looked as async yet though, can take a closer look tommorow. import psycopg
import pytest
def test(hosts: str, ports: str, target_session_attrs: str, expected_port: int) -> None:
print(target_session_attrs)
# Connect to an existing database
if expected_port:
with psycopg.connect(f"host={hosts} port={ports} dbname=postgres user=andrew target_session_attrs={target_session_attrs}") as conn:
# Open a cursor to perform database operations
with conn.cursor() as cur:
cur.execute("select setting from pg_settings where name = 'port'")
port = cur.fetchone()[0]
assert expected_port == int(port)
else:
with pytest.raises(Exception):
psycopg.connect(
f"host={hosts} port={ports} dbname=postgres user=andrew target_session_attrs={target_session_attrs}")
def test_all() -> None:
test(hosts="localhost,localhost", ports="5432,5433",
target_session_attrs="primary", expected_port=5432)
test(hosts="localhost,localhost", ports="5432,5433",
target_session_attrs="read-write", expected_port=5432)
test(hosts="localhost,localhost", ports="5432,5433",
target_session_attrs="standby", expected_port=5433)
test(hosts="localhost,localhost", ports="5432,5433",
target_session_attrs="prefer-standby", expected_port=5433)
test(hosts="localhost,localhost", ports="5432,5433",
target_session_attrs="read-only", expected_port=5433)
test(hosts="localhost", ports="5432",
target_session_attrs="primary", expected_port=5432)
test(hosts="localhost", ports="5432",
target_session_attrs="read-write", expected_port=5432)
test(hosts="localhost", ports="5432",
target_session_attrs="standby", expected_port=None)
test(hosts="localhost", ports="5432",
target_session_attrs="prefer-standby", expected_port=5432)
test(hosts="localhost", ports="5432",
target_session_attrs="read-only", expected_port=None)
test(hosts="localhost", ports="5433",
target_session_attrs="primary", expected_port=None)
test(hosts="localhost", ports="5433",
target_session_attrs="read-write", expected_port=None)
test(hosts="localhost", ports="5433",
target_session_attrs="standby", expected_port=5433)
test(hosts="localhost", ports="5433",
target_session_attrs="prefer-standby", expected_port=5433)
test(hosts="localhost", ports="5433",
target_session_attrs="read-only", expected_port=5433)
if __name__ == "__main__":
test_all() |
|
Thank you very much for implementing this fix. Looking forward to the next release. |
Member
Author
|
@AndrewJackson2020 no worries and thank you for the report and fix proposal! Psycopg 3.2.6 with this fix will hit PyPI in a few minutes! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First attempt all the servers in standby mode, then fall back to any mode.
Fix #1021.
CC. @AndrewJackson2020