electric-sql / electric-sql/pglite

Can't seem to use prepared statements using libpq

Open
#223 12 comments 2 reactions 1 assignee Claimed by @tdrz View on GitHub
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

I'm trying to use pglite with a libpq-based application. I'm using pg-gateway as a proxy.

Following sample C code snippet (which calls `PQprepare` and `PQexecPrepared`) seems not work with pglite.

```c
int main(int argc, char** argv) {
char conninfo[200];
const char *port = argv[1];
sprintf(conninfo, "host=localhost port=%s dbname=postgres user=psi password=abc", port);
PGconn *conn;
PGresult *res;

const char *paramValues[1];
paramValues[0] = argv[2];

conn = PQconnectdb(conninfo);

const char *stmtName = "my_statement";
const char *sql = "select $1";

res = PQprepare(conn, stmtName, sql, 1, NULL);
PQclear(res);
res = PQexecPrepared(conn, stmtName, 1, paramValues, NULL, NULL, 0);

if (PQresultStatus(res) != PGRES_TUPLES_OK) {
fprintf(stderr, "Execution of statement failed: %s", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
exit(1);
}

printf("%s\n", PQgetvalue(res, 0, 0));
PQclear(res);
PQfinish(conn);
return 0;
}
```
When I run this using a normal Postgres database I get the expected result:

```
willem@FH3T244:~/test$ ./pg 5432 hello
hello
willem@FH3T244:~/test$
```

When I use pglite, I get this:
```
willem@FH3T244:~/test$ ./pg 5433 hello
DEBUG: parse my_statement: select $1
DEBUG: bind to my_statement
Execution of statement failed:
```
Code snippet from the server, mostly borrowed from https://github.com/supabase-community/pg-gateway?tab=readme-ov-file#pglite:

```js
async onMessage(data, { isAuthenticated }) {
if (!isAuthenticated) {
return false;
}
try {
const d = await db.execProtocolRaw(data);
socket.write(d);
} catch (err) {
console.log(err)
connection.sendError(err);
connection.sendReadyForQuery();
}
...
```

Seems to happen on both Linux and Windows. When I don't use prepared statements (e.g. by using `PQexec`), it seems to work as expected.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.