pgmoon icon indicating copy to clipboard operation
pgmoon copied to clipboard

multiple statements in one query

Open liverpool8056 opened this issue 2 years ago • 2 comments

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';

liverpool8056 avatar Mar 04 '23 02:03 liverpool8056

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.

leafo avatar Apr 27 '23 02:04 leafo

@leafo Thank you!

liverpool8056 avatar Apr 27 '23 06:04 liverpool8056