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

Commitd78a66d

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 parentd42ffda commitd78a66d

File tree

5 files changed

+167
-8
lines changed

5 files changed

+167
-8
lines changed

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

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include"storage/bufmgr.h"
3434
#include"storage/freespace.h"
3535
#include"utils/builtins.h"
36+
#include"utils/datum.h"
3637
#include"utils/index_selfuncs.h"
3738
#include"utils/memutils.h"
3839
#include"utils/rel.h"
@@ -167,7 +168,7 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
167168

168169
for (;;)
169170
{
170-
boolneed_insert= false;
171+
boolneed_insert;
171172
OffsetNumberoff;
172173
BrinTuple*brtup;
173174
BrinMemTuple*dtup;
@@ -235,6 +236,9 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
235236

236237
dtup=brin_deform_tuple(bdesc,brtup,NULL);
237238

239+
/* If the range starts empty, we're certainly going to modify it. */
240+
need_insert=dtup->bt_empty_range;
241+
238242
/*
239243
* Compare the key values of the new tuple to the stored index values;
240244
* our deformed tuple will get updated if the new tuple doesn't fit
@@ -247,8 +251,20 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
247251
Datumresult;
248252
BrinValues*bval;
249253
FmgrInfo*addValue;
254+
boolhas_nulls;
250255

251256
bval=&dtup->bt_columns[keyno];
257+
258+
/*
259+
* Does the range have actual NULL values? Either of the flags can
260+
* be set, but we ignore the state before adding first row.
261+
*
262+
* We have to remember this, because we'll modify the flags and we
263+
* need to know if the range started as empty.
264+
*/
265+
has_nulls= ((!dtup->bt_empty_range)&&
266+
(bval->bv_hasnulls||bval->bv_allnulls));
267+
252268
addValue=index_getprocinfo(idxRel,keyno+1,
253269
BRIN_PROCNUM_ADDVALUE);
254270
result=FunctionCall4Coll(addValue,
@@ -259,8 +275,33 @@ brininsert(Relation idxRel, Datum *values, bool *nulls,
259275
nulls[keyno]);
260276
/* if that returned true, we need to insert the updated tuple */
261277
need_insert |=DatumGetBool(result);
278+
279+
/*
280+
* If the range was had actual NULL values (i.e. did not start empty),
281+
* make sure we don't forget about the NULL values. Either the allnulls
282+
* flag is still set to true, or (if the opclass cleared it) we need to
283+
* set hasnulls=true.
284+
*
285+
* XXX This can only happen when the opclass modified the tuple, so the
286+
* modified flag should be set.
287+
*/
288+
if (has_nulls&& !(bval->bv_hasnulls||bval->bv_allnulls))
289+
{
290+
Assert(need_insert);
291+
bval->bv_hasnulls= true;
292+
}
262293
}
263294

295+
/*
296+
* After updating summaries for all the keys, mark it as not empty.
297+
*
298+
* If we're actually changing the flag value (i.e. tuple started as
299+
* empty), we should have modified the tuple. So we should not see
300+
* empty range that was not modified.
301+
*/
302+
Assert(!dtup->bt_empty_range||need_insert);
303+
dtup->bt_empty_range= false;
304+
264305
if (!need_insert)
265306
{
266307
/*
@@ -502,6 +543,17 @@ bringetbitmap(IndexScanDesc scan, TIDBitmap *tbm)
502543
CurrentMemoryContext);
503544
}
504545

546+
/*
547+
* If the BRIN tuple indicates that this range is empty,
548+
* we can skip it: there's nothing to match. We don't
549+
* need to examine the next columns.
550+
*/
551+
if (dtup->bt_empty_range)
552+
{
553+
addrange= false;
554+
break;
555+
}
556+
505557
/*
506558
* Check whether the scan key is consistent with the page
507559
* range values; if so, have the pages in the range added
@@ -639,8 +691,24 @@ brinbuildCallback(Relation index,
639691
FmgrInfo*addValue;
640692
BrinValues*col;
641693
Form_pg_attributeattr=TupleDescAttr(state->bs_bdesc->bd_tupdesc,i);
694+
boolhas_nulls;
642695

643696
col=&state->bs_dtuple->bt_columns[i];
697+
698+
/*
699+
* Does the range have actual NULL values? Either of the flags can
700+
* be set, but we ignore the state before adding first row.
701+
*
702+
* We have to remember this, because we'll modify the flags and we
703+
* need to know if the range started as empty.
704+
*/
705+
has_nulls= ((!state->bs_dtuple->bt_empty_range)&&
706+
(col->bv_hasnulls||col->bv_allnulls));
707+
708+
/*
709+
* Call the BRIN_PROCNUM_ADDVALUE procedure. We do this even for NULL
710+
* values, because who knows what the opclass is doing.
711+
*/
644712
addValue=index_getprocinfo(index,i+1,
645713
BRIN_PROCNUM_ADDVALUE);
646714

@@ -652,7 +720,25 @@ brinbuildCallback(Relation index,
652720
PointerGetDatum(state->bs_bdesc),
653721
PointerGetDatum(col),
654722
values[i],isnull[i]);
723+
724+
/*
725+
* If the range was had actual NULL values (i.e. did not start empty),
726+
* make sure we don't forget about the NULL values. Either the allnulls
727+
* flag is still set to true, or (if the opclass cleared it) we need to
728+
* set hasnulls=true.
729+
*/
730+
if (has_nulls&& !(col->bv_hasnulls||col->bv_allnulls))
731+
col->bv_hasnulls= true;
655732
}
733+
734+
/*
735+
* After updating summaries for all the keys, mark it as not empty.
736+
*
737+
* If we're actually changing the flag value (i.e. tuple started as
738+
* empty), we should have modified the tuple. So we should not see
739+
* empty range that was not modified.
740+
*/
741+
state->bs_dtuple->bt_empty_range= false;
656742
}
657743

