What's new
PostgresPool
A new pgmoon.pool module provides a connection pool that matches the Postgres interface, so it can be used as a drop-in replacement when you need to issue queries from multiple coroutines concurrently.
local PostgresPool = require("pgmoon.pool").PostgresPool
local pool = PostgresPool({
host = "127.0.0.1", database = "mydb", user = "postgres",
max_pool_size = 10
})
assert(pool:connect())
local res = assert(pool:query("select * from users limit 10"))For multi-query transactions, pool:reserve() / pool:release(pg) give you exclusive access to a single connection. Releasing a connection that's still in a transaction will automatically issue a ROLLBACK to keep the pool clean. See the README's PostgresPool section for the full API.
Unix socket connections (socket_path)
You can now connect over a Unix domain socket by passing socket_path to Postgres.new:
local pg = pgmoon.new({
socket_path = "/var/run/postgresql/.s.PGSQL.5432",
database = "mydb", user = "postgres",
})In OpenResty this uses the cosocket unix: syntax. Outside OpenResty it requires luaposix (luarocks install luaposix).
transaction_status
The Postgres object now exposes a transaction_status field that reflects the server's reported transaction state after each query: "I" (idle), "T" (in transaction), or "E" (failed transaction, awaiting ROLLBACK). This is also used by keepalive to auto-ROLLBACK dirty connections before returning them to the OpenResty pool.
Connection busy tracking
connect, query, disconnect, keepalive, and wait_for_notification now raise "pgmoon: connection is busy" if invoked while the connection is already mid-operation (e.g. from another coroutine that yielded mid-query) rather than silently corrupting the protocol stream. If you need concurrent access, use PostgresPool.
Other changes
- Add deserializer for
inet[]arrays (oid 1041) - More descriptive error message when the server requests a password but none was provided
- Doc fix for
pool_nameconfig option (#142) - Numerous README typo and example fixes