Git Product home page Git Product logo

Comments (7)

exe-dealer avatar exe-dealer commented on June 15, 2024

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.

exe-dealer avatar exe-dealer commented on June 15, 2024

upd
finished(stream, _ => conn.end()); will swallow error silenlty. Need to rethrow it somehow

from pgwire.

dko-slapdash avatar dko-slapdash commented on June 15, 2024

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.

exe-dealer avatar exe-dealer commented on June 15, 2024

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.

exe-dealer avatar exe-dealer commented on June 15, 2024

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.

exe-dealer avatar exe-dealer commented on June 15, 2024

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.

exe-dealer avatar exe-dealer commented on June 15, 2024

query cancellation is implemented

from pgwire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.