Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is JSON type in PostgreSQL part of transactions?

Just want to know if JSON type is also comes under the transactions. For e.g. If I have started a transaction which insert data both for column JSON types and others and if something wrong happens, will it rollback the json stuff as well?

like image 914
user3170450 Avatar asked Nov 25 '25 20:11

user3170450


1 Answers

Everything is transactional and crash-safe in PostgreSQL unless explicitly documented not to be.

PostgreSQL's transactions operate on tuples, not individual fields. The data type is irrelevant. It isn't really possible to implement a data type that is not transactional in PostgreSQL. (The SERIAL "data type" is just a wrapper for the integer type with a DEFAULT, and is a bit of a special case).

Only a few things have special behaviour regarding transactions - sequences, advisory locks, etc - and they're pretty clearly documented where that's the case.

Note that this imposes some limitations you may not immediately expect. Most importantly, because PostgreSQL relies on MVCC for concurrency control it must copy a value when that value is modified (or, sometimes, when other values in the same tuple are modified). It cannot change fields in-place. So if you have a 5MB json document in a field and you change a single integer value, the whole json document must be copied and written out with the changed value. PostgreSQL will then come along later and mark the old copy as free space that can be re-used.

like image 95
Craig Ringer Avatar answered Nov 28 '25 15:11

Craig Ringer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!