Skip to main content

An asyncio PostgreSQL driver that returns numpy arrays

Project description

GitHub Actions status https://img.shields.io/pypi/v/asyncpg-rkt.svg

asyncpg-rkt is a fork of asyncpg, a database interface library designed specifically for PostgreSQL and Python/asyncio. asyncpg is an efficient, clean implementation of PostgreSQL server binary protocol for use with Python’s asyncio framework. You can read more about asyncpg in an introductory blog post.

asyncpg-rkt extends asyncpg as follows: - Backward compatible with the origin. - It is possible to set the numpy dtype for the fetched query. - Such “typed” queries return numpy arrays instead of lists of Record objects. - We construct numpy arrays directly from the low-level PostgreSQL protocol, without materializing any Python objects. - Although, we support object fields, too. - The time from receiving the response from PostgreSQL server until Connection.fetch() returns is ~20x less. This is because we avoid the overhead of dealing with Python objects in the result. - We return ravel()-ed indexes of nulls while writing NaN-s/NaT-s at the corresponding places in the array. - There is an option to return data by column vs. by row.

asyncpg-rkt provides the best performance when there are thousands of rows returned and the field types map to numpy.

Read the blog post with the introduction.

asyncpg-🚀 requires Python 3.7 or later and is supported for PostgreSQL versions 9.5 to 14. Older PostgreSQL versions or other databases implementing the PostgreSQL protocol may work, but are not being actively tested.

Documentation

The project documentation can be found here.

See below about how to use the fork’s special features.

Performance

In our testing asyncpg is, on average, 3x faster than psycopg2 (and its asyncio variant – aiopg).

https://raw.githubusercontent.com/athenianco/asyncpg-rkt/master/performance.png

The above results are a geometric mean of benchmarks obtained with PostgreSQL client driver benchmarking toolbench in November 2020 (click on the chart to see full details).

Further improvement from writing numpy arrays is ~20x:

https://raw.githubusercontent.com/athenianco/asyncpg-rkt/master/benchmark_20220522_142813.svg https://raw.githubusercontent.com/athenianco/asyncpg-rkt/master/benchmark_20220522_143838.svg

Features

asyncpg implements PostgreSQL server protocol natively and exposes its features directly, as opposed to hiding them behind a generic facade like DB-API.

This enables asyncpg to have easy-to-use support for:

  • prepared statements

  • scrollable cursors

  • partial iteration on query results

  • automatic encoding and decoding of composite types, arrays, and any combination of those

  • straightforward support for custom data types

Installation

asyncpg-🚀 is available on PyPI and requires numpy 1.21+. Use pip to install:

$ pip install asyncpg-rkt

Basic Usage

import asyncio
import asyncpg
from asyncpg.rkt import set_query_dtype
import numpy as np

async def run():
    conn = await asyncpg.connect(user='user', password='password',
                                 database='database', host='127.0.0.1')
    dtype = np.dtype([
        ("a", int),
        ("b", "datetime64[s]"),
    ])
    array, nulls = await conn.fetch(
        set_query_dtype('SELECT * FROM mytable WHERE id = $1', dtype),
        10,
    )
    await conn.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

License

asyncpg-🚀 is developed and distributed under the Apache 2.0 license, just like the original project.

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

asyncpg-rkt-0.26.2.tar.gz (883.6 kB view details)

Uploaded Source

Built Distributions

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

asyncpg_rkt-0.26.2-cp311-cp311-win_amd64.whl (544.2 kB view details)

Uploaded CPython 3.11Windows x86-64

asyncpg_rkt-0.26.2-cp311-cp311-musllinux_1_1_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

asyncpg_rkt-0.26.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

