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

Optimizing storage and managing cleanup

Transactions are an integral part of the PostgreSQL system. However, transactions come with a small price tag attached. As already shown in this chapter, it can happen that concurrent users will be presented with different data. Not everybody will get the same data returned by a query. In addition to this, DELETE and UPDATE are not allowed to actually overwrite data as ROLLBACK would not work. If you happen to be in the middle of a large DELETE operation, you cannot be sure whether you will be able to COMMIT or not. In addition to this, data is still visible while you perform DELETE, and sometimes data is even visible once your modification has long since finished.

Consequently, this means that cleanup has to happen asynchronously. A transaction cannot clean up its own mess and COMMIT/ROLLBACK might be too early to take care of dead rows.

The solution to this problem is VACUUM:

test=# \h VACUUM 
Command: VACUUM
Description: garbage-collect and optionally analyze a database
Syntax:
VACUUM [ ( { FULL | FREEZE | VERBOSE | ANALYZE } [, ...] ) ] [ table_name [ (column_name [, ...] ) ] ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] [ table_name ]
VACUUM [ FULL ] [ FREEZE ] [ VERBOSE ] ANALYZE [ table_name [ (column_name [, ...] ) ] ]

VACUUM will visit all pages that potentially contain modifications and find all the dead space. The free space found is then tracked by Free Space Map (FSM) of the relation.

Note that VACUUM will, in most cases, not shrink the size of a table. Instead, it will track and find free space inside existing storage files.

Tables will usually have the same size after  VACUUM. If there are no valid rows at the end of a table, file sizes can go down in some rare cases. This is not the rule but rather the exception.

What this means to the end users will be outlined in the Watching VACUUM  at work section of this chapter.

主站蜘蛛池模板: 新津县| 宝应县| 巴林左旗| 尼玛县| 石嘴山市| 综艺| 和平区| 泰顺县| 万源市| 文安县| 灵璧县| 竹溪县| 玉田县| 晋江市| 义马市| 玉树县| 朔州市| 静海县| 营口市| 庆云县| 武宁县| 南江县| 普宁市| 乐山市| 高尔夫| 白水县| 裕民县| 万宁市| 正阳县| 苍山县| 彭山县| 鄂尔多斯市| 枣庄市| 赣榆县| 江华| 华蓥市| 锡林郭勒盟| 霍州市| 合川市| 邹城市| 新巴尔虎右旗|