Sign inSign up

pgsharding/spqr-router

By pgsharding

•Updated about 13 hours ago

SPQR Router https://github.com/pg-sharding/spqr

Image
Databases & storage
1

50K+

pgsharding/spqr-router repository overview

github.com/pg-sharding/spqr⁠

GitHub Stars Docker Pulls

SPQR Logo

SPQR (Stateless Postgres Query Router) is a production-ready system for horizontal scaling of PostgreSQL via sharding. It acts as a transparent query router that distributes your queries across multiple PostgreSQL instances (shards) while maintaining full PostgreSQL protocol compatibility.

⁠How to use this image

⁠Start a spqr-router instance

Starting a SPQR router requires PostgreSQL shards to route queries to. The simplest setup uses two PostgreSQL instances:

# Create a network
docker network create spqr-net

# Start PostgreSQL shards
docker run -d --name shard1 --network spqr-net \
  -e POSTGRES_PASSWORD=password -e POSTGRES_USER=user1 -e POSTGRES_DB=db1 \
  postgres:15

docker run -d --name shard2 --network spqr-net \
  -e POSTGRES_PASSWORD=password -e POSTGRES_USER=user1 -e POSTGRES_DB=db1 \
  postgres:15

# Start SPQR router
docker run -d --name spqr-router --network spqr-net \
  -p 6432:6432 -p 7432:7432 \
  -v $(pwd)/router.yaml:/etc/spqr/router.yaml:ro \
  pgsharding/spqr-router:latest

This creates a SPQR router that listens on port 6432 for data queries and 7432 for administrative commands.

⁠... via docker compose

Example docker-compose.yml:

version: '3.8'

services:
  shard1:
    image: postgres:15
    environment:
      POSTGRES_USER: user1
      POSTGRES_PASSWORD: password
      POSTGRES_DB: db1

  shard2:
    image: postgres:15
    environment:
      POSTGRES_USER: user1
      POSTGRES_PASSWORD: password
      POSTGRES_DB: db1

  spqr-router:
    image: pgsharding/spqr-router:latest
    ports:
      - "6432:6432"  # Router port
      - "7432:7432"  # Admin console
    volumes:
      - ./router.yaml:/etc/spqr/router.yaml:ro
    depends_on:
      - shard1
      - shard2

Run docker compose up and your SPQR setup is ready.

⁠Configuration

SPQR is configured via a YAML file. Create router.yaml:

host: '0.0.0.0'
router_port: '6432'
admin_console_port: '7432'

router_mode: PROXY
log_level: info

frontend_rules:
  - usr: user1
    db: db1
    pool_mode: TRANSACTION
    auth_rule:
      auth_method: ok

shards:
  shard1:
    db: db1
    usr: user1
    pwd: password
    type: DATA
    tls:
      sslmode: disable
    hosts:
      - 'shard1:5432'
  shard2:
    db: db1
    usr: user1
    pwd: password
    type: DATA
    tls:
      sslmode: disable
    hosts:
      - 'shard2:5432'

backend_rules:
  - usr: user1
    db: db1
    connection_limit: 100

Mount this file when starting the container:

docker run -d --name spqr-router \
  -v $(pwd)/router.yaml:/etc/spqr/router.yaml:ro \
  pgsharding/spqr-router:latest

See the configuration documentation⁠ for all available options.

⁠Setting up sharding

After starting SPQR, connect to the admin console to configure sharding rules:

psql "host=localhost port=7432 user=user1 dbname=db1 sslmode=disable"

Create a distribution and define key ranges:

-- Create a distribution for integer-based sharding
CREATE DISTRIBUTION ds1 COLUMN TYPES integer;

-- Attach tables to the distribution
ALTER DISTRIBUTION ds1 ATTACH RELATION orders DISTRIBUTION KEY id;

-- Define key ranges (1-999 → shard1, 1000+ → shard2)
CREATE KEY RANGE kr1 FROM 1 ROUTE TO shard1 FOR DISTRIBUTION ds1;
CREATE KEY RANGE kr2 FROM 1000 ROUTE TO shard2 FOR DISTRIBUTION ds1;

Now connect to the router port and use it like a normal PostgreSQL database:

psql "host=localhost port=6432 user=user1 dbname=db1 sslmode=disable"
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer TEXT, total DECIMAL);

INSERT INTO orders VALUES (100, 'Alice', 150.00);   -- Routed to shard1
INSERT INTO orders VALUES (1500, 'Bob', 200.00);    -- Routed to shard2

SELECT * FROM orders WHERE id = 100;  -- Queries shard1
SELECT * FROM orders;                  -- Queries both shards

Queries are routed transparently based on your sharding configuration.

⁠Environment Variables

The image supports these environment variables:

  • SPQR_ROUTER_PORT - Override router port (default: from config)
  • SPQR_ADMIN_PORT - Override admin console port (default: from config)
  • SPQR_LOG_LEVEL - Set log level: debug, info, warn, error

⁠Exposed Ports

  • 6432 - Router port (PostgreSQL protocol) - Your applications connect here
  • 7432 - Admin console (PostgreSQL protocol) - Configure sharding rules
  • 7000 - gRPC API - Programmatic management and monitoring

⁠Image Variants

⁠pgsharding/spqr-router:<version>

This is the recommended image for production use. It contains the SPQR router binary and minimal runtime dependencies (~50MB).

⁠Supported tags
  • latest, stable - Latest stable release
  • v1.2.3, v1.2, v1 - Specific version tags
  • nightly - Built from master branch (latest features, for testing)
  • nightly-<git-hash> - Specific nightly build
  • prerelease - Latest pre-release (alpha, beta, rc)

⁠User Feedback

⁠Issues

If you encounter any problems with the image, please file an issue on GitHub⁠.

For general usage questions and discussions, join our Telegram chat⁠.

⁠Contributing

SPQR is open source and welcomes contributions! See the GitHub repository⁠ for more information.

⁠License

The SPQR source code is distributed under the PostgreSQL Global Development Group License.

Tag summary

Content type

Image

Digest

sha256:163a1f0fe…

Size

17.7 MB

Last updated

about 13 hours ago

docker pull pgsharding/spqr-router:nightly-3.1.1-2870-df9371ab