Skip to main content

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. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

psycopg_yugabytedb-3.3.4.1.tar.gz (185.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

psycopg_yugabytedb-3.3.4.1-py3-none-any.whl (236.1 kB view details)

Uploaded Python 3

File details

Details for the file psycopg_yugabytedb-3.3.4.1.tar.gz.

File metadata

  • Download URL: psycopg_yugabytedb-3.3.4.1.tar.gz
  • Upload date:
  • Size: 185.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for psycopg_yugabytedb-3.3.4.1.tar.gz
Algorithm Hash digest
SHA256 5a73679d756cae0f59412a42450b5a8a4263eb36cc9e3d1b11be64b5307c8a60
MD5 453f996d4ba1e8adc65618258878d5c7
BLAKE2b-256 45e6da838ceaee091ef51d87a817737b0dd11c981937331e8cbebcba1df08213

See more details on using hashes here.

File details

Details for the file psycopg_yugabytedb-3.3.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for psycopg_yugabytedb-3.3.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 074cc9a591fac92338c87b248661c225e89ca5b4a77de3078908453d852d0401
MD5 2368537dbb2c3a4c7f5a0c42352a574a
BLAKE2b-256 04d9269692c3705d54a94e1662dafc29e6a4451cd8b4bf64722be23bbdffa159

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page