Closed richard-powers closed 7 months ago
It seems my main problem was misunderstanding that Result
could not be used if the connection was closed, I got this working by keeping the connection open until I was finished with the Result
.
Is this intended?
Very much so, yes. The connection holds the socket that's used to communicate with PostgreSQL. There's more back and forth, but for simplicity, when you send a query and read a response, you essentially write the query to the socket, e.g. "select * from products", and then read the response from the socket. In order not to keep the connection alive for this would involve reading the entire response in memory. For a large result set this might take hundreds of thousands of megabytes.
You could, pass the socket to the result, make it own the socket, so that the query can be read from the socket without the connection being valid. But it's pretty uncommon, I think, that people just want to run 1 query. Typically, connections are re-used (as part of a pool, which pg.zig offers), because opening and closing connections is relatively expensive - better to keep them open and re-use them.
If you're using the pool, then you can call pool.query(...)
for one-off queries. In that case, the returned result "owns" the connection, so that when you call result.deinit()
, the connection will automatically be returned to the pool.
I believe pool.query doesn't exist on the version I'm using, but that's really useful information. Thanks!
Zig version: 0.11.0
build.zig.zon
:I've created a min-repro:
zig build run
output:For more info that may be helpful, there is just 1 row in the table. The query is correct
SELECT * FROM public."Product" p WHERE p."companyId"=1
, and pg.zig correctly reports back the number of columns. It just segfaults onresult.next()