The following bug has been logged online:
Bug reference: 5093
Logged by: Mike Pomraning
Email address: mjp@pilcrow.madison.wi.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;
}