Detect COPY even if the query starts with whitespace or comments#1198
Conversation
|
I put my version here: 2e1e922 – personally I think it's rather easier to follow/understand. Performance is a few ns slower: But that's okay. What do you think? |
How would this fix that issue by the way? |
|
Thanks for taking care, @arp242. I'll check this tomorrow morning EU time. |
I updated the PR and will merge when ready. I want to look a bit at adding a higher-level test and some documentation on what this actually does. One reason for that is that your PR didn't actually do anything as it still retained the string indexing: So on |
|
You are right, good catch @arp242! Thank you for your time. |
Previously it was just strings.EqualFold(q[:4], "COPY"), and would fail to detect e.g. "/* foo */ copy [..]" or " copy". There is a small ~15ns performance hit for this, which seems acceptable. Co-authored-by: Dario Castañé <dario.castane@datadoghq.com>
|
@arp242 How often |
|
I'm hoping to do a release next week or thereabouts. |
What this PR does
Updated the handling of the
COPYstatement in the Prepare function to utilize theinternal.StartsWithCOPYutility to detect if a query is aCOPYstatement even if it has comments or whitespaces.Motivation
Our
Datadog/dd-trace-golibrary supports instrumentingdatabase/sql, including the DBM-APM link based onSQLcommenterspec, with the difference that we are prepending the comment instead of appending it. This breaks howlib/pqdetects aCOPYstatement.We are aware that there were previous opened issues around
COPYstatements, but we feel this one is an improvement, as SQL comments are not uncommon in the wild. Also, it's the only place where this kind of check happen inlib/pq.This would fix #1154, although it might break the workaround described in #530. We didn't attempt to address the concerns explained in #213.
Benchmarks
Although it's almost eight times slower at the nanoseconds scale, it should negligible in the context of preparing a query.