Skip to content

Allow prepared statement names that are longer than 63 characters #772

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

Closed
wants to merge 2 commits into from

Conversation

bdowning
Copy link

@bdowning bdowning commented May 4, 2015

Postgres truncates prepared statement names at 63 characters. If you have two that are the same in the first 63 characters you'll get a "prepared statement already exists" error. This is very noticeable if you try to use the SQL query itself as the name.

This fixes this by creating and caching unique short names for all long names, and sending the short one to Postgres.

bdowning added 2 commits May 4, 2015 04:17
Since the name is truncated by Postgres, the second query will result in a
"prepared statement already exists" error.
This allows for prepared statement names of any length.  In particular
it lets one safely use the SQL query itself as the name.
@brianc
Copy link
Owner

brianc commented May 21, 2015

First of all, sorry for taking a while to get to this...I really appreciate the thought & work you put into this, but I think this might be better left out of node-postgres itself. I think it introduces some cleverness in the library I could see being a footgun if someone wasn't expecting it to work this way, and if postgres introduces a longer name cap in the future we'd have to come back and figure out a way to support both the old constraint and new constraint. I think something that would be helpful though is spitting a warning out to the console if there is a query name of greater than 63 characters. Because we don't automatically attach a name & use a named query anyone who's using this feature is doing so in their code explicitly. I think it'd be pretty easy for them to do an md5 on the query text or something else - like what you've done - to give their queries unique names. It might even be helpful to make a node module that generates unique query names for you and then it could be shared for anyone who's using that. What do you think?

@bdowning
Copy link
Author

I'd be fine with doing it on my own for my use-case, I have an abstraction layer to run my own queries anyway. Really what I was trying to fix was the WTF-factor of having two different queries with different names silently resolve to the same thing. A warning would suffice to fix that.

@brianc
Copy link
Owner

brianc commented May 21, 2015

Yeah I definitely feel you that the WTF-ness on that is no bueno at all. I'll fix that right up.

brianc added a commit that referenced this pull request May 21, 2015
Postgres has a 63 character limit on query names.  To avoid potential footgunning of users we'll log to their stderr if they use a longer query name.

For reference: #772
brianc added a commit that referenced this pull request May 21, 2015
Postgres has a 63 character limit on query names.  To avoid potential footgunning of users we'll log to their stderr if they use a longer query name.

For reference: #772
@brianc
Copy link
Owner

brianc commented May 21, 2015

How does this look?

#787

@bdowning
Copy link
Author

Closing in favor of #787 which is a lot less invasive. Thanks.

@bdowning bdowning closed this May 21, 2015
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