Is it safe to insert the newest row with SELECT INTO? Trying to keep rows consistent then referencing previous rows.

CREATE TABLE transactions (
    id         int     PRIMARY KEY
       GENERATED BY DEFAULT AS IDENTITY,

    account_id bigint  REFERENCES account,
    change     integer NOT NULL,
    balance    integer NOT NULL,
)


Query from the client is basically

SELECT balance
FROM transactions
WHERE account_id = X
ORDER BY id DESC
LIMIT 1;

INSERT INTO transactions (account_id, change, balance)
VALUES (X, 100, $balance + 100);


This code will produce incorrect results on concurrent inserts. 
So, the question is how to make balance calculation always correct?

Will INSERT INTO ... SELECT(...) be enough?
You could kind of make it work if you included a change_id in the table, had a unique index on (account_id, change_id), and always incremented change_id by 1 from the previous value for that account_id.  Then on conflicting inserts to the same account_id, one will fail with a unique constraint violation and need to re-try.

You could do it in one insert into ... select statement.  Or you can do it in two separate statements as long as they are in one transaction, and the first statement selects both balance and change_id simultaneously.

I think it is horrible design.  But it can work.  Really you have two entities, a transaction and an account, and so should have two tables rather than trying to cram them into one.