Skip to main content

Sharding with SQLAlchemy

Project description

The module consists of two layers:

  • ShardProxy class that just manages connections.

  • ops module that implements helper classes for parallelism and result processing.

ShardProxy

Initialize:

SHARD_URLS = [
    "postgresql+asyncpg://server/shard0",
    "postgresql+asyncpg://server/shard1",
]
proxy = ShardProxy(SHARD_URLS)

Process request:

async def fetch_user(user_id: UUID):
    async with proxy.connect(user_id) as conn:
        async with conn.begin():
            stmt = select(User).where(User.id == user_id)
            res = await conn.execute(stmt)
            return res.all()

Operations

Select bunch of IDs:

UserRow = Row[tuple[User]]

class SelectManyUsers(RunOnKeysOperation[UserRow]):
    async def process(
        self, conn: AsyncConnection, keys: list[UUID]
    ) -> ResultRows[UserRow]:
        stmt = select(User).where(User.id.in_(keys))
        res = await conn.execute(stmt)
        return res.all()

users = await SelectManyUsers().run(proxy)

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

shardproxy-1.0.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

shardproxy-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file shardproxy-1.0.0.tar.gz.

File metadata

  • Download URL: shardproxy-1.0.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for shardproxy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3946037adb640e9ce6bc03021de536fc63ca8eb2b2b8f0c2ce6ca14af77ef5ce
MD5 fa2b0e99fb27fc3a7fd591e8ecd9fce3
BLAKE2b-256 563913b7eb3adf2c631f18ae2cca79d0744b0dfb9c23f457047174195a360efe

See more details on using hashes here.

Provenance

The following attestation bundles were made for shardproxy-1.0.0.tar.gz:

Publisher: release.yml on archisle/shardproxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shardproxy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: shardproxy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for shardproxy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c41ca7470ec3aeaae9a6f92c415bdd6dae473b73e7d2d07715c659307c24e35d
MD5 c567e80d3fe310ed787d3e758e9e76c0
BLAKE2b-256 0abc2c6f2b8c13879b5712b6b695f5a3f3806a4605fcdcc7f5d9ac937c6b2299

See more details on using hashes here.

Provenance

The following attestation bundles were made for shardproxy-1.0.0-py3-none-any.whl:

Publisher: release.yml on archisle/shardproxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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