Skip to content

Error if prepared statement name isn't unique #1813

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
Rich-Harris opened this issue Jan 14, 2019 · 4 comments
Closed

Error if prepared statement name isn't unique #1813

Rich-Harris opened this issue Jan 14, 2019 · 4 comments

Comments

@Rich-Harris
Copy link
Contributor

This is more an admission of incompetence on my part than anything, but on a number of occasions I've copy-pasted a query like this...

const { rows } = await pool.query({
  name: 'some-descriptive-name',
  text: `select thingamajig from flibbertigibbets where id = $1`,
  values: [id]
});

...and tweaked the query but forgotten to change the name:

const { rows } = await pool.query({
  name: 'some-descriptive-name',
  text: `select thingamajig from flibbertigibbets where id = $1 and active = true`,
  values: [id]
});

Typically, this results in subtle errors, by returning plausible but incorrect data.

I wonder if pg could help here by storing the text of a query that specifies a name, and erroring if it encounters another query with the same name but different text? Arguably not the library's job, but if my experience is anything to go by it could save a few developers from tearing their hair out, and deepen their gratitude towards this wonderful project.

Does that seem like a worthwhile addition? If so I'd be glad to try and rustle up a PR. Thank you!

@charmander
Copy link
Collaborator

Does that seem like a worthwhile addition?

I think it does!

@charmander
Copy link
Collaborator

(Note: null/undefined text should be allowed as well.)

brianc pushed a commit that referenced this issue Mar 6, 2019
…eries (#1814)

* compare prepared statement text with previous to prevent incorrect queries - fixes #1813

* make suggested changes to error message
@schrizsz
Copy link

Hi, we have a logging system in our microservices infrastructure that passes request unique id through services and also to postgresql queries. For that we add a /* cid: ${request_id} */ in the query. This "fix" breaks this possibility. Any advice?

@charmander
Copy link
Collaborator

@schrizsz That wouldn’t have worked before this fix either. This fix just makes it apparent. If you want to continue using named prepared statements but also delete them for new requests, you have to associate a client with a request, run DEALLOCATE <name>, and delete client.connection.parsedStatements[name].

Maybe setting the application name would work better if you were intending to go that far?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants