官术网_书友最值得收藏!

Transactional DDLs

PostgreSQL has a very nice feature that is unfortunately not present in many commercial database systems. In PostgreSQL, it is possible to run DDLs (commands that change the data structure) inside a transaction block. In a typical commercial system, a DDL will implicitly commit the current transaction. Not so in PostgreSQL.

Apart from some minor exceptions (DROP DATABASE, CREATE TABLESPACE/DROP TABLESPACE, and so on), all DDLs in PostgreSQL are transactional, which is a huge plus and a real benefit to end users.

Here is an example:

test=# d 
No relations found.
test=# BEGIN;
BEGIN
test=# CREATE TABLE t_test (id int);
CREATE TABLE
test=# ALTER TABLE t_test ALTER COLUMN id TYPE int8;
ALTER TABLE
test=# d t_test
Table "public.t_test"
Column | Type | Modifiers
--------+--------+-----------
id | bigint |

test=# ROLLBACK;
ROLLBACK
test=# d t_test
Did not find any relation named "t_test".

In this example, a table has been created and modified, and the entire transaction is aborted instantly. As you can see, there is no implicit COMMIT or any other strange behavior. PostgreSQL simply acts as expected.

Transactional DDLs are especially important if you want to deploy software. Just imagine running a CMS. If a new version is released, you'll want to upgrade. Running the old version would still be OK; running the new version is also OK but you really don't want a mixture of old and new. Therefore, deploying an upgrade in a single transaction is definitely highly beneficial as it makes upgrades an atomic operation.

psql allows you to include files using the i directive. It allows you to start a transaction, load various files, and execute them in a single transaction.
主站蜘蛛池模板: 许昌县| 铜鼓县| 牟定县| 阿克苏市| 长顺县| 信阳市| 襄汾县| 延川县| 阜南县| 裕民县| 池州市| 临高县| 阳原县| 广水市| 香港 | 南部县| 正宁县| 都昌县| 盈江县| 兴和县| 金溪县| 泌阳县| 宕昌县| 措美县| 沙河市| 安泽县| 庐江县| 凉城县| 兴义市| 马关县| 大竹县| 新源县| 海原县| 乡城县| 郁南县| 砀山县| 皮山县| 上杭县| 托克托县| 吴堡县| 三亚市|