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

Commit8f705d7

Browse files
committed
Fix race in SSI interaction with empty btrees.
When predicate-locking btrees, we have a special case for completelyempty btrees, since there is no page to lock. This was racy, because,without buffer lock held, a matching key could be inserted between the_bt_search() and the PredicateLockRelation() calls.Fix, by rechecking _bt_search() after taking the relation-level SIREADlock, if using SERIALIZABLE isolation and an empty btree is discovered.Back-patch to all supported releases. Fixes one aspect of bug #17949.Reported-by: Artem Anisimov <artem.anisimov.255@gmail.com>Reviewed-by: Dmitry Dolgov <9erthalion6@gmail.com>Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>Discussion:https://postgr.es/m/17949-a0f17035294a55e2%40postgresql.org
1 parent753f20c commit8f705d7

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

‎src/backend/access/nbtree/nbtsearch.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include"access/nbtree.h"
1919
#include"access/relscan.h"
20+
#include"access/xact.h"
2021
#include"miscadmin.h"
2122
#include"pgstat.h"
2223
#include"storage/predicate.h"
@@ -1381,22 +1382,34 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
13811382
{
13821383
/*
13831384
* We only get here if the index is completely empty. Lock relation
1384-
* because nothing finer to lock exists.
1385+
* because nothing finer to lock exists. Without a buffer lock, it's
1386+
* possible for another transaction to insert data between
1387+
* _bt_search() and PredicateLockRelation(). We have to try again
1388+
* after taking the relation-level predicate lock, to close a narrow
1389+
* window where we wouldn't scan concurrently inserted tuples, but the
1390+
* writer wouldn't see our predicate lock.
13851391
*/
1386-
PredicateLockRelation(rel,scan->xs_snapshot);
1387-
1388-
/*
1389-
* mark parallel scan as done, so that all the workers can finish
1390-
* their scan
1391-
*/
1392-
_bt_parallel_done(scan);
1393-
BTScanPosInvalidate(so->currPos);
1392+
if (IsolationIsSerializable())
1393+
{
1394+
PredicateLockRelation(rel,scan->xs_snapshot);
1395+
stack=_bt_search(rel,&inskey,&buf,BT_READ,
1396+
scan->xs_snapshot);
1397+
_bt_freestack(stack);
1398+
}
13941399

1395-
return false;
1400+
if (!BufferIsValid(buf))
1401+
{
1402+
/*
1403+
* Mark parallel scan as done, so that all the workers can finish
1404+
* their scan.
1405+
*/
1406+
_bt_parallel_done(scan);
1407+
BTScanPosInvalidate(so->currPos);
1408+
return false;
1409+
}
13961410
}
1397-
else
1398-
PredicateLockPage(rel,BufferGetBlockNumber(buf),
1399-
scan->xs_snapshot);
1411+
1412+
PredicateLockPage(rel,BufferGetBlockNumber(buf),scan->xs_snapshot);
14001413

14011414
_bt_initialize_more_data(so,dir);
14021415

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp