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

Commitdf3e7b3

Browse files
committed
In UpdateStats(), don't bother to update the pg_class row if it already
contains the correct statistics. This is a partial solution for theproblem of allowing concurrent CREATE INDEX commands: unless they commitat nearly the same instant, the second one will see the first one'spg_class updates as committed, and won't try to update again, thusavoiding the 'tuple concurrently updated' failure.
1 parent1d01d48 commitdf3e7b3

File tree

1 file changed

+40
-54
lines changed

1 file changed

+40
-54
lines changed

‎src/backend/catalog/index.c

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.198 2002/09/2219:42:50 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/catalog/index.c,v 1.199 2002/09/2223:03:58 tgl Exp $
1212
*
1313
*
1414
* INTERFACE ROUTINES
@@ -1168,13 +1168,8 @@ setRelhasindex(Oid relid, bool hasindex, bool isprimary, Oid reltoastidxid)
11681168
}
11691169

11701170
if (!HeapTupleIsValid(tuple))
1171-
{
1172-
if (pg_class_scan)
1173-
heap_endscan(pg_class_scan);
1174-
heap_close(pg_class,RowExclusiveLock);
11751171
elog(ERROR,"setRelhasindex: cannot find relation %u in pg_class",
11761172
relid);
1177-
}
11781173

11791174
/*
11801175
* Update fields in the pg_class tuple.
@@ -1319,11 +1314,7 @@ UpdateStats(Oid relid, double reltuples)
13191314
HeapTupletuple;
13201315
HeapTuplenewtup;
13211316
BlockNumberrelpages;
1322-
inti;
13231317
Form_pg_classrd_rel;
1324-
Datumvalues[Natts_pg_class];
1325-
charnulls[Natts_pg_class];
1326-
charreplace[Natts_pg_class];
13271318
HeapScanDescpg_class_scan=NULL;
13281319
boolin_place_upd;
13291320

@@ -1375,13 +1366,8 @@ UpdateStats(Oid relid, double reltuples)
13751366
}
13761367

13771368
if (!HeapTupleIsValid(tuple))
1378-
{
1379-
if (pg_class_scan)
1380-
heap_endscan(pg_class_scan);
1381-
heap_close(pg_class,RowExclusiveLock);
13821369
elog(ERROR,"UpdateStats: cannot find relation %u in pg_class",
13831370
relid);
1384-
}
13851371

13861372
/*
13871373
* Figure values to insert.
@@ -1417,57 +1403,57 @@ UpdateStats(Oid relid, double reltuples)
14171403
}
14181404

14191405
/*
1420-
* We shouldn't have to do this, but we do... Modify the reldesc in
1421-
* place with the new values so that the cache contains the latest
1422-
* copy.
1406+
* Update statistics in pg_class, if they changed. (Avoiding an
1407+
* unnecessary update is not just a tiny performance improvement;
1408+
* it also reduces the window wherein concurrent CREATE INDEX commands
1409+
* may conflict.)
14231410
*/
1424-
whichRel->rd_rel->relpages= (int32)relpages;
1425-
whichRel->rd_rel->reltuples=reltuples;
1411+
rd_rel= (Form_pg_class)GETSTRUCT(tuple);
14261412

1427-
/*
1428-
* Update statistics in pg_class.
1429-
*/
1430-
if (in_place_upd)
1413+
if (rd_rel->relpages!= (int32)relpages||
1414+
rd_rel->reltuples!= (float4)reltuples)
14311415
{
1432-
/*
1433-
* At bootstrap time, we don't need to worry about concurrency or
1434-
* visibility of changes, so we cheat.Also cheat if REINDEX.
1435-
*/
1436-
rd_rel= (Form_pg_class)GETSTRUCT(tuple);
1437-
LockBuffer(pg_class_scan->rs_cbuf,BUFFER_LOCK_EXCLUSIVE);
1438-
rd_rel->relpages= (int32)relpages;
1439-
rd_rel->reltuples=reltuples;
1440-
LockBuffer(pg_class_scan->rs_cbuf,BUFFER_LOCK_UNLOCK);
1441-
WriteNoReleaseBuffer(pg_class_scan->rs_cbuf);
1442-
if (!IsBootstrapProcessingMode())
1443-
CacheInvalidateHeapTuple(pg_class,tuple);
1444-
}
1445-
else
1446-
{
1447-
/* During normal processing, must work harder. */
1448-
1449-
for (i=0;i<Natts_pg_class;i++)
1416+
if (in_place_upd)
14501417
{
1451-
nulls[i]=' ';
1452-
replace[i]=' ';
1453-
values[i]= (Datum)NULL;
1418+
/*
1419+
* At bootstrap time, we don't need to worry about concurrency or
1420+
* visibility of changes, so we cheat.Also cheat if REINDEX.
1421+
*/
1422+
LockBuffer(pg_class_scan->rs_cbuf,BUFFER_LOCK_EXCLUSIVE);
1423+
rd_rel->relpages= (int32)relpages;
1424+
rd_rel->reltuples= (float4)reltuples;
1425+
LockBuffer(pg_class_scan->rs_cbuf,BUFFER_LOCK_UNLOCK);
1426+
WriteNoReleaseBuffer(pg_class_scan->rs_cbuf);
1427+
if (!IsBootstrapProcessingMode())
1428+
CacheInvalidateHeapTuple(pg_class,tuple);
1429+
}
1430+
else
1431+
{
1432+
/* During normal processing, must work harder. */
1433+
newtup=heap_copytuple(tuple);
1434+
rd_rel= (Form_pg_class)GETSTRUCT(newtup);
1435+
rd_rel->relpages= (int32)relpages;
1436+
rd_rel->reltuples= (float4)reltuples;
1437+
simple_heap_update(pg_class,&tuple->t_self,newtup);
1438+
CatalogUpdateIndexes(pg_class,newtup);
1439+
heap_freetuple(newtup);
14541440
}
1455-
1456-
replace[Anum_pg_class_relpages-1]='r';
1457-
values[Anum_pg_class_relpages-1]=Int32GetDatum((int32)relpages);
1458-
replace[Anum_pg_class_reltuples-1]='r';
1459-
values[Anum_pg_class_reltuples-1]=Float4GetDatum((float4)reltuples);
1460-
newtup=heap_modifytuple(tuple,pg_class,values,nulls,replace);
1461-
simple_heap_update(pg_class,&tuple->t_self,newtup);
1462-
CatalogUpdateIndexes(pg_class,newtup);
1463-
heap_freetuple(newtup);
14641441
}
14651442

14661443
if (!pg_class_scan)
14671444
heap_freetuple(tuple);
14681445
else
14691446
heap_endscan(pg_class_scan);
14701447

1448+
/*
1449+
* We shouldn't have to do this, but we do... Modify the reldesc in
1450+
* place with the new values so that the cache contains the latest
1451+
* copy. (XXX is this really still necessary? The relcache will get
1452+
* fixed at next CommandCounterIncrement, so why bother here?)
1453+
*/
1454+
whichRel->rd_rel->relpages= (int32)relpages;
1455+
whichRel->rd_rel->reltuples= (float4)reltuples;
1456+
14711457
heap_close(pg_class,RowExclusiveLock);
14721458
relation_close(whichRel,NoLock);
14731459
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp