Skip to main content

Stream results of multi statement PostgreSQL queries from Python without a server-side cursor

Project description

streampq CircleCI Test Coverage

Stream results of multi-statement PostgreSQL queries from Python without server-side cursors. Has these benefits over some other Python PostgreSQL drivers:

  • Streams results from complex multi-statement queries even though SQL doesn't allow server-side cursors for such queries - suitable for large amounts of results that don't fit in memory.

  • CTRL+C (SIGINT) by default behaves as expected even during slow queries - a KeyboardInterrupt is raised and quickly bubbles up through streampq code. Unless client code prevents it, the program will exit.

  • Every effort is made to cancel queries on KeyboardInterrupt, SystemExit, or errors - the server doesn't continue needlessly using resources.

Installation

pip install streampq

The libpq binary library is also required. This is typically either already installed, or installed by:

  • macOS + brew: brew install libpq
  • Linux (Debian): apt install libpq5
  • Linux (Red Hat):yum install postgresql-libs

Usage

from streampq import streampq_connect

# libpq connection paramters
# https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS
#
# Any can be ommitted and environment variables will be used instead
# https://www.postgresql.org/docs/current/libpq-envars.html
connection_params = (
    ('host', 'localhost'),
    ('port', '5432'),
    ('dbname', 'postgres'),
    ('user', 'postgres'),
    ('password', 'password'),
)

# SQL statement(s) - if more than one, separate by ;
sql = '''
SELECT * FROM my_table WHERE my_col = {first};
SELECT * FROM my_other_table WHERE my_other_col = {second};
'''

with streampq_connect(connection_params) as query:
    for (columns, rows) in query(sql, literals=(
        ('first', 'a value'),
        ('second', 'another value'),
    )):
        for row in rows:
            pass

Exceptions

Exceptions derive from streampq.StreamPQError. If there is any more information available on the error, it's added as a string in its args property. This is included in the string representation of the exception by default.

Exception hierarchy

  • StreamPQError

    Base class for all explicitly-thrown exceptions

    • ConnectionError

      An error occurred while attempting to connect to the database.

    • QueryError

      An error occurred while attempting to run a query. Typically this is due to a syntax error or a missing column.

    • CancelError

      An error occurred while attempting to cancel a query.

    • CommunicationError

      An error occurred communicating with the database after successful connection.

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

streampq-0.0.9.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

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

streampq-0.0.9-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file streampq-0.0.9.tar.gz.

File metadata

  • Download URL: streampq-0.0.9.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.11

File hashes

Hashes for streampq-0.0.9.tar.gz
Algorithm Hash digest
SHA256 a9e1ed1c6edd7da7fc02370d4fdb8f416f1b5fb046f730cd78131ec058bf8115
MD5 cc9ee4d610421b4144be48e0df118333
BLAKE2b-256 97012a49ec854368c82bafa8752a07a4a45554b53eee212c6b7ed4d0d969a625

See more details on using hashes here.

File details

Details for the file streampq-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: streampq-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.11

File hashes

Hashes for streampq-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 fcd10b74ba10d837f51fd64be579ea26b485191f027b02edec2a5c08182a4d7c
MD5 2e46662a099dd38bdbd4aadc541fe95b
BLAKE2b-256 09a0d8844a711784d6a2a2a2cf856f99dba84ad72dd1f89618e5988d9a3736fc

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