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

CopyTo parameter support #2247

Open
Drahflow opened this issue Jan 31, 2025 · 2 comments
Open

CopyTo parameter support #2247

Drahflow opened this issue Jan 31, 2025 · 2 comments

Comments

@Drahflow
Copy link

Drahflow commented Jan 31, 2025

Is your feature request related to a problem? Please describe.

CopyTo does not take parameters, only a string.

Describe the solution you'd like

CopyTo should be taking arguments ...any just like Exec does.

Describe alternatives you've considered

Exporting a way to correctly quote parameters into an SQL string. (But it is not a good alternative.)

Additional context

This seems to be a backward compatible change. Also it is parallel to how other APIs (e.g. Exec) of this package work.

Documentation of COPY with libpq: https://www.postgresql.org/docs/current/libpq-copy.html

The overall process is that the application first issues the SQL COPY command via PQexec or one of the equivalent functions. The response to this (if there is no error in the command) will be a PGresult object bearing a status code of PGRES_COPY_OUT or PGRES_COPY_IN (depending on the specified copy direction).

This suggests PQexecParams should also be usable (and thus parameters can be passed on the protocol level).

@jackc
Copy link
Owner

jackc commented Feb 2, 2025

I don't think this is possible. pgx handles parameters via the extended protocol / prepared statement interface (and when using the simple protocol it simulates that as closely as possible). But the COPY TO SQL string doesn't actually take any parameters as far as PostgreSQL is concerned.

@Drahflow
Copy link
Author

Drahflow commented Feb 7, 2025

Indeed. I just tried a raw pgConn.Prepare on COPY (SELECT 1, $1, 3) TO stdout and got:

SD: &{Name: SQL:COPY (SELECT 1, $1, 3) TO stdout ParamOIDs:[] Fields:[]} Err: <nil>

... indicating the server did prepare the statement, but did not consider anything in there to be a parameter. 😞

So sanitizeForSimpleQuery it is then. I can do

	res, err := conn.PgConn().CopyTo(ctx, os.Stdout, "COPY (SELECT 1, $1, 3) TO stdout", "'\"';")

with Drahflow/jackc-pgx@master...Drahflow:jackc-pgx:2025-02-07-issue-2247
(not yet a real PR, obviously) and get correctly quoted results.

Do you think you'd like a PR along those lines? If yes, any opinions on how to get a "good" pgtype.Map into the pgconn?

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

No branches or pull requests

2 participants