Skip to main content
Automated test status (Linux and MacOS) Automated test status (Windows) Test coverage

triopg

Welcome to triopg!

PostgreSQL client for Trio based on asyncpg.

License: Your choice of MIT or Apache License 2.0

Quick example:

import trio_asyncio
import triopg


async def main():
    async with triopg.connect() as conn:

        await conn.execute(
            """
            DROP TABLE IF EXISTS users;
            CREATE TABLE IF NOT EXISTS users (
                _id SERIAL PRIMARY KEY,
                user_id VARCHAR(32) UNIQUE
            )"""
        )

        async with conn.transaction():
            await conn.execute("INSERT INTO users (user_id) VALUES (1)")
            await conn.execute("INSERT INTO users (user_id) VALUES (2)")
            await conn.execute("INSERT INTO users (user_id) VALUES (3)")

        print(await conn.fetch("SELECT * FROM users"))


trio_asyncio.run(main)

API basics

triopg is a thin Trio-compatible wrapper around asyncpg. The API is the same, with one exception - triopg does not support manual resource management. In asyncpg you can manage pools, connections and transactions manually:

conn = await asyncpg.connect()
tr = conn.transaction()
# ..
tr.commit()
conn.close()

While in triopg you can only use async with blocks:

async with triopg.connect() as conn:
    async with conn.transaction():
        # ...

Otherwise you can follow asyncpg tutorial and reference. Everything should work the same way. Please file an issue if it doesn’t.

Helpers

In addition to asyncpg-compatible API, triopg provides Trio-style .listen() helper for the eponymous Postgres statement:

async with conn.listen('some.channel', max_buffer_size=1) as notifications:
    async for notification in notifications:
        if notification != triopg.NOTIFY_OVERFLOW:
            print('Notification received:', notification)

max_buffer_size is the amount of notifications you are willing to queue in memory.

If you don’t want to think about buffering, set the buffer size to math.inf and everything will just work in regular non-pathological situations.

Otherwise, you can set a finite buffer. In this case you should handle triopg.NOTIFY_OVERFLOW marker and react according to your use case. For example, you could re-scan the tables, like you would do at startup. Or could you simply ignore the marker if you are only interested in the newest notifications.

For detailed discussion on buffering, see Trio manual, “Buffering in channels” section.

Note: we can’t politely ask Postgres to slow down: LISTEN backpressure is not supported by asyncpg. There’s also an inherent challenge with Postgres. Postgres (like most broadcast systems) doesn’t really have a good way to communicate backpressure further upstream to the clients that are calling NOTIFY.

Metadata

Release files for triopg 0.6.0

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

Source distribution (sdist)

Source distribution for triopg 0.6.0
File Size Uploaded
triopg-0.6.0.tar.gz 19.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for triopg 0.6.0
File Interpreter ABI Platform
triopg-0.6.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.5 kB

Release files / triopg-0.6.0.tar.gz

Download URL triopg-0.6.0.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ead3505aa702da02e4ec78710385cfa3c216747279d0ab4e6c37fff4236ff162
BLAKE2b-256 checksum
How to use checksums
5aa90b2d1d66d922f4712c710de87c90adf073e252737a846e77b981eeb95688
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

Release files / triopg-0.6.0-py3-none-any.whl

Download URL triopg-0.6.0-py3-none-any.whl
Size 13.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a65147183c0992d0902f6acdeb2b177bb23efbff1ca971ee4f480d02c9cd5442
BLAKE2b-256 checksum
How to use checksums
9579adb39234737385586bb407ed854355556120db982074724b972be30d7b18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.6.0 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

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