Comments (7)
True cancellation of queries is not implemented yet. In current implementation stream.destroy()
just reads and ignores records until end of result. The problem with implementation is that postgres protocol allows to cancel current query only. Its implossible to gracefully cancel all queued queries as far as I know, so cancellation algorithm for pgwire is not clear for me now. I will think about it now.
Cancellation of replication stream uses different protocol and it is implemented.
You queryStream
function is almost ok. You can avoid monkey patch and just subscribe to stream event by using builtin utility.
import { finished } from 'stream';
finished(stream, _ => conn.end());
This code will also handle successfull (non-destroy) case.
from pgwire.
upd
finished(stream, _ => conn.end());
will swallow error silenlty. Need to rethrow it somehow
from pgwire.
This is what I had to do actually:
finished(stream, err => {
conn.destroy();
if (err && err.code !== "ERR_STREAM_PREMATURE_CLOSE") {
throw err;
}
});
And this is still not perfect, because for logicalReplication stream I suspect it may eat the last ackImmediate().
What I try to achieve for logicalReplication stream is to: a) send it a signal "please terminate gracefully", b) wait until this signal is correctly processed (i.e. all acks are flushed), and c) then destroy the connection.
How do I do this? I suspect that there is no way currently?
from pgwire.
And this is still not perfect, because for logicalReplication stream I suspect it may eat the last ackImmediate().
replicationStream.destroy()
stops replication stream gracefully. Call of replicationStream.ackImmediate()
after replicationStream.destroy()
is prohibited and exception will be thrown #9 #18
and after destroy of replicationStream you can use regular conn.end()
as you wrote in the first comment.
from pgwire.
I just understood that pgwire.pool already has logicalReplication function that behaves exactly as you want.
logicalReplication(...args) {
const conn = new Connection({
...this._options,
replication: 'database',
});
return conn.logicalReplication(...args).finally(_ => {
conn.end();
});
}
I forgot that there is no need to use finished
at all. Just call conn.end()
after replicationStream is created and gracefull connection termination will be queued and executed automatically after replication stream destroy
from pgwire.
I'm trying to implement query cancellation for cases when there is only one pending query.
- if queueSize > 1 then return and procceed with current skip-based implementation
- block connection tx to prevent sending new queries while cancelling
- send CancelRequest and wait for cancellation socket terminated by backend
- unblock connection tx
from pgwire.
query cancellation is implemented
from pgwire.
Related Issues (20)
- TypeScript support HOT 6
- Feature request: Make ackImmediate() return a Promise HOT 4
- This library is the only NodeJS solution which reads pgoutput and just works
- Sending multiple single-queries through the same connection? HOT 2
- Feature request: Add Pool.newConnection() which always creates a new connection, similar to Pool.logicalReplication()
- Feature request: Non-async pgwire.connect() version
- Feature request: support for “eager filtering” while reading from replication a slot HOT 2
- Should .ack after .destroy throw error?
- pgoutput does not support message_cb, so pgwire can't work with pg_logical_emit_message() even theoretically HOT 1
- Error.stack message overwrite
- logical decoding using pgoutput, fetch the before data for all the updates. HOT 2
- unsafe int8 json HOT 1
- Serialize and Deserialize a BigInt HOT 2
- Add option to not use `JSON.parse` to decode `json` or `jsonb` HOT 3
- incorrect lastLsn HOT 6
- JSON parse stringify is not suitable for array codec
- pool.session
- ssl support HOT 3
- How to close replication? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pgwire.