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

Commit3581cbd

Browse files
committed
Fix handling of empty ranges and NULLs in BRIN
BRIN indexes did not properly distinguish between summaries for empty(no rows) and all-NULL ranges, treating them as essentially the samething. Summaries were initialized with allnulls=true, and opclassessimply reset allnulls to false when processing the first non-NULL value.This however produces incorrect results if the range starts with a NULLvalue (or a sequence of NULL values), in which case we forget the rangecontains NULL values when adding the first non-NULL value.This happens because the allnulls flag is used for two separatepurposes - to mark empty ranges (not representing any rows yet) andranges containing only NULL values.Opclasses don't know which of these cases it is, and so don't knowwhether to set hasnulls=true. Setting the flag in both cases would makeit correct, but it would also make BRIN indexes useless for queries withIS NULL clauses. All ranges start empty (and thus allnulls=true), so allranges would end up with either allnulls=true or hasnulls=true.The severity of the issue is somewhat reduced by the fact that it onlyhappens when adding values to an existing summary with allnulls=true.This can happen e.g. for small tables (because a summary for the firstrange exists for all BRIN indexes), or for tables with large fraction ofNULL values in the indexed columns.Bulk summarization (e.g. during CREATE INDEX or automatic summarization)that processes all values at once is not affected by this issue. In thiscase the flags were updated in a slightly different way, not forgettingthe NULL values.To identify empty ranges we use a new flag, stored in an unused bit inthe BRIN tuple header so the on-disk format remains the same. A matchingflag is added to BrinMemTuple, into a 3B gap after bt_placeholder.That means there's no risk of ABI breakage, although we don't actuallypass the BrinMemTuple to any public API.We could also skip storing index tuples for empty summaries, but thenwe'd have to always process such ranges - even if there are no rows inlarge parts of the table (e.g. after a bulk DELETE), it would stillrequire reading the pages etc. So we store them, but ignore them whenbuilding the bitmap.Backpatch to 11. The issue exists since BRIN indexes were introduced in9.5, but older releases are already EOL.Backpatch-through: 11Reviewed-by: Justin Pryzby, Matthias van de Meent, Alvaro HerreraDiscussion:https://postgr.es/m/402430e4-7d9d-6cf1-09ef-464d80afff3b@enterprisedb.com
1 parent1158c8c commit3581cbd

File tree

5 files changed

+134
-8
lines changed

5 files changed

+134
-8
lines changed

‎src/backend/access/brin/brin.c

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,17 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
592592

593593
bval=&dtup->bt_columns[attno-1];
594594

595+
/*
596+
* If the BRIN tuple indicates that this range is empty,
597+
* we can skip it: there's nothing to match. We don't
598+
* need to examine the next columns.
599+
*/
600+
if (dtup->bt_empty_range)
601+
{
602+
addrange= false;
603+
break;
604+
}
605+
595606
/*
596607
* First check if there are any IS [NOT] NULL scan keys,
597608
* and if we're violating them. In that case we can
@@ -1606,6 +1617,64 @@ union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
16061617
db=brin_deform_tuple(bdesc,b,NULL);
16071618
MemoryContextSwitchTo(oldcxt);
16081619

1620+
/*
1621+
* Check if the ranges are empty.
1622+
*
1623+
* If at least one of them is empty, we don't need to call per-key union
1624+
* functions at all. If "b" is empty, we just use "a" as the result (it
1625+
* might be empty fine, but that's fine). If "a" is empty but "b" is not,
1626+
* we use "b" as the result (but we have to copy the data into "a" first).
1627+
*
1628+
* Only when both ranges are non-empty, we actually do the per-key merge.
1629+
*/
1630+
1631+
/* If "b" is empty - ignore it and just use "a" (even if it's empty etc.). */
1632+
if (db->bt_empty_range)
1633+
{
1634+
/* skip the per-key merge */
1635+
MemoryContextDelete(cxt);
1636+
return;
1637+
}
1638+
1639+
/*
1640+
* Now we know "b" is not empty. If "a" is empty, then "b" is the result.
1641+
* But we need to copy the data from "b" to "a" first, because that's how
1642+
* we pass result out.
1643+
*
1644+
* We have to copy all the global/per-key flags etc. too.
1645+
*/
1646+
if (a->bt_empty_range)
1647+
{
1648+
for (keyno=0;keyno<bdesc->bd_tupdesc->natts;keyno++)
1649+
{
1650+
inti;
1651+
BrinValues*col_a=&a->bt_columns[keyno];
1652+
BrinValues*col_b=&db->bt_columns[keyno];
1653+
BrinOpcInfo*opcinfo=bdesc->bd_info[keyno];
1654+
1655+
col_a->bv_allnulls=col_b->bv_allnulls;
1656+
col_a->bv_hasnulls=col_b->bv_hasnulls;
1657+
1658+
/* If "b" has no data, we're done. */
1659+
if (col_b->bv_allnulls)
1660+
continue;
1661+
1662+
for (i=0;i<opcinfo->oi_nstored;i++)
1663+
col_a->bv_values[i]=
1664+
datumCopy(col_b->bv_values[i],
1665+
opcinfo->oi_typcache[i]->typbyval,
1666+
opcinfo->oi_typcache[i]->typlen);
1667+
}
1668+
1669+
/* "a" started empty, but "b" was not empty, so remember that */
1670+
a->bt_empty_range= false;
1671+
1672+
/* skip the per-key merge */
1673+
MemoryContextDelete(cxt);
1674+
return;
1675+
}
1676+
1677+
/* Now we know neither range is empty. */
16091678
for (keyno=0;keyno<bdesc->bd_tupdesc->natts;keyno++)
16101679
{
16111680
FmgrInfo*unionFn;
@@ -1711,7 +1780,9 @@ add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup,
17111780
Datum*values,bool*nulls)
17121781
{
17131782
intkeyno;
1714-
boolmodified= false;
1783+
1784+
/* If the range starts empty, we're certainly going to modify it. */
1785+
boolmodified=dtup->bt_empty_range;
17151786

17161787
/*
17171788
* Compare the key values of the new tuple to the stored index values; our
@@ -1725,9 +1796,24 @@ add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup,
17251796
Datumresult;
17261797
BrinValues*bval;
17271798
FmgrInfo*addValue;
1799+
boolhas_nulls;
17281800

17291801
bval=&dtup->bt_columns[keyno];
17301802

1803+
/*
1804+
* Does the range have actual NULL values? Either of the flags can
1805+
* be set, but we ignore the state before adding first row.
1806+
*
1807+
* We have to remember this, because we'll modify the flags and we
1808+
* need to know if the range started as empty.
1809+
*/
1810+
has_nulls= ((!dtup->bt_empty_range)&&
1811+
(bval->bv_hasnulls||bval->bv_allnulls));
1812+
1813+
/*
1814+
* If the value we're adding is NULL, handle it locally. Otherwise
1815+
* call the BRIN_PROCNUM_ADDVALUE procedure.
1816+
*/
17311817
if (bdesc->bd_info[keyno]->oi_regular_nulls&&nulls[keyno])
17321818
{
17331819
/*
@@ -1753,8 +1839,33 @@ add_values_to_range(Relation idxRel, BrinDesc *bdesc, BrinMemTuple *dtup,
17531839
nulls[keyno]);
17541840
/* if that returned true, we need to insert the updated tuple */
17551841
modified |=DatumGetBool(result);
1842+
1843+
/*
1844+
* If the range was had actual NULL values (i.e. did not start empty),
1845+
* make sure we don't forget about the NULL values. Either the allnulls
1846+
* flag is still set to true, or (if the opclass cleared it) we need to
1847+
* set hasnulls=true.
1848+
*
1849+
* XXX This can only happen when the opclass modified the tuple, so the
1850+
* modified flag should be set.
1851+
*/
1852+
if (has_nulls&& !(bval->bv_hasnulls||bval->bv_allnulls))
1853+
{
1854+
Assert(modified);
1855+
bval->bv_hasnulls= true;
1856+
}
17561857
}
17571858

1859+
/*
1860+
* After updating summaries for all the keys, mark it as not empty.
1861+
*
1862+
* If we're actually changing the flag value (i.e. tuple started as empty),
1863+
* we should have modified the tuple. So we should not see empty range that
1864+
* was not modified.
1865+
*/
1866+
Assert(!dtup->bt_empty_range||modified);
1867+
dtup->bt_empty_range= false;
1868+
17581869
returnmodified;
17591870
}
17601871

‎src/backend/access/brin/brin_tuple.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
372372
if (tuple->bt_placeholder)
373373
rettuple->bt_info |=BRIN_PLACEHOLDER_MASK;
374374

375+
if (tuple->bt_empty_range)
376+
rettuple->bt_info |=BRIN_EMPTY_RANGE_MASK;
377+
375378
*size=len;
376379
returnrettuple;
377380
}
@@ -399,7 +402,7 @@ brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
399402
rettuple=palloc0(len);
400403
rettuple->bt_blkno=blkno;
401404
rettuple->bt_info=hoff;
402-
rettuple->bt_info |=BRIN_NULLS_MASK |BRIN_PLACEHOLDER_MASK;
405+
rettuple->bt_info |=BRIN_NULLS_MASK |BRIN_PLACEHOLDER_MASK |BRIN_EMPTY_RANGE_MASK;
403406

404407
bitP= ((bits8*) ((char*)rettuple+SizeOfBrinTuple))-1;
405408
bitmask=HIGHBIT;
@@ -489,6 +492,8 @@ brin_new_memtuple(BrinDesc *brdesc)
489492
dtup->bt_allnulls=palloc(sizeof(bool)*brdesc->bd_tupdesc->natts);
490493
dtup->bt_hasnulls=palloc(sizeof(bool)*brdesc->bd_tupdesc->natts);
491494

495+
dtup->bt_empty_range= true;
496+
492497
dtup->bt_context=AllocSetContextCreate(CurrentMemoryContext,
493498
"brin dtuple",
494499
ALLOCSET_DEFAULT_SIZES);
@@ -527,6 +532,8 @@ brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
527532
currdatum+=sizeof(Datum)*brdesc->bd_info[i]->oi_nstored;
528533
}
529534

535+
dtuple->bt_empty_range= true;
536+
530537
returndtuple;
531538
}
532539

@@ -560,6 +567,11 @@ brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
560567

561568
if (BrinTupleIsPlaceholder(tuple))
562569
dtup->bt_placeholder= true;
570+
571+
/* ranges start as empty, depends on the BrinTuple */
572+
if (!BrinTupleIsEmptyRange(tuple))
573+
dtup->bt_empty_range= false;
574+
563575
dtup->bt_blkno=tuple->bt_blkno;
564576

565577
values=dtup->bt_values;

‎src/include/access/brin_tuple.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ typedef struct BrinValues
4444
typedefstructBrinMemTuple
4545
{
4646
boolbt_placeholder;/* this is a placeholder tuple */
47+
boolbt_empty_range;/* range represents no tuples */
4748
BlockNumberbt_blkno;/* heap blkno that the tuple is for */
4849
MemoryContextbt_context;/* memcxt holding the bt_columns values */
4950
/* output arrays for brin_deform_tuple: */
@@ -69,7 +70,7 @@ typedef struct BrinTuple
6970
*
7071
* 7th (high) bit: has nulls
7172
* 6th bit: is placeholder tuple
72-
* 5th bit:unused
73+
* 5th bit:range is empty
7374
* 4-0 bit: offset of data
7475
* ---------------
7576
*/
@@ -82,13 +83,14 @@ typedef struct BrinTuple
8283
* bt_info manipulation macros
8384
*/
8485
#defineBRIN_OFFSET_MASK0x1F
85-
/* bit0x20 is not used at present */
86+
#defineBRIN_EMPTY_RANGE_MASK0x20
8687
#defineBRIN_PLACEHOLDER_MASK0x40
8788
#defineBRIN_NULLS_MASK0x80
8889

8990
#defineBrinTupleDataOffset(tup)((Size) (((BrinTuple *) (tup))->bt_info & BRIN_OFFSET_MASK))
9091
#defineBrinTupleHasNulls(tup)(((((BrinTuple *) (tup))->bt_info & BRIN_NULLS_MASK)) != 0)
9192
#defineBrinTupleIsPlaceholder(tup) (((((BrinTuple *) (tup))->bt_info & BRIN_PLACEHOLDER_MASK)) != 0)
93+
#defineBrinTupleIsEmptyRange(tup) (((((BrinTuple *) (tup))->bt_info & BRIN_EMPTY_RANGE_MASK)) != 0)
9294

9395

9496
externBrinTuple*brin_form_tuple(BrinDesc*brdesc,BlockNumberblkno,

‎src/test/modules/brin/expected/summarization-and-inprogress-insertion.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ starting permutation: s2check s1b s2b s1i s2summ s1c s2c s2check
44
step s2check: SELECT * FROM brin_page_items(get_raw_page('brinidx', 2), 'brinidx'::regclass);
55
itemoffset|blknum|attnum|allnulls|hasnulls|placeholder|value
66
----------+------+------+--------+--------+-----------+--------
7-
1| 0| 1|f |f |f |{1 .. 1}
7+
1| 0| 1|f |t |f |{1 .. 1}
88
(1 row)
99

1010
step s1b: BEGIN ISOLATION LEVEL REPEATABLE READ;
@@ -26,7 +26,7 @@ step s2c: COMMIT;
2626
step s2check: SELECT * FROM brin_page_items(get_raw_page('brinidx', 2), 'brinidx'::regclass);
2727
itemoffset|blknum|attnum|allnulls|hasnulls|placeholder|value
2828
----------+------+------+--------+--------+-----------+-----------
29-
1| 0| 1|f |f |f |{1 .. 1}
29+
1| 0| 1|f |t |f |{1 .. 1}
3030
2| 1| 1|f |f |f |{1 .. 1000}
3131
(2 rows)
3232

@@ -35,7 +35,7 @@ starting permutation: s2check s1b s1i s2vacuum s1c s2check
3535
step s2check: SELECT * FROM brin_page_items(get_raw_page('brinidx', 2), 'brinidx'::regclass);
3636
itemoffset|blknum|attnum|allnulls|hasnulls|placeholder|value
3737
----------+------+------+--------+--------+-----------+--------
38-
1| 0| 1|f |f |f |{1 .. 1}
38+
1| 0| 1|f |t |f |{1 .. 1}
3939
(1 row)
4040

4141
step s1b: BEGIN ISOLATION LEVEL REPEATABLE READ;
@@ -45,7 +45,7 @@ step s1c: COMMIT;
4545
step s2check: SELECT * FROM brin_page_items(get_raw_page('brinidx', 2), 'brinidx'::regclass);
4646
itemoffset|blknum|attnum|allnulls|hasnulls|placeholder|value
4747
----------+------+------+--------+--------+-----------+-----------
48-
1| 0| 1|f |f |f |{1 .. 1}
48+
1| 0| 1|f |t |f |{1 .. 1}
4949
2| 1| 1|f |f |f |{1 .. 1000}
5050
(2 rows)
5151

‎src/test/modules/brin/specs/summarization-and-inprogress-insertion.spec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ setup
99
)WITH(fillfactor=10);
1010
CREATEINDEXbrinidxONbrin_isoUSINGbrin(value)WITH(pages_per_range=1);
1111
--thisfillsthefirstpage
12+
INSERTINTObrin_isoVALUES(NULL);
1213
DO $$
1314
DECLAREcurtidtid;
1415
BEGIN

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp