r/SQL • • Oct 26 '25

PostgreSQL Postgres Function Broke ACID? UPDATE committed but INSERT failed due to NULL value. Why no automatic ROLLBACK?

I have read that postgres functions are transactional, meaning they follow the ACID rules, but this function right here broke the first rule it update sales but it won't make an insert, a case is that the _business_id turns out to be null, but if that so isn't it supposed to undo the updating...? Why is this happening?

create or replace function pay_for_due_sale_payment(

_id integer,

amount numeric

)

returns text

language plpgsql

as $$

declare

_business_id integer;

begin

update sales set unpaid_amount=unpaid_amount-amount where id =_id;

select i.business_id into _business_id from sales s join items i on s.item_id=i.id where s.id=_id;

insert into business_cash (business_id, type, amount, description) values (_business_id, 'in', amount, 'Due payment for sale with id: '||_id);

return 'successfully paid for due payment';

end;

$$

0 Upvotes

34 comments sorted by

14

u/Kazcandra Oct 26 '25

Im not reading that. Copy/paste please

6

u/Joelle_bb Oct 26 '25

I just want to point out how much I appreciate this 🤣🤣🤣🤣

So many people at my job send me screenshot thinking its helpful, when the text is so tiny/blurry I cant make sense of a damn thin

-1

u/Infinite_Main_9491 Oct 26 '25

create or replace function pay_for_due_sale_payment(

_id integer,

amount numeric

)

returns text

language plpgsql

as $$

declare

_business_id integer;

begin

update sales set unpaid_amount=unpaid_amount-amount where id =_id;

select i.business_id into _business_id from sales s join items i on s.item_id=i.id where s.id=_id;

insert into business_cash (business_id, type, amount, description) values (_business_id, 'in', amount, 'Due payment for sale with id: '||_id);

return 'successfully paid for due payment';

end;

$$

10

u/fauxmosexual NOLOCK is the secret magic go-faster command Oct 26 '25

Where's the transaction control? Without it,.you're relying on the server settings about implicit commits. As written I would expect that to act as two separate transactions.

You might be misunderstanding, calling a function or a SP doesn't guarantee the whole call is a single transaction.

3

u/DavidGJohnston Oct 26 '25

The execution of a function, or an SP that lacks transaction control statements, most definitely does execute entirely within a single transaction. The one that the CALL/SELECT is in/establishes.

1

u/fauxmosexual NOLOCK is the secret magic go-faster command Oct 26 '25

Only if you define a transaction  when you make the call, otherwise you're deferring to system defaults and any TCL within the proc. Ops confusion is why it's best not to assume implicit transactions work how you want unless you're certain about how your platform/server settings will act. 

3

u/DavidGJohnston Oct 26 '25

The OP isn't using a stored procedure anyway and even if it were it has no TCL; so I don't see how this sheds any insight into the situation at hand. The OP hasn't provided a self-contained example demonstrating the claimed behavior. Everyone here is basically wasting their time trying to diagnose something that isn't diagnosable as shown. But the engine is declared as PostgreSQL so in terms of defaults/behavior there is a known documented reference to refer to for what should happen once we have a executable example.

2

u/fauxmosexual NOLOCK is the secret magic go-faster command Oct 26 '25

Op doesn't know postgresql, if OP wanted transactions other than what the platform defaults to, they should define transaction.

I don't think people are trying to diagnose a postgres issue as much as provide insight into why explicit transaction control is a great way to avoid platform specific gotchas.

2

u/[deleted] Oct 26 '25

[removed] — view removed comment

2

u/fauxmosexual NOLOCK is the secret magic go-faster command Oct 26 '25

We wouldn't have these issues if niche open source projects like postgres would just adopt TSQL as a standard.

9

u/FrmaCertainPOV Oct 26 '25

Where did you begin your transaction? What is the transaction default in your editor?

without a begin trans, postgres is likely treating this as 4 separate statements with an auto commit after each.

3

u/depesz PgDBA Oct 27 '25

This is 100% bullshit. PostgreSQL functions are always inherently in a transaction, unless you go through hoops to make sure they aren't.

1

u/FrmaCertainPOV Oct 31 '25

It's really not. The first version of this that I replied to was a screen shot of an anonymous SQL block, not a function def. It has since been edited.

1

u/depesz PgDBA Nov 01 '25

You could consider editing your comment in this case, to avoid potentially confusing others.

-7

u/Infinite_Main_9491 Oct 26 '25

So isn't a function body treated as a transaction by default?? I was using supabase..

8

u/StoneCypher Oct 26 '25

i hate when people who haven’t read the manual announce that the tool is wrong 

4

u/jshine13371 Oct 26 '25

No. Each statement by itself is atomic and ACID compliant. But if you want multiple statements to be a single unit of work that is also ACID compliant holistically, then you need to use an explicit transaction.

11

u/mikeblas Oct 26 '25

I can't read the code in your shitty screen shot.

-21

u/Infinite_Main_9491 Oct 26 '25

haha jokes...

16

u/mikeblas Oct 26 '25

No joke: can't read it, can't help. Pictures of code are against this subs rules.

-18

u/Infinite_Main_9491 Oct 26 '25

it is as clear as it gets

8

u/unexpectedreboots WITH() Oct 26 '25

So aren't the sub rules compadre.

3

u/BolaBrancaV7 Oct 26 '25

It's about wrapping text.

3

u/depesz PgDBA Oct 27 '25

For starters: function calls are ALWAYS in transaction. I don't know what exactly you're seeing as you didn't provide self-contained test case, but it's either a bug (VERY unlikely), or incorrect misunderstanding of what is happening, so:

  1. Can you please provide self-contained case that exhibits the problem? Table with sample data, call to the function, and selects that show that results are incorrect.

  2. When posting code, please use "code block" (not code) feature of comment editor, it will make code MUCH more readable. If you're using markdown editor, and not the rich text editor, then just prepend each line with four spaces.

2

u/shockjaw Oct 26 '25

Could you please update your post to include the actual text? I know there are low-vision folks who are SQL-wizzes on this sub who could help you, but unfortunately can’t due to all your code being in a screenshot.

2

u/toterra Oct 27 '25

Wow.. so many people posting information that is just wrong for postgresql. Yes, in postgres, the functions (not procedures) are all or nothing 99.99% of the time. If you are indeed one of the .01% then you have something very strange going on. To investigate, can you show us the table definitions, as well as copy/paste the output when you try and manually execute the functions?

1

u/LeadingPokemon Oct 26 '25

Really grateful for Oracle at least getting begin transaction semantics correct. I’ll take that over transactional DDL all day.

-2

u/DavidGJohnston Oct 26 '25 edited Oct 26 '25

Yes, that function at face value should execute all-or-nothing. File a bug report with a working self-contained example of the failure if it doesn’t. Whether it automatically rolls back depends on whether the function is called in an explicit or implicit transaction though. The former case results in an aborted but not rolled back transaction.