asyncpg_rkt-0.26.2-cp311-cp311-macosx_10_9_x86_64.whl (666.3 kB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

asyncpg_rkt-0.26.2-cp310-cp310-win_amd64.whl (552.8 kB view details)

Uploaded CPython 3.10Windows x86-64

asyncpg_rkt-0.26.2-cp310-cp310-musllinux_1_1_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

asyncpg_rkt-0.26.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

asyncpg_rkt-0.26.2-cp310-cp310-macosx_10_9_x86_64.whl (681.3 kB view details)

Uploaded CPython 3.10macOS 10.9+ x86-64

asyncpg_rkt-0.26.2-cp39-cp39-win_amd64.whl (567.1 kB view details)

Uploaded CPython 3.9Windows x86-64

asyncpg_rkt-0.26.2-cp39-cp39-musllinux_1_1_x86_64.whl (3.4 MB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

asyncpg_rkt-0.26.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

asyncpg_rkt-0.26.2-cp39-cp39-macosx_10_9_x86_64.whl (700.3 kB view details)

Uploaded CPython 3.9macOS 10.9+ x86-64

asyncpg_rkt-0.26.2-cp38-cp38-win_amd64.whl (568.0 kB view details)

Uploaded CPython 3.8Windows x86-64

asyncpg_rkt-0.26.2-cp38-cp38-musllinux_1_1_x86_64.whl (3.8 MB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

asyncpg_rkt-0.26.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.3 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

asyncpg_rkt-0.26.2-cp38-cp38-macosx_10_9_x86_64.whl (691.0 kB view details)

Uploaded CPython 3.8macOS 10.9+ x86-64

File details

Details for the file asyncpg-rkt-0.26.2.tar.gz.

File metadata

  • Download URL: asyncpg-rkt-0.26.2.tar.gz
  • Upload date:
  • Size: 883.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for asyncpg-rkt-0.26.2.tar.gz
Algorithm Hash digest
SHA256 a547b72d753a6a0f4a38d15371fbbeb3e4fdcefcc80e1c2eaaf6cd7704eeb5ee
MD5 f9e07066e5b4d7acaf171e56dbfb7f78
BLAKE2b-256 3d0665cd5887c1a54d46572c8df69a199dbd6378ccb4f070cab614e75ea1b814

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 059ecdc2b0f7ee50dbf52122c562858bdf82fc84552959a81d1410bafc7a6f02
MD5 bc38b0da666b4bf72d3a39e7990a2436
BLAKE2b-256 2193a635b49858494b7cec45469a0a93e09cfbcb3ccb2df9d9c80d64d9ff1675

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9423798a39c7d5fb57c59898695562d5ed30345a2b93bd1bef19eaaa67d2d034
MD5 14f04438354b7550e50dabd3427d0d49
BLAKE2b-256 f988fde54f44ef6b30c0bb75be52f0f60ce2864c817e78294bf3ddfa637bb700

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ec3d9d5370b4ebeaa548056cf8ca107cf7c257871c3a1383133e0d07a9c45956
MD5 9058508402eaae3db116bacc096ee5dc
BLAKE2b-256 7af7124302a4678e8b7c3ea07a83b692d74ac244f7aed89dfe407bf956c57be3

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 22d89602736e9b79482024c22a45ef4324f0e80492fee6ff24b119f2218cf0f8
MD5 072d22aa6f2692b79cbedb0b7f3dd537
BLAKE2b-256 b7759029424262a8b7c13c216a598166bdaa1120224d71fb472f43d46871c25c

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 56a8d86f974ab6f60525b0fdded0cf99055d410b65be36a9ccad4babb0bc742f
MD5 cedeafa3a4f3c56d201d2f448bc24bb3
BLAKE2b-256 847a762610891377cd63daf5e470f2563513818073fe3d81d1019bbe01e128fc

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e52f0b82dbbe48babb9968b504496e8821b15b52be9033e89a9a0c2a6a26086f
MD5 c5cb8745f938a57fe85f44d4edb0d771
BLAKE2b-256 eae477f56f9f1a7af4d1ccdb8074d4d4a2979038a8be96d7a300a38f417210f1

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b5230d8e8881d6714f9d31a72f65599384c921bb275a7bcb086bc4d461f6681a
MD5 3b9918f1fb3ea47e8746d12de849455f
BLAKE2b-256 ed190563bf91d572327a0cccdff4fb912f276f4313c5f4fc14890506cb3ac6fd

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 82a5d5cd1b4ee894fc5db8545331e726c60fd2a9f2c2a831d4aab5b9d0e7aa87
MD5 6ef558c5617f2de6581f096068967d7d
BLAKE2b-256 c8c7efc3969ec23da67655f8beca8b04750596e0b315544e390ec1e1e1c3d6de

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: asyncpg_rkt-0.26.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 567.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for asyncpg_rkt-0.26.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 c3a106b415738bfee71ec8f2169c3a9fea335417eb600fb1c1ad68c9cd90c5f4
MD5 7ee86af9ea9acc5403bad108f39f92a1
BLAKE2b-256 1f86b681404820d1724f60876c92518336ae727d3a9e7d8c71f2f7dc22cdc41a

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f444733034fd65d7c46595767236e64c98707c0921d08226a2605b56cb49746d
MD5 b5c5b05700a6b420b1a36b5de49f9528
BLAKE2b-256 29ec2e19eec834fc782eed88e95c13e31295114d2b2e70701b1940b1e9ebab4c

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d18e762464e02f86a7be862ff681f0e63140a368944b73c4d71bbfd94f71ac7e
MD5 f4f3689ad6ed5e2ed95b536837cdfd28
BLAKE2b-256 693311be47c543c0530c3e3487461ea1b31fad55980535ef08795f33929d25eb

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 96b274e2f1b01825d66fc3aec9b3a465d87ff7f83b160f53067ca52d55887c98
MD5 5effa116a2e75eaa24b3bb88f161143d
BLAKE2b-256 2cb2d4f9596401639b378e3b385a95e7a892448f1924f1e5e219a6f9ce8ce366

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: asyncpg_rkt-0.26.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 568.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for asyncpg_rkt-0.26.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 cf1f1cd791ae9fe9ee6d77e7d4893fe25ae819190f85e3bd41a9ce2c0757394c
MD5 f1fb1cf7b5a85a0e1a58fff070687e86
BLAKE2b-256 c33d1a7f433aca22d765305092de6e190279d30c1612e36fffbea817554a6431

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 cc1008ff7f3e26e7769a2771459203e732562dd9deca2f653e5d7a5fbdad84b3
MD5 23c956c2cf316fca233c0c5b52b89916
BLAKE2b-256 0981e42251ebf0549c0b5c3e77e825e1e78211bfd163c6641eb720f40e0b5e18

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b4cfcf1d8a45fee34f0f649b87463de89bfbec244f2381f28d9fe0998089050f
MD5 6f3797e8ddaa90e6a145ae7362f304f8
BLAKE2b-256 1a1da815b62d496ac97f0c3eed968829ed6d4c33beefc4ae231a061652cc1291

See more details on using hashes here.

File details

Details for the file asyncpg_rkt-0.26.2-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for asyncpg_rkt-0.26.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e80819909cbcf9364eb4eef7556958faf0efc30cf624e9b0b8b1e9a931d90d19
MD5 fbda200d7cd775d698a7757c677ddfc3
BLAKE2b-256 0e4dc3e7b2ba68bf58fead1db526bdf6ea73d499e1e489a16f6fa84f922e1b99

See more details on using hashes here.

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