Skip to main content

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. Install it via the [pool] extra:

pip install "psycopg-yugabytedb[pool]"

That pulls our driver plus the unmodified upstream psycopg-pool; the pool’s import psycopg resolves to our driver and every conn the pool opens goes through the dispatcher.

Install without the [pool] extra with pip install psycopg-yugabytedb, or pin to a specific version (3.3.4.1 is the first GA release). The fork cannot coexist with upstream psycopg in the same environment — both install into site-packages/psycopg/.

Logging

Every smart-driver module logs to its own logger under psycopg.yb.*. Levels follow the standard library, plus a custom TRACE level (numeric 5, finer than DEBUG) for counter-mutation-level firehose detail.

  • WARNING — driver gave up (no eligible nodes, control conn re-open failed)

  • INFO — cluster bootstrap, topology change observed, host quarantined, control conn re-opened on a survivor

  • DEBUG — per-pick, per-refresh, per-control-conn open/close

  • TRACE — every counter increment / decrement, every filtered candidate

Enable:

import logging
from psycopg.yb import TRACE

logging.basicConfig(level=logging.INFO)

# Lifecycle events only (default INFO above is fine):
logging.getLogger("psycopg.yb").setLevel(logging.INFO)

# Per-operation debug:
logging.getLogger("psycopg.yb").setLevel(logging.DEBUG)

# Full firehose:
logging.getLogger("psycopg.yb").setLevel(TRACE)

# Or narrow to one subsystem:
logging.getLogger("psycopg.yb.policy").setLevel(logging.DEBUG)

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

Metadata

Release files for psycopg-yugabytedb 3.3.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for psycopg-yugabytedb 3.3.4.1
File Size Uploaded
psycopg_yugabytedb-3.3.4.1.tar.gz 185.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for psycopg-yugabytedb 3.3.4.1
File Interpreter ABI Platform
psycopg_yugabytedb-3.3.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 421.1 kB

Release files / psycopg_yugabytedb-3.3.4.1.tar.gz

Download URL psycopg_yugabytedb-3.3.4.1.tar.gz
Size 185.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5a73679d756cae0f59412a42450b5a8a4263eb36cc9e3d1b11be64b5307c8a60
BLAKE2b-256 checksum
How to use checksums
45e6da838ceaee091ef51d87a817737b0dd11c981937331e8cbebcba1df08213
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / psycopg_yugabytedb-3.3.4.1-py3-none-any.whl

Download URL psycopg_yugabytedb-3.3.4.1-py3-none-any.whl
Size 236.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
074cc9a591fac92338c87b248661c225e89ca5b4a77de3078908453d852d0401
BLAKE2b-256 checksum
How to use checksums
04d9269692c3705d54a94e1662dafc29e6a4451cd8b4bf64722be23bbdffa159
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release history Release notifications | RSS feed

This release

3.3.4.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page