修改表的填充率

最近更新时间: 2025-02-18 16:02:00

一个表的填充率(fillfactor)默认值是100。INSERT 操作仅按照填充率指定的百分率填充表页。每个页上的剩余空间将用于在该页上的行更新,这就使得 UPDATE 有机会在同一页上放置同一条记录的新版本,这比把新版本放置在其它页上更有效。对于一个从不更新的表将填充因子设为 100 效率最高,但是对于频繁更新的表,较小的填充率则更加有效,但小的填充率会占用更大有空间。

在PostgreSQL中,当更新一行数据时,实际上旧行并没有删除,只是插入了一行新数据。如果这个表其他列上有索引,而更新的列上没有索引,因为新行的物理位置发生变化,因此需要更新索引,这将导致性能下降。为了解决这个问题,PostgreSQL引入了Heap Only Tuple(HOT)技术,如果更新后的新行和旧行位于同一个数据块内,则旧行会有一个指针指向新行,这样就不用更新索引了,通过索引访问到旧行数据,进而访问到新行数据。

  • 复制
    复制成功
postgres=# \d+ t1
                                 Table "tbase_pg_proc.t1"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+---------+--------------+-------------
 f1     | integer |           | not null |         | plain   |              | 
 f2     | integer |           |          |         | plain   |              | 
Indexes:
    "t1_f1_idx" btree (f1)
Distribute By: SHARD(f1)
Location Nodes: dn002, dn001

postgres=# alter table t1 set(fillfactor=80); 
ALTER TABLE
postgres=#  \d+ t1
                                 Table "tbase_pg_proc.t1"
 Column |  Type   | Collation | Nullable | Default | Storage | Stats target | Description 
--------+---------+-----------+----------+---------+---------+--------------+-------------
 f1     | integer |           | not null |         | plain   |              | 
 f2     | integer |           |          |         | plain   |              | 
Indexes:
    "t1_f1_idx" btree (f1)
Distribute By: SHARD(f1)
Location Nodes: dn002, dn001
Options: fillfactor=80

postgres=#