YugabyteDB smart-driver fork of psycopg 3 — adds cluster-aware and topology-aware load balancing
Project description
Psycopg 3 is a modern implementation of a PostgreSQL adapter for Python. This is the YugabyteDB smart-driver fork, distributed as psycopg-yugabytedb.
This distribution contains the pure Python package psycopg.
YugabyteDB smart driver
This fork (distribution name psycopg-yugabytedb) extends psycopg 3 with cluster-aware and topology-aware connection load balancing for YugabyteDB. The import name stays psycopg — existing code is unchanged. Adding load_balance_hosts=true to the conninfo string opts into the smart driver.
Opt in with one parameter:
import psycopg
conn = psycopg.connect(
"host=h1,h2,h3 port=5433 user=yugabyte dbname=yugabyte "
"load_balance_hosts=true"
)
The driver discovers every live tserver via yb_servers() on the first connect, then distributes subsequent connects across them with a least-loaded picker (random tie-break, atomic under a per-cluster lock). One contact point in host=… is enough to bootstrap; the rest of the cluster is discovered.
Smart-driver conninfo parameters:
Parameter |
Values |
Default |
Description |
|---|---|---|---|
load_balance_hosts |
true / false / disable / random |
absent |
true enables the smart driver. false is equivalent to libpq’s disable. disable / random pass through to libpq unchanged. |
topology_keys |
cloud.region.zone (comma-separated for multiple keys) |
none |
Restrict picks to tservers matching at least one placement. Zone may be * for any zone in that cloud/region. Cloud / region wildcards are rejected. Strict in v1 — no cluster-wide fallback if all matching nodes are down. |
yb_servers_refresh_interval |
seconds (integer) |
300 |
How often to re-query yb_servers(). Clamped to [0, 600]. |
failed_host_reconnect_delay_secs |
seconds (integer) |
5 |
How long to quarantine a node after a failed connect, before reconsidering it. Clamped to [0, 60]. |
Topology-aware example — bind traffic to one zone:
conn = psycopg.connect(
"host=h1,h2,h3 port=5433 user=yugabyte dbname=yugabyte "
"load_balance_hosts=true "
"topology_keys=cloud1.datacenter1.zoneA"
)
The upstream psycopg-pool connection pool works unchanged with the smart driver — the dispatcher sits underneath the pool, so pool-managed connections honour the configured policy too.
The fork is pre-release. Install with pip install --pre psycopg-yugabytedb or pin to 3.3.4.1rc1. It cannot coexist with upstream psycopg in the same environment (both install into site-packages/psycopg/).
Installation
In short, run the following:
pip install --upgrade pip # to upgrade pip pip install "psycopg[binary,pool]" # to install package and dependencies
If something goes wrong, and for more information about installation, please check out the Installation documentation.
Hacking
For development information check out the project readme.
Copyright (C) 2020 The Psycopg Team
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file psycopg_yugabytedb-3.3.4.1rc1.tar.gz.
File metadata
- Download URL: psycopg_yugabytedb-3.3.4.1rc1.tar.gz
- Upload date:
- Size: 181.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da4cebac1cfd8e48f1dc72c83912916660ed42d214b04632477ec52244b0906f
|
|
| MD5 |
3a6d37e1151c188aa14a2ee73160e484
|
|
| BLAKE2b-256 |
eaf6f06ea4db7781fb9f85d135dea3b472bb5e9ba6deecd095569e76c29f2f05
|
File details
Details for the file psycopg_yugabytedb-3.3.4.1rc1-py3-none-any.whl.
File metadata
- Download URL: psycopg_yugabytedb-3.3.4.1rc1-py3-none-any.whl
- Upload date:
- Size: 232.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ce9c0c8fd1be9117488a65643ca85876e5f496f3f6cc24e77537fbeee08de6e4
|
|
| MD5 |
3004663d21abbbc12bcba4c9ae9f39ed
|
|
| BLAKE2b-256 |
e102b6b846908543104be72b437e9a19d4e65b8496933038c5be863b34925898
|