Postgres module

stream

procedure · 1 overloadsince 0.27.0Source

query for results too big to hold: the rows arrive batch at a time and for decodes them one by one, so at most one batch is in memory. The statement is sent here, and the connection is busy until the loop ends; leaving the loop early cancels the rest. A failure, before or during the loop, lands in error (cleared first). Strings in it live until the next batch: copy what you keep.

Example
error: Pg.Pg_Error;
for Pg.stream(c, *error, "select id, body from events where day = %", day, batch = 5000) {
    total += it.body.count;
}
if error.kind != .NONE ...

Not run: the example is marked ignore.

Not run: it needs a PostgreSQL server.

stream :: (
    c: *Connection,
    error: *Pg_Error,
    $sql: string,
    args: ..Any,
    batch := 1000,
    $call := #caller_code
) -> Row_Stream(#run row_type(sql, call, 2, #insert,scope(call) #run row_table_type(sql, call, 2, #insert,scope(call) #run row_table_exists(sql, call, 2)), Row(#run row_shape(sql, call, 2))))

query for results too big to hold: the rows arrive batch at a time and for decodes them one by one, so at most one batch is in memory. The statement is sent here, and the connection is busy until the loop ends; leaving the loop early cancels the rest. A failure, before or during the loop, lands in error (cleared first). Strings in it live until the next batch: copy what you keep.

Not run: it needs a PostgreSQL server.

Parameters

c: *Connection
the connection to run on; it is busy until the loop ends.
error: *Pg_Error
set to the failure, if any, once the loop ends.
$sql: string
the statement, as for query.
args: ..Any
one value per %, in order.
batch := 1000
how many rows arrive at a time.
$call := #caller_code
the code of the call, which lets the query text be checked while compiling. Leave the default.

Returns

Row_Stream(#run row_type(sql, call, 2, #insert,scope(call) #run row_table_type(sql, call, 2, #insert,scope(call) #run row_table_exists(sql, call, 2)), Row(#run row_shape(sql, call, 2))))
Example
error: Pg.Pg_Error;
for Pg.stream(c, *error, "select id, body from events where day = %", day, batch = 5000) {
    total += it.body.count;
}
if error.kind != .NONE ...

Not run: the example is marked ignore.

See also