Postgrex.stream
You're seeing just the function
stream
, go back to Postgrex module for more information.
Specs
stream(DBConnection.t(), iodata() | Postgrex.Query.t(), list(), [option]) :: Postgrex.Stream.t() when option: execute_option() | {:max_rows, pos_integer()}
Returns a stream for a query on a connection.
Stream consumes memory in chunks of at most max_rows
rows (see Options).
This is useful for processing large datasets.
A stream must be wrapped in a transaction and may be used as an Enumerable
or a Collectable
.
When used as an Enumerable
with a COPY .. TO STDOUT
SQL query no other
queries or streams can be interspersed until the copy has finished. Otherwise
it is possible to intersperse enumerable streams and queries.
When used as a Collectable
the values are passed as copy data with the
query. No other queries or streams can be interspersed until the copy has
finished. If the query is not copying to the database the copy data will still
be sent but is silently discarded.
Options
:max_rows
- Maximum numbers of rows in a result (default to500
):decode_mapper
- Fun to map each row in the result to a term after decoding, (default:fn x -> x end
);:mode
- set to:savepoint
to use a savepoint to rollback to before an execute on error, otherwise set to:transaction
(default::transaction
);
Examples
Postgrex.transaction(pid, fn(conn) ->
query = Postgrex.prepare!(conn, "", "COPY posts TO STDOUT")
stream = Postgrex.stream(conn, query, [])
result_to_iodata = fn(%Postgrex.Result{rows: rows}) -> rows end
Enum.into(stream, File.stream!("posts"), result_to_iodata)
end)
Postgrex.transaction(pid, fn(conn) ->
stream = Postgrex.stream(conn, "COPY posts FROM STDIN", [])
Enum.into(File.stream!("posts"), stream)
end)