pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: sfackler@gmail.com
Subject: BUG #16149: Prepared COPY queries always report 0 parameters when described
Date: Thu, 05 Dec 2019 00:21:36 +0000
Message-ID: <16149-531659f1ce965744@postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      16149
Logged by:          Steven Fackler
Email address:      sfackler@gmail.com
PostgreSQL version: 12.1
Operating system:   Debian Buster
Description:        

When a Postgres backend describes a prepared `COPY ... TO STDOUT query`, it
always reports 0 query parameters regardless of how many are actually
present. Here's a simple example program that demonstrates the issue:

```
#include <libpq-fe.h>
#include <assert.h>
#include <stdio.h>

int main() {
	PGconn *conn = PQconnectdb("host=localhost port=5433 user=postgres");
	assert(PQstatus(conn) == CONNECTION_OK);

	PGresult *result = PQprepare(conn, "a", "COPY (SELECT $1::TEXT) TO STDOUT",
0, NULL);
	assert(PQresultStatus(result) == PGRES_COMMAND_OK);

	result = PQdescribePrepared(conn, "a");
	assert(PQresultStatus(result) == PGRES_COMMAND_OK);
	printf("nparams: %d\n", PQnparams(result));

	result = PQexecPrepared(conn, "a", 0, NULL, NULL, NULL, 0);
	assert(PQresultStatus(result) == PGRES_FATAL_ERROR);
	printf("error: %s\n", PQresultErrorField(result,
PG_DIAG_MESSAGE_PRIMARY));
```

When run, it prints the following:

```
nparams: 0
error: there is no parameter $1
```

If you change the query to just the inner `SELECT $1::TEXT`, the number of
parameters is correctly reported, but interestingly the error message
changes:

```
nparams: 1
error: bind message supplies 0 parameters, but prepared statement "a"
requires 1
```

From some quick Googling, I did see this StackOverflow post[1] stating that
COPY queries don't support parameters, but if that's the case it seems like
an error should be reported at the preparation stage. I also don't see
anything about that in the documentation for COPY[2], though I may have
missed it! I see the same behavior on Postgres 11.1 as well, if that's
relevant.

[1]: https://stackoverflow.com/a/22963085
[2]: https://www.postgresql.org/docs/12/sql-copy.html



view thread (2+ messages)  latest in thread

Message-ID: <16149-531659f1ce965744@postgresql.org>
Permalink:  ../16149-531659f1ce965744@postgresql.org/
Also on:    postgresql.org/message-id/16149-531659f1ce965744@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, sfackler@gmail.com
  Subject: Re: BUG #16149: Prepared COPY queries always report 0 parameters when described
  In-Reply-To: <16149-531659f1ce965744@postgresql.org>
  References: <16149-531659f1ce965744@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox