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
Postgres.js supports [`copy ...`](https://www.postgresql.org/docs/14/sql-copy.html) queries, which are exposed as [Node.js streams](https://nodejs.org/api/stream.html).
432
+
433
+
> **NOTE** This is a low-level API which does not provide any type safety. To make this work, you must match your [`copy query` parameters](https://www.postgresql.org/docs/14/sql-copy.html) correctly to your [Node.js stream read or write](https://nodejs.org/api/stream.html) code. Ensure [Node.js stream backpressure](https://nodejs.org/en/docs/guides/backpressuring-in-streams/) is handled correctly to avoid memory exhaustion.
434
+
435
+
#### ```awaitsql`copy ... from stdin`-> Writable```
436
+
437
+
```js
438
+
const { pipeline } =require('stream/promises')
439
+
440
+
// Stream of users with the default tab delimitated cells and new-line delimitated rows
441
+
constuserStream=Readable.from([
442
+
'Murray\t68\n',
443
+
'Walter\t80\n'
444
+
])
445
+
446
+
constquery=awaitsql`copy users (name, age) from stdin`.writable()
447
+
awaitpipeline(userStream, query);
448
+
```
449
+
450
+
#### ```awaitsql`copy ... to stdin`-> Readable```
451
+
452
+
##### stream pipeline
453
+
```js
454
+
const { pipeline } =require('stream/promises')
455
+
const { createWriteStream } =require('fs')
456
+
457
+
constreadableStream=awaitsql`copy users (name, age) to stdin`.readable()
Postgres.js supports, [canceling queries in progress](https://www.postgresql.org/docs/7.1/protocol-protocol.html#AEN39000). It works by opening a new connection with a protocol level startup message to cancel the current query running on a specific connection. That means there is no guarantee that the query will be canceled, and due to the possible race conditions it might even result in canceling another query. This is fine for long running queries, but in the case of high load and fast queries it might be better to simply ignore results instead of canceling.
0 commit comments