Home

LibPQ

Stable Latest Build Status CodeCov

Examples

Selection

using LibPQ, DataStreams, NamedTuples

conn = LibPQ.Connection("dbname=postgres")
result = execute(conn, "SELECT typname FROM pg_type WHERE oid = 16")
data = Data.stream!(result, NamedTuple)

# the same but with parameters
result = execute(conn, "SELECT typname FROM pg_type WHERE oid = \$1", ["16"])
data = Data.stream!(result, NamedTuple)

# the same but using `fetch!` to handle streaming and clearing
data = fetch!(NamedTuple, execute(conn, "SELECT typname FROM pg_type WHERE oid = \$1", ["16"]))

close(conn)

Insertion

using LibPQ, DataStreams

conn = LibPQ.Connection("dbname=postgres user=$DATABASE_USER")

result = execute(conn, """
    CREATE TEMPORARY TABLE libpqjl_test (
        no_nulls    varchar(10) PRIMARY KEY,
        yes_nulls   varchar(10)
    );
""")

Data.stream!(
    data,
    LibPQ.Statement,
    conn,
    "INSERT INTO libpqjl_test (no_nulls, yes_nulls) VALUES (\$1, \$2);",
)

close(conn)

A Note on Bulk Insertion

When inserting a large number of rows, wrapping your insert queries in a transaction will greatly increase performance. See the PostgreSQL documentation 14.4.1. Disable Autocommit for more information.

Concretely, this means surrounding your query like this:

execute(conn, "BEGIN;")

Data.stream!(
    data,
    LibPQ.Statement,
    conn,
    "INSERT INTO libpqjl_test (no_nulls, yes_nulls) VALUES (\$1, \$2);",
)

execute(conn, "COMMIT;")