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

Commit6c837c2

Browse files
committed
Move I/O before the index_update_stats() buffer lock region.
Commita07e03f enlarged the work donehere under the pg_class heap buffer lock. Two preexisting actions arebest done before holding that lock. Both RelationGetNumberOfBlocks()and visibilitymap_count() do I/O, and the latter might exclusive-lock avisibility map buffer. Moving these reduces contention and risk ofundetected LWLock deadlock. Back-patch to v12, like that commit.Discussion:https://postgr.es/m/20241031200139.b4@rfd.leadboat.com
1 parent6f9dd22 commit6c837c2

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

‎src/backend/catalog/index.c‎

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,9 @@ index_update_stats(Relation rel,
28122812
boolhasindex,
28132813
doublereltuples)
28142814
{
2815+
boolupdate_stats;
2816+
BlockNumberrelpages;
2817+
BlockNumberrelallvisible;
28152818
Oidrelid=RelationGetRelid(rel);
28162819
Relationpg_class;
28172820
ScanKeyDatakey[1];
@@ -2820,6 +2823,38 @@ index_update_stats(Relation rel,
28202823
Form_pg_classrd_rel;
28212824
booldirty;
28222825

2826+
/*
2827+
* As a special hack, if we are dealing with an empty table and the
2828+
* existing reltuples is -1, we leave that alone. This ensures that
2829+
* creating an index as part of CREATE TABLE doesn't cause the table to
2830+
* prematurely look like it's been vacuumed. The rd_rel we modify may
2831+
* differ from rel->rd_rel due to e.g. commit of concurrent GRANT, but the
2832+
* commands that change reltuples take locks conflicting with ours. (Even
2833+
* if a command changed reltuples under a weaker lock, this affects only
2834+
* statistics for an empty table.)
2835+
*/
2836+
if (reltuples==0&&rel->rd_rel->reltuples<0)
2837+
reltuples=-1;
2838+
2839+
update_stats=reltuples >=0;
2840+
2841+
/*
2842+
* Finish I/O and visibility map buffer locks before
2843+
* systable_inplace_update_begin() locks the pg_class buffer. The rd_rel
2844+
* we modify may differ from rel->rd_rel due to e.g. commit of concurrent
2845+
* GRANT, but no command changes a relkind from non-index to index. (Even
2846+
* if one did, relallvisible doesn't break functionality.)
2847+
*/
2848+
if (update_stats)
2849+
{
2850+
relpages=RelationGetNumberOfBlocks(rel);
2851+
2852+
if (rel->rd_rel->relkind!=RELKIND_INDEX)
2853+
visibilitymap_count(rel,&relallvisible,NULL);
2854+
else/* don't bother for indexes */
2855+
relallvisible=0;
2856+
}
2857+
28232858
/*
28242859
* We always update the pg_class row using a non-transactional,
28252860
* overwrite-in-place update. There are several reasons for this:
@@ -2864,15 +2899,6 @@ index_update_stats(Relation rel,
28642899
/* Should this be a more comprehensive test? */
28652900
Assert(rd_rel->relkind!=RELKIND_PARTITIONED_INDEX);
28662901

2867-
/*
2868-
* As a special hack, if we are dealing with an empty table and the
2869-
* existing reltuples is -1, we leave that alone. This ensures that
2870-
* creating an index as part of CREATE TABLE doesn't cause the table to
2871-
* prematurely look like it's been vacuumed.
2872-
*/
2873-
if (reltuples==0&&rd_rel->reltuples<0)
2874-
reltuples=-1;
2875-
28762902
/* Apply required updates, if any, to copied tuple */
28772903

28782904
dirty= false;
@@ -2882,16 +2908,8 @@ index_update_stats(Relation rel,
28822908
dirty= true;
28832909
}
28842910

2885-
if (reltuples >=0)
2911+
if (update_stats)
28862912
{
2887-
BlockNumberrelpages=RelationGetNumberOfBlocks(rel);
2888-
BlockNumberrelallvisible;
2889-
2890-
if (rd_rel->relkind!=RELKIND_INDEX)
2891-
visibilitymap_count(rel,&relallvisible,NULL);
2892-
else/* don't bother for indexes */
2893-
relallvisible=0;
2894-
28952913
if (rd_rel->relpages!= (int32)relpages)
28962914
{
28972915
rd_rel->relpages= (int32)relpages;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp