leafo / pgmoon

A pure Lua Postgres driver for use in OpenResty & more
MIT License
390 stars 93 forks source link

multiple statements in one query #137

Open liverpool8056 opened 1 year ago

liverpool8056 commented 1 year ago

Hi I found when sending multiple statements separated by ';\n' in one query, they will be included in one single transaction on Postgres. And I tried to do the similar thing via plsql or Java JDBC Framework , these statements will be separated into different transactions respectively. I just want to confirm if it is as expected?

ENV: pgmoon 1.16.0-1

Here is my test case:

local pgmoon = require("pgmoon")
local pg = pgmoon.new({
  host = "127.0.0.1",
  port = "5432",
  database = "db",
  user = "dbuser",
  password = "pass",
})

assert(pg:connect())

statements = [[
DELETE FROM "events" WHERE "expire_at" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
DELETE FROM "audit_log" WHERE "ttl" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
]]

assert(pg:query(statements))

The query log output on Postgres:

STATEMENT:    DELETE FROM "events" WHERE "expire_at" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
      DELETE FROM "audit_log" WHERE "ttl" < CURRENT_TIMESTAMP AT TIME ZONE 'UTC';
leafo commented 1 year ago

This is expected, this is functionality of the postgres simple query protocol. If you use the extended protocol then you can only issue one query at a time.

liverpool8056 commented 1 year ago

@leafo Thank you!