Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything one cannot do (and then rollback safely) in transaction?

Is there anything one cannot do (and then rollback safely) in transaction?

My transaction is a simple test of a major, disruptive change in a production database. The SQL code wrapped in a transaction consists of statements such as below. The SQL code works in the test database, with a tiny amount of data, where it runs and can be rolled back fine, taking up 0.5 seconds per transaction. I want to measure the performance of the code in production, with several GB of data deleted and then written in the transaction, estimated to take up to 30 min.

ALTER TABLE
UPDATE ...
DROP VIEW ... CASCADE
CREATE OR REPLACE VIEW ...
CREATE MATERIALIZED VIEW ...
CREATE UNIQUE INDEX ... ON <the materialized view just created above> ...
-- I can omit "CONCURRENTLY" if needed:
REFRESH MATERIALIZED VIEW CONCURRENTLY <the materialized view just created above>
CREATE OR REPLACE VIEW ...

Related:

  • Determining T-SQL / DDL that cannot be executed in a transaction
  • Is it possible to run multiple DDL statements inside a transaction (within SQL Server)?
  • Huge transaction in Sql Server, are there any problems?
like image 895
Timur Shtatland Avatar asked Oct 28 '25 14:10

Timur Shtatland


1 Answers

Think twice

If at all possible, run invasive tests on a copy of the productive database, don't take the risk of breaking your productive DB. Even if all goes well, your presence won't go unnoticed. You may slow down or block concurrent activity.
The fastest way to create a copy within the same DB cluster - while there are no concurrent sessions:

CREATE DATABASE mydb_test TEMPLATE mydb;

See:

  • How to clone a test database from a production one in one single action?

You avoid direct friction, blockage and breakage. But you still compete for resources when testing on the same server. Testing on a different server would avoid that, too ...

About ROLLBACK

In Postgres, the vast majority of all DML and DDL commands are fully transactional (can run inside a transaction block) and their effects can be rolled back. Just make sure you don't COMMIT by accident.

Notable exceptions include: (none of these lists are comprehensive)

Things that don't even run in a transaction

There are some commands that cannot run in a transaction context to begin with. So they also cannot be rolled back. If you tried to include any of those in your transaction, Postgres would raise an exception before they even run. So nothing lost except the work that had been done already - and is rolled back.

  • CREATE DATABASE, DROP DATABASE

  • CREATE TABLESPACE, DROP TABLESPACE

  • CREATE INDEX CONCURRENTLY, DROP INDEX CONCURRENTLY, REINDEX CONCURRENTLY

  • REINDEX - when including a partitioned index or table

  • VACUUM

  • BEGIN, START TRANSACTION, COMMIT, ROLLBACK - obviously. Those start & end transactions and cannot run inside one. Only raise a WARNING if called in the wrong place.

  • CALL - if the called procedure includes transaction control commands (see above).

  • DO - if the code block includes transaction control commands (see above).

Things that are not (or cannot be) rolled back

Those are the ones you really need to be aware of. Like:

  • Effects of the sequence manipulation functions setval() and nextval(), implicitly used by serial and IDENTITY columns. So expect gaps in serial numbers. See:

    • Serial numbers per group of rows for compound key
    • How to reset Postgres' primary key sequence when it falls out of sync?
  • Anything written to log files

  • Anything returned to clients

  • Effects of dblink calls (or similar). See:

    • Does Postgres support nested or autonomous transactions?

Corner cases

  • LISTEN, UNLISTEN, and NOTIFY are transactional, but there are some edge cases.

  • TRUNCATE - the manual:

TRUNCATE is not MVCC-safe. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred. See Section 13.6 for more details.

TRUNCATE is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit.

Related:

  • Do stored procedures run in database transaction in Postgres?
  • Can I dry run/sandbox sql commands?
like image 189
Erwin Brandstetter Avatar answered Oct 30 '25 05:10

Erwin Brandstetter