Thread: Question about VACUUM behavior with sub-transactions in stored procedures
Question about VACUUM behavior with sub-transactions in stored procedures
Hi hackers,,
I have a question regarding the behavior of the auto VACUUM in PostgreSQL in the context of using stored procedures with sub-transactions.
As I understand it, the parameters that control the initiation of VACUUM are set in the configuration file, such as autovacuum_vacuum_threshold
, autovacuum_vacuum_scale_factor
, and are stored in the system tables pg_stat_user_tables or pg_class (please correct me if I'm wrong). These system tables are updated after each completed transaction, and VACUUM analyzes them to determine whether to clean up dead rows, depending on the configured thresholds.
Here is the scenario: we have several stored procedures that modify or update table data. These procedures use sub-transactions, which are committed via COMMIT
. However, from my understanding, the system table, which VACUUM checks, is not updated until the main (outermost) transaction completes. This means that during the execution of the procedures, a significant number of dead rows may accumulate, and only after the final COMMIT
of the main transaction do these dead rows become visible for VACUUM.
As a result, there could be a sharp spike in CPU usage when VACUUM runs after the completion of the procedures, as it begins to clean up a large number of accumulated dead rows.
I would like to know if this behavior is expected and correct? Or could there be a potential issue or bug in this scenario?
To illustrate the issue, here’s an example:
Thank you in advance for your help!
With Regards,
Vyacheslav Kirillov!
Re: Question about VACUUM behavior with sub-transactions in stored procedures
I have a question regarding the behavior of the auto VACUUM in PostgreSQL in the context of using stored procedures with sub-transactions.
Here is the scenario: we have several stored procedures that modify or update table data. These procedures use sub-transactions, which are committed via
COMMIT
.
Re: Question about VACUUM behavior with sub-transactions in stored procedures
stats_timeout = pgstat_report_stat(false);if (stats_timeout > 0) { if (!get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT)) enable_timeout_after(IDLE_STATS_UPDATE_TIMEOUT, stats_timeout); } else { /* all stats flushed, no need for the timeout */ if (get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT)) disable_timeout(IDLE_STATS_UPDATE_TIMEOUT, false); }
- Is it expected behavior that system table updates are deferred until all nested transactions are complete? This would mean that auto VACUUM might not account for dead tuples accumulated during procedure execution until the entire main transaction is finished.
- Is it possible or advisable to call pgstat_report_stat after each CommitTransactionCommand() within procedures so that auto VACUUM can track intermediate changes and prevent an accumulation of dead rows?
- To what extent would this approach be viable in terms of performance and correctness?
Vyacheslav Kirillov
Отправлено: 21 октября 2024 г. 16:55
Кому: Кириллов Вячеслав
Копия: pgsql-hackers@postgresql.org
Тема: Re: Question about VACUUM behavior with sub-transactions in stored procedures
I have a question regarding the behavior of the auto VACUUM in PostgreSQL in the context of using stored procedures with sub-transactions.
Here is the scenario: we have several stored procedures that modify or update table data. These procedures use sub-transactions, which are committed via
COMMIT
.
Re: Question about VACUUM behavior with sub-transactions in stored procedures
stats_timeout = pgstat_report_stat(false);if (stats_timeout > 0)
{ if (!get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT)) enable_timeout_after(IDLE_STATS_UPDATE_TIMEOUT, stats_timeout);
}
else
{ /* all stats flushed, no need for the timeout */ if (get_timeout_active(IDLE_STATS_UPDATE_TIMEOUT)) disable_timeout(IDLE_STATS_UPDATE_TIMEOUT, false);
}
- Is it expected behavior that system table updates are deferred until all nested transactions are complete? This would mean that auto VACUUM might not account for dead tuples accumulated during procedure execution until the entire main transaction is finished.
- Is it possible or advisable to call pgstat_report_stat after each CommitTransactionCommand() within procedures so that auto VACUUM can track intermediate changes and prevent an accumulation of dead rows?
- To what extent would this approach be viable in terms of performance and correctness?
Vyacheslav Kirillov