Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is supported as transactional in postgres

I am trying find out what is postgres can handle safely inside of transaction, but I cannot find the relavant information in the postgres manual. So far I have found out the following:

  • UPDATE, INSERT and DELTE are fully supported inside transactions and rolled back when the transaction is not finished
  • DROP TABLE is not handled safely inside a transaction, and is undone with a CREATE TABLE, thus recreates the dropped table but does not repopulate it
  • CREATE TABLE is also not truly transactionized and is instead undone with a corresponding DROP TABLE

Is this correct? Also I could not find any hints as to the handling of ALTER TABLE and TRUNCATE. In what way are those handled and are they safe inside transactions? Is there a difference of the handling between different types of transactions and different versions of postgres?

like image 207
LiKao Avatar asked Jun 10 '11 08:06

LiKao


People also ask

How are the transactions supported in PostgreSQL?

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it. A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block.

Is Postgres function transactional?

Yes, functions are transactional, even if written in LANGUAGE SQL .

Does Postgres support distributed transaction?

Postgres does not support distributed transactions, so all commandsof a transaction are executed by one backend. We don't currently handle nested transactions, either.

What does Transactional mean in SQL?

A transaction is a single unit of work. If a transaction is successful, all of the data modifications made during the transaction are committed and become a permanent part of the database. If a transaction encounters errors and must be canceled or rolled back, then all of the data modifications are erased.


2 Answers

DROP TABLE is transactional. To undo this, you need to issue a ROLLBACK not a CREATE TABLE. The same goes for CREATE TABLE (which is also undone using ROLLBACK).

ROLLBACK is always the only correct way to undo a transaction - that includes ALTER TABLE and TRUNCATE.

The only thing that is never transactional in Postgres are the numbers generated by a sequence (CREATE/ALTER/DROP SEQUENCE themselves are transactional though).

like image 122
a_horse_with_no_name Avatar answered Oct 01 '22 07:10

a_horse_with_no_name


Best I'm aware all of these commands are transaction aware, except for TRUNCATE ... RESTART IDENTITY (and even that one is transactional since 9.1.)

See the manual on concurrency control and transaction-related commands.

like image 36
Denis de Bernardy Avatar answered Oct 01 '22 07:10

Denis de Bernardy