CREATE PROCEDURE
Creates a persisted routine with no return value. In this stage, executable bodies are supplied by a language extension such as PLAmphi:
CREATE PROCEDURE transfer_stock(source_id int4, target_id int4, amount int4)
LANGUAGE plamphi AS $$
if (amount <= 0) { throw 'Amount must be positive'; }
transaction {
const remaining = sql<int4?>`UPDATE inventory
SET quantity = quantity - ${amount}
WHERE client_id = ${source_id} AND quantity >= ${amount}
RETURNING quantity`;
if (remaining == null) { throw 'Insufficient stock'; }
sql<int4?>`UPDATE inventory
SET quantity = quantity + ${amount}
WHERE client_id = ${target_id}
RETURNING quantity`;
}
$$;
CALL transfer_stock(1, 2, 4);
CALL accepts positional arguments compatible with the declared types and
returns no row. A transaction {} block commits atomically when called outside
a caller transaction and uses a savepoint inside BEGIN; the outer COMMIT or
ROLLBACK still controls the caller transaction. Errors propagate their
SQLSTATE and roll back writes performed in the block.
CREATE OR REPLACE PROCEDURE replaces the revision with the same signature.
DROP PROCEDURE name(type, ...) removes the selected routine. OUT and
INOUT parameters, autonomous transactions, and procedures in languages that
do not implement the procedure contract are outside this stage.