20
Conflicts: Solutions
Solution 0: Avoid conflicts
avoid running commands that acquire locks
disable file truncation at end of vacuuming: retention setting
vacuum_truncate = off
Solution 1: Delay applying conflicting records
max_standby_streaming_delay
max_standby_archive_delay
except deadlocks, DROP TABLESPACE, DROP DATABASE
One way to handle conflicts is to avoid operations on the leader that would
result in exclusive locks. For example, you can avoid running commands like
DROP TABLE, TRUNCATE, LOCK, DROP INDEX, DROP TRIGGER, and
similar operations, or run them only when you know there are no active
queries on the replica.
Some implicit locks can be avoided by disabling the corresponding
functionality on the leader. For instance, if vacuum frees up pages at the end
of a file, shrinking the file size requires an exclusive lock on the table.
Then, you can disable file truncation by setting the table storage parameter
vacuum_truncate = off. However, vacuum cannot be disabled or postponed
indefinitely, so this approach won't help avoid conflicts when vacuum
removes tuples that are still needed by queries.
Another approach is to delay the application of conflicting records on the
replica. This method works with both streaming replication and archive-
based replication; it delays the application of records related to both vacuum
operations and exclusive locks. You can set the maximum
application delay (measured from when the record is received).
If the conflicting query hasn't finished by that time, it will be terminated.
However, conflicts caused by rare causes (database or tablespace deletion,
deadlocks) are not delayed and are resolved immediately by terminating the
query.