Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli/sql: avoid session reconnect upon query cancellation #76483

Closed
knz opened this issue Feb 13, 2022 · 13 comments · Fixed by #79739
Closed

cli/sql: avoid session reconnect upon query cancellation #76483

knz opened this issue Feb 13, 2022 · 13 comments · Fixed by #79739
Labels
A-cli-client CLI commands that pertain to using SQL features C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Comments

@knz
Copy link
Contributor

knz commented Feb 13, 2022

Describe the problem

This is a followup to #76437, especially the problem noticed during testing: a bug in lib/pq forces the connection to be dropped.
Discussed with @rafiss we have two solutions

  • improve lib/pq to fix the bug
  • use pgx instead.

To Reproduce

run a long query and interrupt it interactively.

Expected behavior

The session remains open, including all its session variable customizations.

Jira issue: CRDB-13144

@knz knz added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-cli-client CLI commands that pertain to using SQL features labels Feb 13, 2022
@knz
Copy link
Contributor Author

knz commented Feb 13, 2022

@rafiss I did some research into using pgx instead of lib/pq.

The APIs are certainly nicer and cleaner. Unfortunately, pgx does not yet support GSSAPI authentication, which some of our customers have become dependent on.

So we have several possible choices forward:

  • add kerberos support to pgx. The implementation in lib/pq is very small, so maybe it'll be small in pgx too.
  • fix lib/pq to remove the offending bug. In that upstream PR, mjibson suggested he's open to reverting the change if there's a unit test to demonstrate the problem and prevent a regression.
  • as you suggested earlier, expose the query cancel method as a public API in lib/pq, then change our CLI Code to use that. It woulnd't be as elegant/compact as context-based cancellation, but 🤷

WDYT?

@rafiss
Copy link
Collaborator

rafiss commented Mar 16, 2022

I think it would be great if pgx had kerberos support; that's my first choice. tracking issue for it: jackc/pgx#1166

@knz
Copy link
Contributor Author

knz commented Mar 21, 2022

Thanks sounds good. Are we providing incentives for that to happen?

@rafiss
Copy link
Collaborator

rafiss commented Mar 23, 2022

Thanks sounds good. Are we providing incentives for that to happen?

We're discussing that with the DX team.

@erikgrinaker
Copy link
Contributor

I think we should do something about this before we ship 22.1, the current Ctrl-C behavior is not a good look:

^C
attempting to cancel query...
ERROR: query execution canceled
SQLSTATE: 57014
warning: error retrieving the transaction status: driver: bad connection
warning: connection lost!
opening new connection: all session settings will be lost
root@:26257/defaultdb ?>

@knz
Copy link
Contributor Author

knz commented Apr 7, 2022

IMHO it's objectively better than the previous look, where the SQL shell would simply exit. What do you think?

@erikgrinaker
Copy link
Contributor

IMHO it's objectively better than the previous look, where the SQL shell would simply exit. What do you think?

For sure, but this makes it look like something is broken or wrong. Maybe we at least can make the error message a bit more friendly or explanatory?

@knz
Copy link
Contributor Author

knz commented Apr 7, 2022

What would be a good example output?

@erikgrinaker
Copy link
Contributor

Dropping the two warning messages would go a long way, I think.

@knz
Copy link
Contributor Author

knz commented Apr 9, 2022

NB: in #79629, @otan is modifying lib/pq to support the copy protocol in a way the sql shell uses it. However, in this issue @rafiss is recommending we switch over from lib/pq to pgx to benefit from better support for query cancellation (pending support for k5s authn in pgx).

How can we reconcile the two efforts? If we go towards pgx, then @otan's efforts in lib/pq will not help; how can we ensure \copy will continue to work when we switch over to pgx?

@otan
Copy link
Contributor

otan commented Apr 9, 2022

i'd like COPY to be (if desirable) merged into 22.1, so at least we make progress there.

we'd want to expose pgconn for continued support for COPY; i think this is still aligned here but may slightly make the direction a little difficult (though i'm sure pgx/v4 uses pgconn and so we can perhaps look at exposing that to render access to COPY).

knz added a commit to knz/cockroach that referenced this issue Apr 10, 2022
As discussed in issue cockroachdb#76483, there's a bug in lib/pq
which causes the session to be aborted on query cancellation.

The resulting UX is just too poor; if the session terminates, there's
no real benefit in keeping the shell alive. The user may as well stop
the shell and then restart it, which makes the situation clearer.

This commit thus disables cancellation support in the client
temporarily until we fix cockroachdb#76483.

Release note (cli change): The mechanism for query cancellation
is disabled in the `sql` shell until a later patch release.
knz added a commit to knz/cockroach that referenced this issue Apr 10, 2022
As discussed in issue cockroachdb#76483, there's a bug in lib/pq
which causes the session to be aborted on query cancellation.

The resulting UX is just too poor; if the session terminates, there's
no real benefit in keeping the shell alive. The user may as well stop
the shell and then restart it, which makes the situation clearer.

This commit thus disables cancellation support in the client
temporarily until we fix cockroachdb#76483.

Release note (cli change): The mechanism for query cancellation
is disabled in the `sql` shell until a later patch release.
@knz
Copy link
Contributor Author

knz commented Apr 10, 2022

i'd like COPY to be (if desirable) merged into 22.1, so at least we make progress there.

no objection. My question above is to determine what we'll do post-release.

we'd want to expose pgconn for continued support for COPY; i think this is still aligned here but may slightly make the direction a little difficult (though i'm sure pgx/v4 uses pgconn and so we can perhaps look at exposing that to render access to COPY).

that's indeed what i had in mind too, I just hope we can bring that point across with the pgx maintainers.

@knz
Copy link
Contributor Author

knz commented Apr 10, 2022

Dropping the two warning messages would go a long way, I think.

I've tried doing that, but the resulting UX is still not good; a session reconnect without warning is confusing, because the user loses their customization.

I think it'll be just better to remove the cancellation altogether until we fix the pq bug (or move to pgx). Doing this in #79739.

@craig craig bot closed this as completed in 4275d52 Apr 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli-client CLI commands that pertain to using SQL features C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants