You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add support for COPY ... FROM STDIN and COPY ... TO STDOUT statements in PostgreSQL #36
Marking this as a good contribution for someone looking to contribute to the internals.
The following is my initial thoughts on what a good API would look like.
implPgCopyOut{// Returns a cursor over the rows of the data streamfninto_cursor(self) -> PgCursor;// Returns a stream of bytesfninto_stream(self) -> implStream<Item = u8>;fninto_async_read(self) -> implAsyncRead;}implPgCopyIn{// Expect a stream of bytesfninto_sink(self) -> implSink<Item = u8>;fninto_async_write(self) -> implAsyncWrite;}pubtraitPgExecutorExt:Executor{// Expected COPY TO ... STDOUTfncopy_out<'q,'e,E>(&'emutself,statement:&'qstr,) -> PgCopyOut<'q,'e>;// Expected COPY FROM ... STDINfn copy_in<'q,'e>&'e mutself,
statement:&'q str,) -> PgCopyIn<'q,'e>;}// Copy from one database to anotherlet rx = con1.copy_out("COPY articles TO STDOUT");let tx = con2.copy_in("COPY articles FROM STDIN");
rx.into_stream().forward(tx.into_sink()).await?;
@mehcode one hiccup with into_cursor() is that we're never given the type OIDs of columns in the data stream, so we have no way to give informed errors in Row::try_get().
We can have a similar API but I'm thinking specialized types that clearly document that they're semi-unchecked. PgCopyCursor and PgCopyRow or something.
Maybe on top of that we can have a proc-macro API that looks at the table schema and makes it "safe" or something.
Additionally, the main API probably shouldn't take raw statements but instead either a table name and list of columns or the data source query.
fncopy_out(&mutself, table;&str,columns:implIntoIterator<Item = &str>) -> PgCopyOut/// Accepts just the SELECT/INSERT/UPDATE/DELETE RETURNING portionfncopy_out_with(&mutself,query:&str) -> PgCopyOutfncopy_in(&mutself,table:&str,columns:implIntoIterator<Item = &str>) -> PgCopyIn
I'd be okay with _raw() variants that take the full statement (e.g. to force text format).
into_stream returning a Stream<Item = u8> seems kinda inefficient/pointless. Maybe Box<[u8]> instead, which also lets it work with futures::io even on Tokio. into_sink(self) has dubious utility I think, unless it's generic over T: AsRef<[u8]>.
I'm assuming the types that into_async_read(self) and into_async_write() return depend on the current runtime.
SQLx is not a query builder (yet). I'm really hesitant to provide an API on Executor that is anything more than "raw SQL here".
It's simple to die immediately if they don't send the right command as postgres responds in a specific way and tells us the details about their copy command.
I mainly suggest not just taking the full statement because by default COPY uses the text format which isn't going to be very fast since we'll have to decode each row as UTF-8 first before actually decoding it. The user would have to remember every time to specify binary format if they want full performance.
Maybe we should do a builder here then. There's not really that much to configure if you're using binary format, it's just whether or not you supply a column list or subquery.
I think the Executor trait should still be the low-level operational trait that takes the raw query. I'm not against adding a DSL on top of it to make things simpler.
Just that we should do this task in phases.
Raw COPY ... STDIN and COPY ... STDOUT statements where the "deliverable" is two distinct use cases.
Emit a CSV from a table or query
Push a table or query into another table ( on another connection )
Cursor support to iterate rows coming from a COPY ... STDOUT statement.
Some way to push rows to a COPY ... STDIN statement. I believe we talked about a IntoArguments trait.
A DSL around the above to make things simpler and less error-prone to setup.
For the first step, should we still do copy_in_raw() and copy_out_raw() so we have some space to play with naming for the higher-level API? Maybe it could directly return impl AsyncRead or impl AsyncWrite.
Activity
abonander commentedon Jan 7, 2020
This could be implemented as an inherent method on
PgConnection.mehcode commentedon Mar 25, 2020
Marking this as a good contribution for someone looking to contribute to the internals.
The following is my initial thoughts on what a good API would look like.
abonander commentedon Mar 27, 2020
@mehcode one hiccup with
into_cursor()is that we're never given the type OIDs of columns in the data stream, so we have no way to give informed errors inRow::try_get().mehcode commentedon Mar 27, 2020
Good point. We just get a stream of
DataRowmessages essentially.abonander commentedon Mar 30, 2020
We can have a similar API but I'm thinking specialized types that clearly document that they're semi-unchecked.
PgCopyCursorandPgCopyRowor something.Maybe on top of that we can have a proc-macro API that looks at the table schema and makes it "safe" or something.
abonander commentedon Mar 30, 2020
Additionally, the main API probably shouldn't take raw statements but instead either a table name and list of columns or the data source query.
I'd be okay with
_raw()variants that take the full statement (e.g. to forcetextformat).into_streamreturning aStream<Item = u8>seems kinda inefficient/pointless. MaybeBox<[u8]>instead, which also lets it work withfutures::ioeven on Tokio.into_sink(self)has dubious utility I think, unless it's generic overT: AsRef<[u8]>.I'm assuming the types that
into_async_read(self)andinto_async_write()return depend on the current runtime.mehcode commentedon Mar 30, 2020
SQLx is not a query builder (yet). I'm really hesitant to provide an API on
Executorthat is anything more than "raw SQL here".It's simple to die immediately if they don't send the right command as postgres responds in a specific way and tells us the details about their copy command.
mehcode commentedon Mar 30, 2020
into_streamandinto_sinkare kind of pointless, yeah.AsyncRead/AsyncWriteare more than fine probably.abonander commentedon Mar 30, 2020
I mainly suggest not just taking the full statement because by default
COPYuses the text format which isn't going to be very fast since we'll have to decode each row as UTF-8 first before actually decoding it. The user would have to remember every time to specifybinaryformat if they want full performance.mehcode commentedon Mar 30, 2020
I don't disagree. My point is that something like query construction is better suited to a DSL if/when we get it:
Macro version perhaps:
abonander commentedon Mar 30, 2020
Maybe we should do a builder here then. There's not really that much to configure if you're using binary format, it's just whether or not you supply a column list or subquery.
mehcode commentedon Mar 30, 2020
I think the Executor trait should still be the low-level operational trait that takes the raw query. I'm not against adding a DSL on top of it to make things simpler.
Just that we should do this task in phases.
Raw
COPY ... STDINandCOPY ... STDOUTstatements where the "deliverable" is two distinct use cases.Cursor support to iterate rows coming from a
COPY ... STDOUTstatement.Some way to push rows to a
COPY ... STDINstatement. I believe we talked about aIntoArgumentstrait.A DSL around the above to make things simpler and less error-prone to setup.
abonander commentedon Mar 30, 2020
Yeah, I can get behind that.
abonander commentedon Mar 30, 2020
For the first step, should we still do
copy_in_raw()andcopy_out_raw()so we have some space to play with naming for the higher-level API? Maybe it could directly returnimpl AsyncReadorimpl AsyncWrite.shiftrtech commentedon Oct 23, 2020
Any news about this?, is there something i can do meanwhile to make a massive database ingest, thanks