- Mastering PostgreSQL 10
- Hans Jürgen Sch?nig
- 408字
- 2021-06-30 19:03:49
Working with PostgreSQL transactions
PostgreSQL provides you with a highly advanced transaction machinery that offers countless features to developers and administrators alike. In this section, it is time to look at the basic concept of transactions.
The first important thing to know is that in PostgreSQL, everything is a transaction. If you send a simple query to the server, it is already a transaction. Here is an example:
test=# \x Expanded display is on.
test=# SELECT now(), now();
now | now
-------------------------------+------------------------------
2017-08-24 16:03:27.174253+02 | 2017-08-24 16:03:27.174253+02
(1 row)
In this case, the SELECT statement will be a separate transaction. If the same command is executed again, different timestamps will be returned.
If more than one statement has to be a part of the same transaction, the BEGIN statement must be used:
test=# \h BEGIN
Command: BEGIN
Description: Start a transaction block
Syntax:
BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]
where transaction_mode is one of:
ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ
| READ COMMITTED | READ UNCOMMITTED }
READ WRITE | READ ONLY
[ NOT ] DEFERRABLE
The BEGIN statement will ensure that more than one command will be packed into a transaction. Here is how it works:
test=# BEGIN; BEGIN
test=# SELECT now(); now ------------------------------- 2017-08-24 16:04:08.105131+02 (1 row)
test=# SELECT now(); now ------------------------------- 2017-08-24 16:04:08.105131+02 (1 row)
test=# COMMIT; COMMIT
The important point here is that both timestamps will be identical. As mentioned earlier, we are talking about transaction time here.
To end the transaction, COMMIT can be used:
test=# \h COMMIT
Command: COMMIT
Description: Commit the current transaction
Syntax:
COMMIT [ WORK | TRANSACTION ]
There are a couple of syntax elements here. You can just use COMMIT, COMMIT WORK, or COMMIT TRANSACTION. All three options have the same meaning. If this is not enough, there is more:
test=# \h END Command: END Description: commit the current transaction Syntax: END [ WORK | TRANSACTION ]
The END clause is the same as the COMMIT clause.
ROLLBACK is the counterpart of COMMIT. Instead of successfully ending a transaction, it will simply stop the transaction without ever making things visible to other transactions:
test=# \h ROLLBACK
Command: ROLLBACK
Description: Abort the current transaction
Syntax:
ROLLBACK [ WORK | TRANSACTION ]
Some applications use ABORT instead of ROLLBACK. The meaning is the same.
- WOW!Illustrator CS6完全自學(xué)寶典
- 腦動力:PHP函數(shù)速查效率手冊
- VB語言程序設(shè)計
- 計算機網(wǎng)絡(luò)技術(shù)實訓(xùn)
- 統(tǒng)計策略搜索強化學(xué)習(xí)方法及應(yīng)用
- 信息物理系統(tǒng)(CPS)測試與評價技術(shù)
- JavaScript典型應(yīng)用與最佳實踐
- 網(wǎng)中之我:何明升網(wǎng)絡(luò)社會論稿
- 面向?qū)ο蟪绦蛟O(shè)計綜合實踐
- 軟件工程及實踐
- Mastering Geospatial Analysis with Python
- Hands-On Dashboard Development with QlikView
- JRuby語言實戰(zhàn)技術(shù)
- 工業(yè)機器人操作
- 軟件測試設(shè)計