Transactions and Error Handling in PostgreSQL
Jason Myers
Principal Engineer
DO $$
BEGIN
UPDATE cookies SET quantity = quantity-6 WHERE name = 'Linga';
EXCEPTION
WHEN check_violation THEN
INSERT INTO errors (msg) values ('Quantity can not be less than 0');
UPDATE cookies SET quantity = 0 WHERE name = 'Linga';
INSERT INTO errors (msg) values ('Set quantity to the 0 for Linga.');
END$$;
Transactions and Error Handling in PostgreSQL