mirror of
https://github.com/postgres/postgres.git
synced 2025-07-31 22:04:40 +03:00
Speedup truncations of relation forks.
When a relation is truncated, shared_buffers needs to be scanned so that any buffers for the relation forks are invalidated in it. Previously, shared_buffers was scanned for each relation forks, i.e., MAIN, FSM and VM, when VACUUM truncated off any empty pages at the end of relation or TRUNCATE truncated the relation in place. Since shared_buffers needed to be scanned multiple times, it could take a long time to finish those commands especially when shared_buffers was large. This commit changes the logic so that shared_buffers is scanned only one time for those three relation forks. Author: Kirk Jamison Reviewed-by: Masahiko Sawada, Thomas Munro, Alvaro Herrera, Takayuki Tsunakawa and Fujii Masao Discussion: https://postgr.es/m/D09B13F772D2274BB348A310EE3027C64E2067@g01jpexmbkw24
This commit is contained in:
@ -383,6 +383,8 @@ pg_truncate_visibility_map(PG_FUNCTION_ARGS)
|
||||
{
|
||||
Oid relid = PG_GETARG_OID(0);
|
||||
Relation rel;
|
||||
ForkNumber fork;
|
||||
BlockNumber block;
|
||||
|
||||
rel = relation_open(relid, AccessExclusiveLock);
|
||||
|
||||
@ -392,7 +394,12 @@ pg_truncate_visibility_map(PG_FUNCTION_ARGS)
|
||||
RelationOpenSmgr(rel);
|
||||
rel->rd_smgr->smgr_vm_nblocks = InvalidBlockNumber;
|
||||
|
||||
visibilitymap_truncate(rel, 0);
|
||||
block = visibilitymap_prepare_truncate(rel, 0);
|
||||
if (BlockNumberIsValid(block))
|
||||
{
|
||||
fork = VISIBILITYMAP_FORKNUM;
|
||||
smgrtruncate(rel->rd_smgr, &fork, 1, &block);
|
||||
}
|
||||
|
||||
if (RelationNeedsWAL(rel))
|
||||
{
|
||||
@ -418,7 +425,7 @@ pg_truncate_visibility_map(PG_FUNCTION_ARGS)
|
||||
* here and when we sent the messages at our eventual commit. However,
|
||||
* we're currently only sending a non-transactional smgr invalidation,
|
||||
* which will have been posted to shared memory immediately from within
|
||||
* visibilitymap_truncate. Therefore, there should be no race here.
|
||||
* smgr_truncate. Therefore, there should be no race here.
|
||||
*
|
||||
* The reason why it's desirable to release the lock early here is because
|
||||
* of the possibility that someone will need to use this to blow away many
|
||||
|
Reference in New Issue
Block a user