从表中清除文件而不删除PostgreSQL 9.6.3中的行

时间:2018-09-19 08:01:52

标签: postgresql postgresql-9.6

我有一个包含文件的表以及与此表的各种关系,文件存储为bytea。我想释放旧文件占用的空间(根据时间戳),但是表中仍应存在行。

null设置为bytea字段是否足够?这样会实际从表中删除数据吗?

1 个答案:

答案 0 :(得分:1)

In PostgreSQL, updating a row creates a new tuple (row version), and the old one is left to be deleted by autovacuum.

Also, larger bytea attributes will be stored out-of-line in the TOAST table that belongs to the table.

When you set the bytea attribute to NULL (which is the right thing to do), two things will happen:

  • The main table will become bigger because of all the new tuples created by the UPDATE. Autovacuum will free the space, but not shrink the table (the empty space can be re-used by future data modifications).

  • Entries in the TOAST table will be deleted. Again, autovacuum will free the space, but the table won't shrink.

So what you will actually observe is that after the UPDATE, your table uses more space than before.

You can get rid of all that empty space by running VACUUM (FULL) on the table, but that will block concurrent access to the table for the duration of the operation, so be ready to schedule some down time (you'll probably do that for the UPDATE anyway).