Scripts2026年5月17日·1 分钟阅读

PgCat — PostgreSQL Pooler with Sharding and Load Balancing

A high-performance PostgreSQL connection pooler written in Rust that supports sharding, read/write splitting, load balancing, and automatic failover.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
PgCat Overview
通用 CLI 安装命令
npx tokrepo install 30d2ad67-51a8-11f1-9bc6-00163e2b0d79

Introduction

PgCat is a PostgreSQL connection pooler and proxy written in Rust. It goes beyond simple pooling by offering built-in sharding, read/write query splitting, load balancing across replicas, and automatic failover, making it suitable for scaling PostgreSQL horizontally.

What PgCat Does

  • Pools client connections to reduce backend connection overhead
  • Routes read queries to replicas and writes to the primary automatically
  • Shards data across multiple PostgreSQL instances using configurable strategies
  • Performs health checks and automatic failover when a backend goes down
  • Supports both transaction-mode and session-mode pooling

Architecture Overview

PgCat runs as a single async Rust binary using Tokio. It accepts PostgreSQL wire protocol connections from clients and maintains pools of backend connections to one or more PostgreSQL servers. A router layer inspects queries to determine read vs write intent and selects the appropriate backend. Sharding logic hashes a configurable key to route queries to the correct shard.

Self-Hosting & Configuration

  • Deploy via Docker or compile from source with cargo build --release
  • Configure in pgcat.toml with pools, users, shards, and replicas
  • Set pool mode to transaction (default) or session per pool
  • Enable query routing with primary_reads_enabled and replica settings
  • Monitor via the built-in admin database accessible through SQL commands

Key Features

  • Transaction-mode pooling with up to 10,000+ client connections per instance
  • Automatic read/write splitting without application changes
  • Sharding support with consistent hashing or range-based routing
  • Health checks with configurable intervals and automatic ban/unban of failing backends
  • Zero-downtime configuration reloads via SIGHUP

Comparison with Similar Tools

  • PgBouncer — industry standard pooler but no sharding, no read/write splitting
  • Odyssey — multi-threaded pooler by Yandex, no built-in sharding
  • pgpool-II — feature-rich but complex configuration and single-threaded query parsing
  • Supavisor — Elixir-based pooler from Supabase, focused on multi-tenant cloud
  • HAProxy — generic TCP load balancer, no PostgreSQL protocol awareness

FAQ

Q: Can PgCat replace PgBouncer? A: Yes for most use cases. PgCat supports transaction-mode pooling like PgBouncer and adds features like sharding and read/write splitting.

Q: How does automatic failover work? A: PgCat health-checks backends at configurable intervals. If a primary fails health checks, it is banned and clients receive errors until it recovers or a new primary is promoted.

Q: Does PgCat support prepared statements? A: Yes in session mode. Transaction mode has limited prepared statement support due to connection multiplexing constraints.

Q: What is the performance overhead? A: Minimal. PgCat adds sub-millisecond latency per query due to its async Rust architecture with zero-copy parsing.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产