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

Clean connection state up after protocol named prepared statement #163

Merged

Conversation

drdrsh
Copy link
Collaborator

@drdrsh drdrsh commented Sep 8, 2022

We only send DISCARD ALL if we created a named prepared statement or issued a SET xxx query. This PR catches an edge case when we create named prepared statements using P message.

let _ = final_buffer.get_i32() as usize;

// Almost certainly true
if first_message_code == 'P' {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not run this check in 'P' => { block? Should be cheaper than cloning the entire buffer (which can be very large) every S (effectively every query).

Copy link
Contributor

@levkk levkk Sep 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, aren't all Postgres strings NULL-terminated, so this check would never fire?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on the clone, the reason I run this in the S and not in the P is that we have two places where we handle P buffering so I am trying to avoid duplication. Let me see if we can get away with no clones.

re: NULL-terminated, we only care about the first byte after P and the 32bit size. If it is \0 the this is an unnamed prepared statement, anything else would be interpreted as a named prepared statement.

@drdrsh
Copy link
Collaborator Author

drdrsh commented Sep 8, 2022

I made a change to access the buffer without cloning. Using get does not seem to advance the internal pointer which should be ideal.
I added a test that uses PG#prepare which creates a named prepared statement using P message.

@levkk
Copy link
Contributor

levkk commented Sep 8, 2022

Pretty cool!

@levkk levkk merged commit 9514b3b into postgresml:main Sep 8, 2022
jmeagher pushed a commit to jmeagher/pgcat that referenced this pull request Feb 17, 2023
Avoid reporting ProtocolSyncError when admin session disconnects (postgresml#160)
Better logging for failure to get connection from pool (postgresml#161)
Send signal even if process is gone (postgresml#162)
Clean connection state up after protocol named prepared statement (postgresml#163)
Add Discord link (postgresml#164)
Add SHOW CLIENTS / SHOW SERVERS + Stats refactor and tests (postgresml#159)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants