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

Commitae6d536

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 parent5396b18 commitae6d536

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"
@@ -1377,22 +1378,34 @@ _bt_first(IndexScanDesc scan, ScanDirection dir)
13771378
{
13781379
/*
13791380
* We only get here if the index is completely empty. Lock relation
1380-
* because nothing finer to lock exists.
1381+
* because nothing finer to lock exists. Without a buffer lock, it's
1382+
* possible for another transaction to insert data between
1383+
* _bt_search() and PredicateLockRelation(). We have to try again
1384+
* after taking the relation-level predicate lock, to close a narrow
1385+
* window where we wouldn't scan concurrently inserted tuples, but the
1386+
* writer wouldn't see our predicate lock.
13811387
*/
1382-
PredicateLockRelation(rel,scan->xs_snapshot);
1383-
1384-
/*
1385-
* mark parallel scan as done, so that all the workers can finish
1386-
* their scan
1387-
*/
1388-
_bt_parallel_done(scan);
1389-
BTScanPosInvalidate(so->currPos);
1388+
if (IsolationIsSerializable())
1389+
{
1390+
PredicateLockRelation(rel,scan->xs_snapshot);
1391+
stack=_bt_search(rel,&inskey,&buf,BT_READ,
1392+
scan->xs_snapshot);
1393+
_bt_freestack(stack);
1394+
}
13901395

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

13971410
_bt_initialize_more_data(so,dir);
13981411

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp