BUG #5093: Prepared query gives different PGresult than exec'd equivalent

From: "Mike Pomraning" <mjp(at)pilcrow(dot)madison(dot)wi(dot)us>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #5093: Prepared query gives different PGresult than exec'd equivalent
Date: 2009-10-02 05:05:34
Message-ID: 200910020505.n9255YxV026346@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 5093
Logged by: Mike Pomraning
Email address: mjp(at)pilcrow(dot)madison(dot)wi(dot)us
PostgreSQL version: 8.4.1
Operating system: Linux i686 2.6.18-128.7.1.el5
Description: Prepared query gives different PGresult than exec'd
equivalent
Details:

The following short program installs a RULE to SELECT two rows INSTEAD of
INSERTing into a VIEW.

When it PQexec's the insertion, I get a PGresult object with PQntuples == 2.
However, when it PREPAREs/EXECUTEs the very same SQL, the PGresult has
PQntuples == 0.

My expectation is that the prepared statement would return the same PGresult
as its unprepared equivalent.

Am I using the API incorrectly, is my expectation amiss, is this a bug,
etc.?

/*
$ ./a.out 'dbname=postgres password="mypass"'
PGresult of exec() ('INSERT 0 0'): ntuples 2
PGresult of execPrepd() ('INSERT 0 0'): ntuples 0
*/
#include <stdio.h>
#include <libpq-fe.h>

static char sql[] = "INSERT INTO v VALUES (1)";

int
main(int argc, char **argv) {
PGconn *pg;
PGresult *r;

pg = PQconnectdb(argv[1] ? argv[1] : "");
if (!pg || PQstatus(pg) == CONNECTION_BAD) {
printf("connection failed\n");
return 1;
}

PQexec(pg, "CREATE TEMPORARY VIEW v AS SELECT 1 WHERE 1=0");
PQexec(pg, "CREATE RULE r AS ON INSERT TO v DO INSTEAD (SELECT 1 UNION
SELECT 2)");

r = PQexec(pg, sql);
printf("PGresult of exec() ('%s'): ntuples %d\n", PQcmdStatus(r),
PQntuples(r));

PQprepare(pg, "pstmt", sql, 0, NULL);

r = PQexecPrepared(pg, "pstmt", 0, NULL, NULL, NULL, 0);
printf("PGresult of execPrepd() ('%s'): ntuples %d\n", PQcmdStatus(r),
PQntuples(r));

PQexec(pg, "DROP RULE r");
return 0;
}

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Sergey Tomoulevitch 2009-10-02 07:35:10 BUG #5094: bad result in dblink_get_result when async command sending
Previous Message Dan French 2009-10-02 02:17:14 BUG #5092: to_ascii(); ascii() don't work with bytea