658744
/*
@@ -1473,6 +1559,64 @@ union_tuples(BrinDesc *bdesc, BrinMemTuple *a, BrinTuple *b)
14731559
db=brin_deform_tuple(bdesc,b,NULL);
14741560
MemoryContextSwitchTo(oldcxt);
14751561

1562+
/*
1563+
* Check if the ranges are empty.
1564+
*
1565+
* If at least one of them is empty, we don't need to call per-key union
1566+
* functions at all. If "b" is empty, we just use "a" as the result (it
1567+
* might be empty fine, but that's fine). If "a" is empty but "b" is not,
1568+
* we use "b" as the result (but we have to copy the data into "a" first).
1569+
*
1570+
* Only when both ranges are non-empty, we actually do the per-key merge.
1571+
*/
1572+
1573+
/* If "b" is empty - ignore it and just use "a" (even if it's empty etc.). */
1574+
if (db->bt_empty_range)
1575+
{
1576+
/* skip the per-key merge */
1577+
MemoryContextDelete(cxt);
1578+
return;
1579+
}
1580+
1581+
/*
1582+
* Now we know "b" is not empty. If "a" is empty, then "b" is the result.
1583+
* But we need to copy the data from "b" to "a" first, because that's how
1584+
* we pass result out.
1585+
*
1586+
* We have to copy all the global/per-key flags etc. too.
1587+
*/
1588+
if (a->bt_empty_range)
1589+
{
1590+
for (keyno=0;keyno<bdesc->bd_tupdesc->natts;keyno++)
1591+
{
1592+
inti;
1593+
BrinValues*col_a=&a->bt_columns[keyno];
1594+
BrinValues*col_b=&db->bt_columns[keyno];
1595+
BrinOpcInfo*opcinfo=bdesc->bd_info[keyno];
1596+
1597+
col_a->bv_allnulls=col_b->bv_allnulls;
1598+
col_a->bv_hasnulls=col_b->bv_hasnulls;
1599+
1600+
/* If "b" has no data, we're done. */
1601+
if (col_b->bv_allnulls)
1602+
continue;
1603+
1604+
for (i=0;i<opcinfo->oi_nstored;i++)
1605+
col_a->bv_values[i]=
1606+
datumCopy(col_b->bv_values[i],
1607+
opcinfo->oi_typcache[i]->typbyval,
1608+
opcinfo->oi_typcache[i]->typlen);
1609+
}
1610+
1611+
/* "a" started empty, but "b" was not empty, so remember that */
1612+
a->bt_empty_range= false;
1613+
1614+
/* skip the per-key merge */
1615+
MemoryContextDelete(cxt);
1616+
return;
1617+
}
1618+
1619+
/* Neither range is empty, so call the union proc. */
14761620
for (keyno=0;keyno<bdesc->bd_tupdesc->natts;keyno++)
14771621
{
14781622
FmgrInfo*unionFn;

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

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

342+
if (tuple->bt_empty_range)
343+
rettuple->bt_info |=BRIN_EMPTY_RANGE_MASK;
344+
342345
*size=len;
343346
returnrettuple;
344347
}
@@ -366,7 +369,7 @@ brin_form_placeholder_tuple(BrinDesc *brdesc, BlockNumber blkno, Size *size)
366369
rettuple=palloc0(len);
367370
rettuple->bt_blkno=blkno;
368371
rettuple->bt_info=hoff;
369-
rettuple->bt_info |=BRIN_NULLS_MASK |BRIN_PLACEHOLDER_MASK;
372+
rettuple->bt_info |=BRIN_NULLS_MASK |BRIN_PLACEHOLDER_MASK |BRIN_EMPTY_RANGE_MASK;
370373

371374
bitP= ((bits8*) ((char*)rettuple+SizeOfBrinTuple))-1;
372375
bitmask=HIGHBIT;
@@ -456,6 +459,8 @@ brin_new_memtuple(BrinDesc *brdesc)
456459
dtup->bt_allnulls=palloc(sizeof(bool)*brdesc->bd_tupdesc->natts);
457460
dtup->bt_hasnulls=palloc(sizeof(bool)*brdesc->bd_tupdesc->natts);
458461

462+
dtup->bt_empty_range= true;
463+
459464
dtup->bt_context=AllocSetContextCreate(CurrentMemoryContext,
460465
"brin dtuple",
461466
ALLOCSET_DEFAULT_SIZES);
@@ -489,6 +494,8 @@ brin_memtuple_initialize(BrinMemTuple *dtuple, BrinDesc *brdesc)
489494
currdatum+=sizeof(Datum)*brdesc->bd_info[i]->oi_nstored;
490495
}
491496

497+
dtuple->bt_empty_range= true;
498+
492499
returndtuple;
493500
}
494501

@@ -522,6 +529,11 @@ brin_deform_tuple(BrinDesc *brdesc, BrinTuple *tuple, BrinMemTuple *dMemtuple)
522529

523530
if (BrinTupleIsPlaceholder(tuple))
524531
dtup->bt_placeholder= true;
532+
533+
/* ranges start as empty, depends on the BrinTuple */
534+
if (!BrinTupleIsEmptyRange(tuple))
535+
dtup->bt_empty_range= false;
536+
525537
dtup->bt_blkno=tuple->bt_blkno;
526538

527539
values=dtup->bt_values;

‎src/include/access/brin_tuple.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct BrinValues
3636
typedefstructBrinMemTuple
3737
{
3838
boolbt_placeholder;/* this is a placeholder tuple */
39+
boolbt_empty_range;/* range represents no tuples */
3940
BlockNumberbt_blkno;/* heap blkno that the tuple is for */
4041
MemoryContextbt_context;/* memcxt holding the bt_columns values */
4142
/* output arrays for brin_deform_tuple: */
@@ -61,7 +62,7 @@ typedef struct BrinTuple
6162
*
6263
* 7th (high) bit: has nulls
6364
* 6th bit: is placeholder tuple
64-
* 5th bit:unused
65+
* 5th bit:range is empty
6566
* 4-0 bit: offset of data
6667
* ---------------
6768
*/
@@ -74,13 +75,14 @@ typedef struct BrinTuple
7475
* bt_info manipulation macros
7576
*/
7677
#defineBRIN_OFFSET_MASK0x1F
77-
/* bit0x20 is not used at present */
78+
#defineBRIN_EMPTY_RANGE_MASK0x20
7879
#defineBRIN_PLACEHOLDER_MASK0x40
7980
#defineBRIN_NULLS_MASK0x80
8081

8182
#defineBrinTupleDataOffset(tup)((Size) (((BrinTuple *) (tup))->bt_info & BRIN_OFFSET_MASK))
8283
#defineBrinTupleHasNulls(tup)(((((BrinTuple *) (tup))->bt_info & BRIN_NULLS_MASK)) != 0)
8384
#defineBrinTupleIsPlaceholder(tup) (((((BrinTuple *) (tup))->bt_info & BRIN_PLACEHOLDER_MASK)) != 0)
85+
#defineBrinTupleIsEmptyRange(tup) (((((BrinTuple *) (tup))->bt_info & BRIN_EMPTY_RANGE_MASK)) != 0)
8486

8587

8688
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