pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

functions with BEGIN ATOMIC produce a syntax error when dumped

Open whoward opened this issue 10 months ago • 1 comments

Given the following database definition:

CREATE TABLE accounts(user_id INT, balance INT);

CREATE OR REPLACE FUNCTION transfer_funds(sender_id INT, receiver_id INT, amount NUMERIC)
RETURNS void
LANGUAGE SQL
BEGIN ATOMIC
    UPDATE accounts SET balance = balance - amount WHERE user_id = sender_id;
    UPDATE accounts SET balance = balance + amount WHERE user_id = receiver_id;
END;

When pgschema dump is run, we get the following output:

--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 17.0
-- Dumped by pgschema version 1.6.0


--
-- Name: accounts; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS accounts (
    user_id integer,
    balance integer
);

--
-- Name: transfer_funds(integer, integer, numeric); Type: FUNCTION; Schema: -; Owner: -
--

CREATE OR REPLACE FUNCTION transfer_funds(
    sender_id integer,
    receiver_id integer,
    amount numeric
)
RETURNS void
LANGUAGE sql
VOLATILE
AS $$BEGIN ATOMIC
 UPDATE accounts SET balance = ((accounts.balance)::numeric - transfer_funds.amount)
   WHERE (accounts.user_id = transfer_funds.sender_id);
 UPDATE accounts SET balance = ((accounts.balance)::numeric + transfer_funds.amount)
   WHERE (accounts.user_id = transfer_funds.receiver_id);
END$$;

When this dump is fed back into pgschema plan we get the following error:

Error: failed to apply desired state: failed to apply schema SQL to temporary schema pgschema_tmp_20260114_184221_2ffb480d: ERROR: syntax error at or near "ATOMIC" (SQLSTATE 42601)

whoward avatar Jan 14 '26 23:01 whoward

working great, thanks!

whoward avatar Jan 15 '26 18:01 whoward