Skip to main content

asyncpg-lostream

CRUD on PostgreSQL large objects using async drivers and asyncio. It is tested against SQLAlchemy.

Purpose

Synchronous drivers such as psycopg2 support large objects via a file-like interface utilizing libpg. Unfortunately, async drivers such as asyncpg do not support large objects at all (at this time). This module is designed to provide an interface for PostgreSQL large objects that is compatible with asyncio and async database drivers. This interface is achieved by calling the supporting PostgreSQL stored functions that are an interface to the large object store.

This interface is designed to operate on data by reading and writing in chunks so as to stream the data to and from the large object store.

This codebase is not tied or affiliated with asyncpg. It does utilize SQLAlchemy's AsyncConnection class in its typing.

PostgreSQL Large Objects

A large object is a special store in PostgreSQL that operates similarly to a file. The large object itself is the record. The database data type is bytea (bytes in Python). The interface operates as a read and write to the allocated large object via read and write functions. (There are more, but they are out of scope for this project.) The data type of the large object id is oid. The tables used by PostgreSQL are pg_largeobject and pg_largeobject_metadata. The pg_largeobject table holds the data itself. The pg_largeobject_metadata has a link to the owner oid. These two tables are linked by the oid reference.

When assiciating the large object to a table record, add an oid type column to hold the allocated large object oid value from a created large object.

See the PostgreSQL documentation here.

Utilization

Explicit Create

from asyncpg_lostream.lostream import PGLargeObject

# It is the responsibility of the caller to resolve how an
# AsyncEngine is created and how an AsyncConnection is created.

# Create a large object
lob_oid = await PGLargeObject.create_large_object(db)

# Open a large object for read and write
pgl = PGLargeObject(async_connection, lob_oid, mode="rw")

with open("my_data.txt", "r") as data:
    for buff in data.read(pgl.chunk_size):
        await pgl.write(buff.encode)

pgl.close()

Context Manager Create

from asyncpg_lostream.lostream import PGLargeObject

# It is the responsibility of the caller to resolve how an
# AsyncEngine is created and how an AsyncConnection is created.

with open("my_data.txt", "r") as data:
    async with PGLargeObject(async_connection, 0, "w") as pgl:
        for buff in data.read(pgl.chunk_size):
            await pgl.write(buff.encode())

Context manager read

from asyncpg_lostream.lostream import PGLargeObject

# It is the responsibility of the caller to resolve how an
# AsyncEngine is created and how an AsyncConnection is created.

async with PGLargeObject(async_connection, existing_lob_oid, "r") as pgl:
    async for buff in pgl:
        print(buff.decode())

Development

Environment

  1. Create a virtual environment
    python -m venv /path/to/venv
    
  2. Activate the virtual environment
    source /path/to/venv/bin/activate
    
  3. Ensure pip is up to date
    pip install --upgrade pip
    
  4. Install packages from requirements.txt
    pip install -r requirements.txt
    
  5. Install pre-config
    pre-config install
    

Development

make, docker-compose and docker are required for development.

To list the make targets, use make help

To start a local PostgreSQL 13 container: make start_db

To shutdown the local PostgreSQL 13 container: make stop_db

After making changes, create your unit tests in the asyncpg-lostream/tests directory.

Test your changes with the command make test

Packaging

If you intend to make a releaase, change the version in the setup.cfg file. This value will be copied to the module's __version__ file.

Build the package using make build. This will run the tests and then build the artifacts. These will be put into the dist/ directory.

For instructions on upload to PyPI, see the Packaging Python Projects Dcoumentation

Release files for asyncpg-lostream 0.1.2

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

Source distribution (sdist)

Source distribution for asyncpg-lostream 0.1.2
File Size Uploaded
asyncpg-lostream-0.1.2.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asyncpg-lostream 0.1.2
File Interpreter ABI Platform
asyncpg_lostream-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 21.6 kB

Release files / asyncpg-lostream-0.1.2.tar.gz

Download URL asyncpg-lostream-0.1.2.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
fd911c1bb4e843b048bfaf87890d41a58167eef69385b9719f478472052723ee
BLAKE2b-256 checksum
How to use checksums
93174dbab3eec41efde274b7903abad9bdea2db144fe77a2eeb370e805d62003
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release files / asyncpg_lostream-0.1.2-py3-none-any.whl

Download URL asyncpg_lostream-0.1.2-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9db9e2d1652b5c63032634ae97c9cbc1574a606e09af7679d8d0a57e3b7218d8
BLAKE2b-256 checksum
How to use checksums
fc0e252c207751dc1b4b78a51ec3da366c35ca6d17979da20049aa79f229d885
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

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