Skip to content

fix: modify COPY prepare handling to use internal utility #1198

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

darccio
Copy link

@darccio darccio commented Aug 12, 2025

What this PR does

Updated the handling of the COPY statement in the Prepare function to utilize the internal.StartsWithCOPY utility to detect if a query is a COPY statement even if it has comments or whitespaces.

Motivation

Our Datadog/dd-trace-go library supports instrumenting database/sql, including the DBM-APM link based on SQLcommenter spec, with the difference that we are prepending the comment instead of appending it. This breaks how lib/pq detects a COPY statement.

We are aware that there were previous opened issues around COPY statements, 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 in lib/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

goos: darwin
goarch: arm64
pkg: github.com/lib/pq/internal
cpu: Apple M1 Max
BenchmarkStartsWithCOPY-10    	39674830	        28.50 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/lib/pq/internal	1.363s

goos: darwin
goarch: arm64
pkg: github.com/lib/pq/internal
cpu: Apple M1 Max
BenchmarkEqualFold-10    	292420224	         3.771 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/lib/pq/internal	1.721s

Although it's almost eight times slower at the nanoseconds scale, it should negligible in the context of preparing a query.

Updated the handling of the COPY statement in the Prepare function to utilize the internal.StartsWithCOPY utility to detect if a query is a COPY statement even if it has comments or whitespaces.
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.

Exectuing pq.CopyIn fails with one of two errors depending on usage
1 participant