Using pg_squeeze
Suggest editsUse pg_squeeze
to remove unused space from a table and optionally sort tuples according to a particular index.
Understanding lock levels while using pg_squeeze
While using pg_squeeze
, the extension performs a series of actions, and some of these actions place a lock on the squeezed table, as described in the following steps. The table being squeezed is available for both read and write operations by other transactions most of the time.
When squeezing a table, the extension:
Creates a transient table and copies the contents of the source table using the snapshot.
pg_squeeze
holds anACCESS SHARE
lock on the source table during this step.Builds indexes on the transient table.
pg_squeeze
holds anACCESS SHARE
lock on the source table during this step.Decodes and applies the concurrent changes that occurred on the source table while the initial load is in progress.
Applies any concurrent changes that take place while waiting for the lock and swaps the storage of the transient and the source table.
pg_squeeze
holds anACCESS EXCLUSIVE
lock for a short period during this step.Drops the transient table.
Registering a table for regular processing
Before using pg_squeeze
, ensure that your table has either a primary key or a replica identity. This is necessary to process changes resulting from other transactions while pg_squeeze
is doing its work.
To make the pg_squeeze
extension aware of the table, you must insert a record into the squeeze.tables
table. After it is added, table statistics are checked periodically. When the table meets the necessary criteria to be squeezed, a task is added to a queue. The tasks are processed sequentially, in the order they are created.
Here is an example of a simple registration:
Additional columns can be specified optionally, for example:
The following list describes the table metadata:
tabschema
— Schema name.tabname
— Table name.schedule
— Specifies when the table should be checked and possibly squeezed. The schedule is described by a value of the following composite data type, which resembles a crontab entry:In this data type
minutes
(0-59) andhours
(0-23) determine the time when the check occurs within a day, whiledays_of_month
(1-31),months
(1-12) anddays_of_week
(0-7, where both 0 and 7 stand for Sunday) determine the day of the check.The check is performed if the
minutes
,hours
, andmonths
all match the current timestamp.NULL
means any minute, hour, and month, respectively. Eitherdays_of_month
ordays_of_week
must match the current timestamp or both must beNULL
for the check to take place.For example, the entries in the sample registration specify to check the table
public.bar
every Wednesday and Friday at 22:30.free_space_extra