postgres icon indicating copy to clipboard operation
postgres copied to clipboard

Cannot get count of COPY query

Open ChristophP opened this issue 2 years ago • 4 comments

Hi @porsager thanks for making this library.

We are currently trying to migrate our data base code to this library and don't know how to get the count of a COPY query.

Our code looks like this:

const query = sql`COPY transaction (name) FROM STDIN`;
console.log(query.writeable); // undefined
console.log(await query); // Writeable

Normally one would use the .count property as described here https://github.com/porsager/postgres#count in the docs, but the problem is that in the example above await query is not a Result object but a Writeable (and thus has no .count property).

From the docs it seemed to me that one would have to call .writeable() on the query in order to get the stream but that's not what the console.log shows. I'm I missunderstanding something or should this behave differently?

ChristophP avatar Aug 16 '22 14:08 ChristophP

Never mind. Layer 8 problem, typing .writable() instead of .writeable() helps a lot. :-(

ChristophP avatar Aug 16 '22 14:08 ChristophP

But I still have a remaining question:

If this is the way to copy rows into a table using streams

const query = await sql`copy users (name, age) from stdin`.writable()
await pipeline(userStream, query);

Is there a way to get the Result.count somehow to find out how many rows got inserted? Since all I get is the Writable and not a Result

Additional info: I seem to get the Writable whether I actually call .writable() or not.

await sql`copy users (name, age) from stdin`;           // Writable
await sql`copy users (name, age) from stdin`.writable() // Writeable

ChristophP avatar Aug 16 '22 15:08 ChristophP

I too am looking for this feature!

Is there a way to glean count information from the COPY command / stream?

jgnieuwhof avatar Feb 14 '23 18:02 jgnieuwhof

Yeah, it's available, but not until CommandComplete is sent by the db, which happens after the stream is finished.

Thinking of that now appears to be the wrong behaviour. I think for v4 it'd be better to push error handling to the stream, both initial query, and also wait for CommandComplete. That way we could include the count too.

porsager avatar Jun 25 '23 23:06 porsager