Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit82a4eda

Browse files
committed
hio: Take number of prior relation extensions into account
The new relation extension logic, introduced in00d1e02, could lead toslowdowns in some scenarios. E.g., when loading narrow rows into a table usingCOPY, the caller of RelationGetBufferForTuple() will only request a smallnumber of pages. Without concurrency, we just extended using pwritev() in thatcase. However, if there is *some* concurrency, we switched between extendingby a small number of pages and a larger number of pages, depending on thenumber of waiters for the relation extension logic. However, somefilesystems, XFS in particular, do not perform well when switching betweenextending files using fallocate() and pwritev().To avoid that issue, remember the number of prior relation extensions inBulkInsertState and extend more aggressively if there were prior relationextensions. That not just avoids the aforementioned slowdown, but also leadsto noticeable performance gains in other situations, primarily due toextending more aggressively when there is no concurrency. I should have doneit this way from the get go.Reported-by: Masahiko Sawada <sawada.mshk@gmail.com>Author: Andres Freund <andres@anarazel.de>Discussion:https://postgr.es/m/CAD21AoDvDmUQeJtZrau1ovnT_smN940=Kp6mszNGK3bq9yRN6g@mail.gmail.comBackpatch: 16-, where the new relation extension code was added
1 parent94f9c08 commit82a4eda

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

‎src/backend/access/heap/heapam.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,6 +1776,7 @@ GetBulkInsertState(void)
17761776
bistate->current_buf=InvalidBuffer;
17771777
bistate->next_free=InvalidBlockNumber;
17781778
bistate->last_free=InvalidBlockNumber;
1779+
bistate->already_extended_by=0;
17791780
returnbistate;
17801781
}
17811782

‎src/backend/access/heap/hio.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,24 @@ RelationAddBlocks(Relation relation, BulkInsertState bistate,
283283
*/
284284
extend_by_pages+=extend_by_pages*waitcount;
285285

286+
/* ---
287+
* If we previously extended using the same bistate, it's very likely
288+
* we'll extend some more. Try to extend by as many pages as
289+
* before. This can be important for performance for several reasons,
290+
* including:
291+
*
292+
* - It prevents mdzeroextend() switching between extending the
293+
* relation in different ways, which is inefficient for some
294+
* filesystems.
295+
*
296+
* - Contention is often intermittent. Even if we currently don't see
297+
* other waiters (see above), extending by larger amounts can
298+
* prevent future contention.
299+
* ---
300+
*/
301+
if (bistate)
302+
extend_by_pages=Max(extend_by_pages,bistate->already_extended_by);
303+
286304
/*
287305
* Can't extend by more than MAX_BUFFERS_TO_EXTEND_BY, we need to pin
288306
* them all concurrently.
@@ -409,6 +427,7 @@ RelationAddBlocks(Relation relation, BulkInsertState bistate,
409427
/* maintain bistate->current_buf */
410428
IncrBufferRefCount(buffer);
411429
bistate->current_buf=buffer;
430+
bistate->already_extended_by+=extend_by_pages;
412431
}
413432

414433
returnbuffer;

‎src/include/access/hio.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,22 @@ typedef struct BulkInsertStateData
3232
Buffercurrent_buf;/* current insertion target page */
3333

3434
/*
35-
* State for bulk extensions. Further pages that were unused at the time
36-
* of the extension. They might be in use by the time we use them though,
37-
* so rechecks are needed.
35+
* State for bulk extensions.
36+
*
37+
* last_free..next_free are further pages that were unused at the time of
38+
* the last extension. They might be in use by the time we use them
39+
* though, so rechecks are needed.
3840
*
3941
* XXX: Eventually these should probably live in RelationData instead,
4042
* alongside targetblock.
43+
*
44+
* already_extended_by is the number of pages that this bulk inserted
45+
* extended by. If we already extended by a significant number of pages,
46+
* we can be more aggressive about extending going forward.
4147
*/
4248
BlockNumbernext_free;
4349
BlockNumberlast_free;
50+
uint32already_extended_by;
4451
}BulkInsertStateData;
4552

4653

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp