What should I do if error happens when using pg:query ?
xiangnanscu opened this issue · 1 comments
local pgmoon = require("pgmoon")
local pg = pgmoon.new({
host = "127.0.0.1",
port = "5432",
database = "mydb",
user = "postgres"
})
assert(pg:connect())
local res, err = pg:query("select * from users")
if res == nil then
-- what should I do here ? pg:disconnect() or do nothing?
else
pg:keepalive()
end
An error when calling query
generally means that the query you provided is invalid due to an execution error or parsing error (for example: malformed syntax, invalid type, violation of index, etc). It shouldn't be necessary to call disconnect, check the error message.
In the case that the connection was terminated, the error message will reflect that and it's not necessary to call disconnect.
Do keep in mind that postgres connections have some minimal state. It's rare to use it, but if you do, you can terminate the connection and create a new one to reset that state. Check "feature map" for pgbouncer to see examples of queries that can have side effects on the state of the connection: https://www.pgbouncer.org/features.html