PostgreSQL Source Code git master
brin_minmax_multi.c File Reference
#include "postgres.h"
#include <sys/socket.h>
#include "access/brin.h"
#include "access/brin_internal.h"
#include "access/brin_tuple.h"
#include "access/genam.h"
#include "access/htup_details.h"
#include "access/reloptions.h"
#include "access/stratnum.h"
#include "catalog/pg_am.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_type.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/datum.h"
#include "utils/float.h"
#include "utils/inet.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/timestamp.h"
#include "utils/uuid.h"
Include dependency graph for brin_minmax_multi.c:

Go to the source code of this file.

Data Structures

struct  MinmaxMultiOpaque
 
struct  MinMaxMultiOptions
 
struct  Ranges
 
struct  SerializedRanges
 
struct  ExpandedRange
 
struct  DistanceValue
 
struct  compare_context
 

Macros

#define MINMAX_MAX_PROCNUMS   1 /* maximum support procs we need */
 
#define PROCNUM_DISTANCE   11 /* required, distance between values */
 
#define PROCNUM_BASE   11
 
#define MINMAX_BUFFER_FACTOR   10
 
#define MINMAX_BUFFER_MIN   256
 
#define MINMAX_BUFFER_MAX   8192
 
#define MINMAX_BUFFER_LOAD_FACTOR   0.5
 
#define MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE   32
 
#define MinMaxMultiGetValuesPerRange(opts)
 
#define SAMESIGN(a, b)   (((a) < 0) == ((b) < 0))
 

Typedefs

typedef struct MinmaxMultiOpaque MinmaxMultiOpaque
 
typedef struct MinMaxMultiOptions MinMaxMultiOptions
 
typedef struct Ranges Ranges
 
typedef struct SerializedRanges SerializedRanges
 
typedef struct ExpandedRange ExpandedRange
 
typedef struct DistanceValue DistanceValue
 
typedef struct compare_context compare_context
 

Functions

static SerializedRangesbrin_range_serialize (Ranges *range)
 
static Rangesbrin_range_deserialize (int maxvalues, SerializedRanges *serialized)
 
static FmgrInfominmax_multi_get_procinfo (BrinDesc *bdesc, uint16 attno, uint16 procnum)
 
static FmgrInfominmax_multi_get_strategy_procinfo (BrinDesc *bdesc, uint16 attno, Oid subtype, uint16 strategynum)
 
static int compare_values (const void *a, const void *b, void *arg)
 
static void AssertCheckRanges (Ranges *ranges, FmgrInfo *cmpFn, Oid colloid)
 
static void AssertCheckExpandedRanges (BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, ExpandedRange *ranges, int nranges)
 
static Rangesminmax_multi_init (int maxvalues)
 
static void range_deduplicate_values (Ranges *range)
 
static int compare_expanded_ranges (const void *a, const void *b, void *arg)
 
static bool has_matching_range (BrinDesc *bdesc, Oid colloid, Ranges *ranges, Datum newval, AttrNumber attno, Oid typid)
 
static bool range_contains_value (BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval, bool full)
 
static void fill_expanded_ranges (ExpandedRange *eranges, int neranges, Ranges *ranges)
 
static int sort_expanded_ranges (FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
 
static int merge_overlapping_ranges (FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
 
static int compare_distances (const void *a, const void *b)
 
static DistanceValuebuild_distances (FmgrInfo *distanceFn, Oid colloid, ExpandedRange *eranges, int neranges)
 
static ExpandedRangebuild_expanded_ranges (FmgrInfo *cmp, Oid colloid, Ranges *ranges, int *nranges)
 
static int reduce_expanded_ranges (ExpandedRange *eranges, int neranges, DistanceValue *distances, int max_values, FmgrInfo *cmp, Oid colloid)
 
static void store_expanded_ranges (Ranges *ranges, ExpandedRange *eranges, int neranges)
 
static bool ensure_free_space_in_buffer (BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *range)
 
static bool range_add_value (BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval)
 
static void compactify_ranges (BrinDesc *bdesc, Ranges *ranges, int max_values)
 
Datum brin_minmax_multi_opcinfo (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_float4 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_float8 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_int2 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_int4 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_int8 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_tid (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_numeric (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_uuid (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_date (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_time (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_timetz (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_timestamp (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_interval (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_pg_lsn (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_macaddr (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_macaddr8 (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_distance_inet (PG_FUNCTION_ARGS)
 
static void brin_minmax_multi_serialize (BrinDesc *bdesc, Datum src, Datum *dst)
 
static int brin_minmax_multi_get_values (BrinDesc *bdesc, MinMaxMultiOptions *opts)
 
Datum brin_minmax_multi_add_value (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_consistent (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_union (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_options (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_summary_in (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_summary_out (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_summary_recv (PG_FUNCTION_ARGS)
 
Datum brin_minmax_multi_summary_send (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ MINMAX_BUFFER_FACTOR

#define MINMAX_BUFFER_FACTOR   10

Definition at line 106 of file brin_minmax_multi.c.

◆ MINMAX_BUFFER_LOAD_FACTOR

#define MINMAX_BUFFER_LOAD_FACTOR   0.5

Definition at line 109 of file brin_minmax_multi.c.

◆ MINMAX_BUFFER_MAX

#define MINMAX_BUFFER_MAX   8192

Definition at line 108 of file brin_minmax_multi.c.

◆ MINMAX_BUFFER_MIN

#define MINMAX_BUFFER_MIN   256

Definition at line 107 of file brin_minmax_multi.c.

◆ MINMAX_MAX_PROCNUMS

#define MINMAX_MAX_PROCNUMS   1 /* maximum support procs we need */

Definition at line 92 of file brin_minmax_multi.c.

◆ MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE

#define MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE   32

Definition at line 128 of file brin_minmax_multi.c.

◆ MinMaxMultiGetValuesPerRange

#define MinMaxMultiGetValuesPerRange (   opts)
Value:
((opts) && (((MinMaxMultiOptions *) (opts))->valuesPerRange != 0) ? \
((MinMaxMultiOptions *) (opts))->valuesPerRange : \
MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE)
static AmcheckOptions opts
Definition: pg_amcheck.c:112

Definition at line 130 of file brin_minmax_multi.c.

◆ PROCNUM_BASE

#define PROCNUM_BASE   11

Definition at line 99 of file brin_minmax_multi.c.

◆ PROCNUM_DISTANCE

#define PROCNUM_DISTANCE   11 /* required, distance between values */

Definition at line 93 of file brin_minmax_multi.c.

◆ SAMESIGN

#define SAMESIGN (   a,
  b 
)    (((a) < 0) == ((b) < 0))

Definition at line 135 of file brin_minmax_multi.c.

Typedef Documentation

◆ compare_context

◆ DistanceValue

typedef struct DistanceValue DistanceValue

◆ ExpandedRange

typedef struct ExpandedRange ExpandedRange

◆ MinmaxMultiOpaque

◆ MinMaxMultiOptions

◆ Ranges

typedef struct Ranges Ranges

◆ SerializedRanges

Function Documentation

◆ AssertCheckExpandedRanges()

static void AssertCheckExpandedRanges ( BrinDesc bdesc,
Oid  colloid,
AttrNumber  attno,
Form_pg_attribute  attr,
ExpandedRange ranges,
int  nranges 
)
static

Definition at line 427 of file brin_minmax_multi.c.

430{
431#ifdef USE_ASSERT_CHECKING
432 int i;
433 FmgrInfo *eq;
434 FmgrInfo *lt;
435
436 eq = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
438
439 lt = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
441
442 /*
443 * Each range independently should be valid, i.e. that for the boundary
444 * values (lower <= upper).
445 */
446 for (i = 0; i < nranges; i++)
447 {
448 Datum r;
449 Datum minval = ranges[i].minval;
450 Datum maxval = ranges[i].maxval;
451
452 if (ranges[i].collapsed) /* collapsed: minval == maxval */
453 r = FunctionCall2Coll(eq, colloid, minval, maxval);
454 else /* non-collapsed: minval < maxval */
455 r = FunctionCall2Coll(lt, colloid, minval, maxval);
456
458 }
459
460 /*
461 * And the ranges should be ordered and must not overlap, i.e. upper <
462 * lower for boundaries of consecutive ranges.
463 */
464 for (i = 0; i < nranges - 1; i++)
465 {
466 Datum r;
467 Datum maxval = ranges[i].maxval;
468 Datum minval = ranges[i + 1].minval;
469
470 r = FunctionCall2Coll(lt, colloid, maxval, minval);
471
473 }
474#endif
475}
static FmgrInfo * minmax_multi_get_strategy_procinfo(BrinDesc *bdesc, uint16 attno, Oid subtype, uint16 strategynum)
#define Assert(condition)
Definition: c.h:815
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1149
int i
Definition: isn.c:72
static bool DatumGetBool(Datum X)
Definition: postgres.h:95
uintptr_t Datum
Definition: postgres.h:69
#define BTLessStrategyNumber
Definition: stratnum.h:29
#define BTEqualStrategyNumber
Definition: stratnum.h:31
Definition: fmgr.h:57

References Assert, BTEqualStrategyNumber, BTLessStrategyNumber, DatumGetBool(), FunctionCall2Coll(), i, ExpandedRange::maxval, minmax_multi_get_strategy_procinfo(), and ExpandedRange::minval.

Referenced by brin_minmax_multi_union(), and ensure_free_space_in_buffer().

◆ AssertCheckRanges()

static void AssertCheckRanges ( Ranges ranges,
FmgrInfo cmpFn,
Oid  colloid 
)
static

Definition at line 297 of file brin_minmax_multi.c.

298{
299#ifdef USE_ASSERT_CHECKING
300 int i;
301
302 /* some basic sanity checks */
303 Assert(ranges->nranges >= 0);
304 Assert(ranges->nsorted >= 0);
305 Assert(ranges->nvalues >= ranges->nsorted);
306 Assert(ranges->maxvalues >= 2 * ranges->nranges + ranges->nvalues);
307 Assert(ranges->typid != InvalidOid);
308
309 /*
310 * First the ranges - there are 2*nranges boundary values, and the values
311 * have to be strictly ordered (equal values would mean the range is
312 * collapsed, and should be stored as a point). This also guarantees that
313 * the ranges do not overlap.
314 */
315 AssertArrayOrder(cmpFn, colloid, ranges->values, 2 * ranges->nranges);
316
317 /* then the single-point ranges (with nvalues boundary values ) */
318 AssertArrayOrder(cmpFn, colloid, &ranges->values[2 * ranges->nranges],
319 ranges->nsorted);
320
321 /*
322 * Check that none of the values are not covered by ranges (both sorted
323 * and unsorted)
324 */
325 if (ranges->nranges > 0)
326 {
327 for (i = 0; i < ranges->nvalues; i++)
328 {
329 Datum compar;
330 int start,
331 end;
332 Datum minvalue = ranges->values[0];
333 Datum maxvalue = ranges->values[2 * ranges->nranges - 1];
334 Datum value = ranges->values[2 * ranges->nranges + i];
335
336 compar = FunctionCall2Coll(cmpFn, colloid, value, minvalue);
337
338 /*
339 * If the value is smaller than the lower bound in the first range
340 * then it cannot possibly be in any of the ranges.
341 */
342 if (DatumGetBool(compar))
343 continue;
344
345 compar = FunctionCall2Coll(cmpFn, colloid, maxvalue, value);
346
347 /*
348 * Likewise, if the value is larger than the upper bound of the
349 * final range, then it cannot possibly be inside any of the
350 * ranges.
351 */
352 if (DatumGetBool(compar))
353 continue;
354
355 /* bsearch the ranges to see if 'value' fits within any of them */
356 start = 0; /* first range */
357 end = ranges->nranges - 1; /* last range */
358 while (true)
359 {
360 int midpoint = (start + end) / 2;
361
362 /* this means we ran out of ranges in the last step */
363 if (start > end)
364 break;
365
366 /* copy the min/max values from the ranges */
367 minvalue = ranges->values[2 * midpoint];
368 maxvalue = ranges->values[2 * midpoint + 1];
369
370 /*
371 * Is the value smaller than the minval? If yes, we'll recurse
372 * to the left side of range array.
373 */
374 compar = FunctionCall2Coll(cmpFn, colloid, value, minvalue);
375
376 /* smaller than the smallest value in this range */
377 if (DatumGetBool(compar))
378 {
379 end = (midpoint - 1);
380 continue;
381 }
382
383 /*
384 * Is the value greater than the minval? If yes, we'll recurse
385 * to the right side of range array.
386 */
387 compar = FunctionCall2Coll(cmpFn, colloid, maxvalue, value);
388
389 /* larger than the largest value in this range */
390 if (DatumGetBool(compar))
391 {
392 start = (midpoint + 1);
393 continue;
394 }
395
396 /* hey, we found a matching range */
397 Assert(false);
398 }
399 }
400 }
401
402 /* and values in the unsorted part must not be in the sorted part */
403 if (ranges->nsorted > 0)
404 {
405 compare_context cxt;
406
407 cxt.colloid = ranges->colloid;
408 cxt.cmpFn = ranges->cmp;
409
410 for (i = ranges->nsorted; i < ranges->nvalues; i++)
411 {
412 Datum value = ranges->values[2 * ranges->nranges + i];
413
414 Assert(bsearch_arg(&value, &ranges->values[2 * ranges->nranges],
415 ranges->nsorted, sizeof(Datum),
416 compare_values, &cxt) == NULL);
417 }
418 }
419#endif
420}
static int compare_values(const void *a, const void *b, void *arg)
return str start
static struct @162 value
void * bsearch_arg(const void *key, const void *base0, size_t nmemb, size_t size, int(*compar)(const void *, const void *, void *), void *arg)
Definition: bsearch_arg.c:55
#define InvalidOid
Definition: postgres_ext.h:37
FmgrInfo * cmp
Datum values[FLEXIBLE_ARRAY_MEMBER]

References Assert, bsearch_arg(), Ranges::cmp, compare_context::cmpFn, Ranges::colloid, compare_context::colloid, compare_values(), DatumGetBool(), FunctionCall2Coll(), i, InvalidOid, Ranges::maxvalues, Ranges::nranges, Ranges::nsorted, Ranges::nvalues, start, Ranges::typid, value, and Ranges::values.

Referenced by compactify_ranges(), ensure_free_space_in_buffer(), range_add_value(), and range_deduplicate_values().

◆ brin_minmax_multi_add_value()

Datum brin_minmax_multi_add_value ( PG_FUNCTION_ARGS  )

Definition at line 2413 of file brin_minmax_multi.c.

2414{
2415 BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2416 BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
2420 Oid colloid = PG_GET_COLLATION();
2421 bool modified = false;
2422 Form_pg_attribute attr;
2423 AttrNumber attno;
2424 Ranges *ranges;
2425 SerializedRanges *serialized = NULL;
2426
2427 Assert(!isnull);
2428
2429 attno = column->bv_attno;
2430 attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2431
2432 /* use the already deserialized value, if possible */
2433 ranges = (Ranges *) DatumGetPointer(column->bv_mem_value);
2434
2435 /*
2436 * If this is the first non-null value, we need to initialize the range
2437 * list. Otherwise, just extract the existing range list from BrinValues.
2438 *
2439 * When starting with an empty range, we assume this is a batch mode and
2440 * we use a larger buffer. The buffer size is derived from the BRIN range
2441 * size, number of rows per page, with some sensible min/max values. A
2442 * small buffer would be bad for performance, but a large buffer might
2443 * require a lot of memory (because of keeping all the values).
2444 */
2445 if (column->bv_allnulls)
2446 {
2447 MemoryContext oldctx;
2448
2449 int target_maxvalues;
2450 int maxvalues;
2451 BlockNumber pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
2452
2453 /* what was specified as a reloption? */
2454 target_maxvalues = brin_minmax_multi_get_values(bdesc, opts);
2455
2456 /*
2457 * Determine the insert buffer size - we use 10x the target, capped to
2458 * the maximum number of values in the heap range. This is more than
2459 * enough, considering the actual number of rows per page is likely
2460 * much lower, but meh.
2461 */
2462 maxvalues = Min(target_maxvalues * MINMAX_BUFFER_FACTOR,
2463 MaxHeapTuplesPerPage * pagesPerRange);
2464
2465 /* but always at least the original value */
2466 maxvalues = Max(maxvalues, target_maxvalues);
2467
2468 /* always cap by MIN/MAX */
2469 maxvalues = Max(maxvalues, MINMAX_BUFFER_MIN);
2470 maxvalues = Min(maxvalues, MINMAX_BUFFER_MAX);
2471
2472 oldctx = MemoryContextSwitchTo(column->bv_context);
2473 ranges = minmax_multi_init(maxvalues);
2474 ranges->attno = attno;
2475 ranges->colloid = colloid;
2476 ranges->typid = attr->atttypid;
2477 ranges->target_maxvalues = target_maxvalues;
2478
2479 /* we'll certainly need the comparator, so just look it up now */
2480 ranges->cmp = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2482
2483 MemoryContextSwitchTo(oldctx);
2484
2485 column->bv_allnulls = false;
2486 modified = true;
2487
2488 column->bv_mem_value = PointerGetDatum(ranges);
2490 }
2491 else if (!ranges)
2492 {
2493 MemoryContext oldctx;
2494
2495 int maxvalues;
2496 BlockNumber pagesPerRange = BrinGetPagesPerRange(bdesc->bd_index);
2497
2498 oldctx = MemoryContextSwitchTo(column->bv_context);
2499
2500 serialized = (SerializedRanges *) PG_DETOAST_DATUM(column->bv_values[0]);
2501
2502 /*
2503 * Determine the insert buffer size - we use 10x the target, capped to
2504 * the maximum number of values in the heap range. This is more than
2505 * enough, considering the actual number of rows per page is likely
2506 * much lower, but meh.
2507 */
2508 maxvalues = Min(serialized->maxvalues * MINMAX_BUFFER_FACTOR,
2509 MaxHeapTuplesPerPage * pagesPerRange);
2510
2511 /* but always at least the original value */
2512 maxvalues = Max(maxvalues, serialized->maxvalues);
2513
2514 /* always cap by MIN/MAX */
2515 maxvalues = Max(maxvalues, MINMAX_BUFFER_MIN);
2516 maxvalues = Min(maxvalues, MINMAX_BUFFER_MAX);
2517
2518 ranges = brin_range_deserialize(maxvalues, serialized);
2519
2520 ranges->attno = attno;
2521 ranges->colloid = colloid;
2522 ranges->typid = attr->atttypid;
2523
2524 /* we'll certainly need the comparator, so just look it up now */
2525 ranges->cmp = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2527
2528 column->bv_mem_value = PointerGetDatum(ranges);
2530
2531 MemoryContextSwitchTo(oldctx);
2532 }
2533
2534 /*
2535 * Try to add the new value to the range. We need to update the modified
2536 * flag, so that we serialize the updated summary later.
2537 */
2538 modified |= range_add_value(bdesc, colloid, attno, attr, ranges, newval);
2539
2540
2541 PG_RETURN_BOOL(modified);
2542}
int16 AttrNumber
Definition: attnum.h:21
uint32 BlockNumber
Definition: block.h:31
#define BrinGetPagesPerRange(relation)
Definition: brin.h:40
static bool range_add_value(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval)
#define MINMAX_BUFFER_MAX
static int brin_minmax_multi_get_values(BrinDesc *bdesc, MinMaxMultiOptions *opts)
#define MINMAX_BUFFER_FACTOR
static void brin_minmax_multi_serialize(BrinDesc *bdesc, Datum src, Datum *dst)
static Ranges * brin_range_deserialize(int maxvalues, SerializedRanges *serialized)
static Ranges * minmax_multi_init(int maxvalues)
#define MINMAX_BUFFER_MIN
#define Min(x, y)
Definition: c.h:961
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:204
#define Max(x, y)
Definition: c.h:955
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GET_OPCLASS_OPTIONS()
Definition: fmgr.h:342
#define PG_DETOAST_DATUM(datum)
Definition: fmgr.h:240
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define newval
#define MaxHeapTuplesPerPage
Definition: htup_details.h:624
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
unsigned int Oid
Definition: postgres_ext.h:32
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bv_context
Definition: brin_tuple.h:36
Datum bv_mem_value
Definition: brin_tuple.h:35
brin_serialize_callback_type bv_serialize
Definition: brin_tuple.h:37
Datum * bv_values
Definition: brin_tuple.h:34
AttrNumber bv_attno
Definition: brin_tuple.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
int target_maxvalues
AttrNumber attno
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:154

References Assert, Ranges::attno, BrinDesc::bd_index, BrinDesc::bd_tupdesc, brin_minmax_multi_get_values(), brin_minmax_multi_serialize(), brin_range_deserialize(), BrinGetPagesPerRange, BTLessStrategyNumber, BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_context, BrinValues::bv_mem_value, BrinValues::bv_serialize, BrinValues::bv_values, Ranges::cmp, Ranges::colloid, DatumGetPointer(), Max, MaxHeapTuplesPerPage, SerializedRanges::maxvalues, MemoryContextSwitchTo(), Min, MINMAX_BUFFER_FACTOR, MINMAX_BUFFER_MAX, MINMAX_BUFFER_MIN, minmax_multi_get_strategy_procinfo(), minmax_multi_init(), newval, opts, PG_DETOAST_DATUM, PG_GET_COLLATION, PG_GET_OPCLASS_OPTIONS, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_BOOL, PG_USED_FOR_ASSERTS_ONLY, PointerGetDatum(), range_add_value(), Ranges::target_maxvalues, TupleDescAttr(), and Ranges::typid.

◆ brin_minmax_multi_consistent()

Datum brin_minmax_multi_consistent ( PG_FUNCTION_ARGS  )

Definition at line 2550 of file brin_minmax_multi.c.

2551{
2552 BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2553 BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
2554 ScanKey *keys = (ScanKey *) PG_GETARG_POINTER(2);
2555 int nkeys = PG_GETARG_INT32(3);
2556
2557 Oid colloid = PG_GET_COLLATION(),
2558 subtype;
2559 AttrNumber attno;
2560 Datum value;
2561 FmgrInfo *finfo;
2562 SerializedRanges *serialized;
2563 Ranges *ranges;
2564 int keyno;
2565 int rangeno;
2566 int i;
2567
2568 attno = column->bv_attno;
2569
2570 serialized = (SerializedRanges *) PG_DETOAST_DATUM(column->bv_values[0]);
2571 ranges = brin_range_deserialize(serialized->maxvalues, serialized);
2572
2573 /* inspect the ranges, and for each one evaluate the scan keys */
2574 for (rangeno = 0; rangeno < ranges->nranges; rangeno++)
2575 {
2576 Datum minval = ranges->values[2 * rangeno];
2577 Datum maxval = ranges->values[2 * rangeno + 1];
2578
2579 /* assume the range is matching, and we'll try to prove otherwise */
2580 bool matching = true;
2581
2582 for (keyno = 0; keyno < nkeys; keyno++)
2583 {
2584 bool matches;
2585 ScanKey key = keys[keyno];
2586
2587 /* NULL keys are handled and filtered-out in bringetbitmap */
2588 Assert(!(key->sk_flags & SK_ISNULL));
2589
2590 attno = key->sk_attno;
2591 subtype = key->sk_subtype;
2592 value = key->sk_argument;
2593 switch (key->sk_strategy)
2594 {
2597 finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2598 key->sk_strategy);
2599 /* first value from the array */
2600 matches = DatumGetBool(FunctionCall2Coll(finfo, colloid, minval, value));
2601 break;
2602
2604 {
2605 Datum compar;
2606 FmgrInfo *cmpFn;
2607
2608 /* by default this range does not match */
2609 matches = false;
2610
2611 /*
2612 * Otherwise, need to compare the new value with
2613 * boundaries of all the ranges. First check if it's
2614 * less than the absolute minimum, which is the first
2615 * value in the array.
2616 */
2617 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2619 compar = FunctionCall2Coll(cmpFn, colloid, minval, value);
2620
2621 /* smaller than the smallest value in this range */
2622 if (DatumGetBool(compar))
2623 break;
2624
2625 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2627 compar = FunctionCall2Coll(cmpFn, colloid, maxval, value);
2628
2629 /* larger than the largest value in this range */
2630 if (DatumGetBool(compar))
2631 break;
2632
2633 /*
2634 * We haven't managed to eliminate this range, so
2635 * consider it matching.
2636 */
2637 matches = true;
2638
2639 break;
2640 }
2643 finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2644 key->sk_strategy);
2645 /* last value from the array */
2646 matches = DatumGetBool(FunctionCall2Coll(finfo, colloid, maxval, value));
2647 break;
2648
2649 default:
2650 /* shouldn't happen */
2651 elog(ERROR, "invalid strategy number %d", key->sk_strategy);
2652 matches = false;
2653 break;
2654 }
2655
2656 /* the range has to match all the scan keys */
2657 matching &= matches;
2658
2659 /* once we find a non-matching key, we're done */
2660 if (!matching)
2661 break;
2662 }
2663
2664 /*
2665 * have we found a range matching all scan keys? if yes, we're done
2666 */
2667 if (matching)
2668 PG_RETURN_BOOL(true);
2669 }
2670
2671 /*
2672 * And now inspect the values. We don't bother with doing a binary search
2673 * here, because we're dealing with serialized / fully compacted ranges,
2674 * so there should be only very few values.
2675 */
2676 for (i = 0; i < ranges->nvalues; i++)
2677 {
2678 Datum val = ranges->values[2 * ranges->nranges + i];
2679
2680 /* assume the range is matching, and we'll try to prove otherwise */
2681 bool matching = true;
2682
2683 for (keyno = 0; keyno < nkeys; keyno++)
2684 {
2685 bool matches;
2686 ScanKey key = keys[keyno];
2687
2688 /* we've already dealt with NULL keys at the beginning */
2689 if (key->sk_flags & SK_ISNULL)
2690 continue;
2691
2692 attno = key->sk_attno;
2693 subtype = key->sk_subtype;
2694 value = key->sk_argument;
2695 switch (key->sk_strategy)
2696 {
2702
2703 finfo = minmax_multi_get_strategy_procinfo(bdesc, attno, subtype,
2704 key->sk_strategy);
2705 matches = DatumGetBool(FunctionCall2Coll(finfo, colloid, val, value));
2706 break;
2707
2708 default:
2709 /* shouldn't happen */
2710 elog(ERROR, "invalid strategy number %d", key->sk_strategy);
2711 matches = false;
2712 break;
2713 }
2714
2715 /* the range has to match all the scan keys */
2716 matching &= matches;
2717
2718 /* once we find a non-matching key, we're done */
2719 if (!matching)
2720 break;
2721 }
2722
2723 /* have we found a range matching all scan keys? if yes, we're done */
2724 if (matching)
2725 PG_RETURN_BOOL(true);
2726 }
2727
2728 PG_RETURN_BOOL(false);
2729}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define PG_GETARG_INT32(n)
Definition: fmgr.h:269
long val
Definition: informix.c:689
#define SK_ISNULL
Definition: skey.h:115
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

References Assert, brin_range_deserialize(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BrinValues::bv_attno, BrinValues::bv_values, DatumGetBool(), elog, ERROR, FunctionCall2Coll(), i, sort-test::key, SerializedRanges::maxvalues, minmax_multi_get_strategy_procinfo(), Ranges::nranges, Ranges::nvalues, PG_DETOAST_DATUM, PG_GET_COLLATION, PG_GETARG_INT32, PG_GETARG_POINTER, PG_RETURN_BOOL, SK_ISNULL, val, value, and Ranges::values.

◆ brin_minmax_multi_distance_date()

Datum brin_minmax_multi_distance_date ( PG_FUNCTION_ARGS  )

Definition at line 2081 of file brin_minmax_multi.c.

2082{
2083 float8 delta = 0;
2084 DateADT dateVal1 = PG_GETARG_DATEADT(0);
2085 DateADT dateVal2 = PG_GETARG_DATEADT(1);
2086
2087 delta = (float8) dateVal2 - (float8) dateVal1;
2088
2089 Assert(delta >= 0);
2090
2091 PG_RETURN_FLOAT8(delta);
2092}
double float8
Definition: c.h:587
int32 DateADT
Definition: date.h:23
#define PG_GETARG_DATEADT(n)
Definition: date.h:89
#define PG_RETURN_FLOAT8(x)
Definition: fmgr.h:367

References Assert, PG_GETARG_DATEADT, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_float4()

Datum brin_minmax_multi_distance_float4 ( PG_FUNCTION_ARGS  )

Definition at line 1884 of file brin_minmax_multi.c.

1885{
1886 float a1 = PG_GETARG_FLOAT4(0);
1887 float a2 = PG_GETARG_FLOAT4(1);
1888
1889 /* if both values are NaN, then we consider them the same */
1890 if (isnan(a1) && isnan(a2))
1891 PG_RETURN_FLOAT8(0.0);
1892
1893 /* if one value is NaN, use infinite distance */
1894 if (isnan(a1) || isnan(a2))
1896
1897 /*
1898 * We know the values are range boundaries, but the range may be collapsed
1899 * (i.e. single points), with equal values.
1900 */
1901 Assert(a1 <= a2);
1902
1903 PG_RETURN_FLOAT8((double) a2 - (double) a1);
1904}
static float8 get_float8_infinity(void)
Definition: float.h:94
#define PG_GETARG_FLOAT4(n)
Definition: fmgr.h:281
static const FormData_pg_attribute a1
Definition: heap.c:143
static const FormData_pg_attribute a2
Definition: heap.c:156

References a1, a2, Assert, get_float8_infinity(), PG_GETARG_FLOAT4, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_float8()

Datum brin_minmax_multi_distance_float8 ( PG_FUNCTION_ARGS  )

Definition at line 1910 of file brin_minmax_multi.c.

1911{
1912 double a1 = PG_GETARG_FLOAT8(0);
1913 double a2 = PG_GETARG_FLOAT8(1);
1914
1915 /* if both values are NaN, then we consider them the same */
1916 if (isnan(a1) && isnan(a2))
1917 PG_RETURN_FLOAT8(0.0);
1918
1919 /* if one value is NaN, use infinite distance */
1920 if (isnan(a1) || isnan(a2))
1922
1923 /*
1924 * We know the values are range boundaries, but the range may be collapsed
1925 * (i.e. single points), with equal values.
1926 */
1927 Assert(a1 <= a2);
1928
1930}
#define PG_GETARG_FLOAT8(n)
Definition: fmgr.h:282

References a1, a2, Assert, get_float8_infinity(), PG_GETARG_FLOAT8, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_inet()

Datum brin_minmax_multi_distance_inet ( PG_FUNCTION_ARGS  )

Definition at line 2298 of file brin_minmax_multi.c.

2299{
2300 float8 delta;
2301 int i;
2302 int len;
2303 unsigned char *addra,
2304 *addrb;
2305
2306 inet *ipa = PG_GETARG_INET_PP(0);
2307 inet *ipb = PG_GETARG_INET_PP(1);
2308
2309 int lena,
2310 lenb;
2311
2312 /*
2313 * If the addresses are from different families, consider them to be in
2314 * maximal possible distance (which is 1.0).
2315 */
2316 if (ip_family(ipa) != ip_family(ipb))
2317 PG_RETURN_FLOAT8(1.0);
2318
2319 addra = (unsigned char *) palloc(ip_addrsize(ipa));
2320 memcpy(addra, ip_addr(ipa), ip_addrsize(ipa));
2321
2322 addrb = (unsigned char *) palloc(ip_addrsize(ipb));
2323 memcpy(addrb, ip_addr(ipb), ip_addrsize(ipb));
2324
2325 /*
2326 * The length is calculated from the mask length, because we sort the
2327 * addresses by first address in the range, so A.B.C.D/24 < A.B.C.1 (the
2328 * first range starts at A.B.C.0, which is before A.B.C.1). We don't want
2329 * to produce a negative delta in this case, so we just cut the extra
2330 * bytes.
2331 *
2332 * XXX Maybe this should be a bit more careful and cut the bits, not just
2333 * whole bytes.
2334 */
2335 lena = ip_bits(ipa);
2336 lenb = ip_bits(ipb);
2337
2338 len = ip_addrsize(ipa);
2339
2340 /* apply the network mask to both addresses */
2341 for (i = 0; i < len; i++)
2342 {
2343 unsigned char mask;
2344 int nbits;
2345
2346 nbits = Max(0, lena - (i * 8));
2347 if (nbits < 8)
2348 {
2349 mask = (0xFF << (8 - nbits));
2350 addra[i] = (addra[i] & mask);
2351 }
2352
2353 nbits = Max(0, lenb - (i * 8));
2354 if (nbits < 8)
2355 {
2356 mask = (0xFF << (8 - nbits));
2357 addrb[i] = (addrb[i] & mask);
2358 }
2359 }
2360
2361 /* Calculate the difference between the addresses. */
2362 delta = 0;
2363 for (i = len - 1; i >= 0; i--)
2364 {
2365 unsigned char a = addra[i];
2366 unsigned char b = addrb[i];
2367
2368 delta += (float8) b - (float8) a;
2369 delta /= 256;
2370 }
2371
2372 Assert((delta >= 0) && (delta <= 1));
2373
2374 pfree(addra);
2375 pfree(addrb);
2376
2377 PG_RETURN_FLOAT8(delta);
2378}
int b
Definition: isn.c:69
int a
Definition: isn.c:68
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len
Definition: inet.h:53
#define ip_addr(inetptr)
Definition: inet.h:77
#define PG_GETARG_INET_PP(n)
Definition: inet.h:134
#define ip_family(inetptr)
Definition: inet.h:71
#define ip_addrsize(inetptr)
Definition: inet.h:80
#define ip_bits(inetptr)
Definition: inet.h:74

References a, Assert, b, i, ip_addr, ip_addrsize, ip_bits, ip_family, len, Max, palloc(), pfree(), PG_GETARG_INET_PP, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_int2()

Datum brin_minmax_multi_distance_int2 ( PG_FUNCTION_ARGS  )

Definition at line 1936 of file brin_minmax_multi.c.

1937{
1940
1941 /*
1942 * We know the values are range boundaries, but the range may be collapsed
1943 * (i.e. single points), with equal values.
1944 */
1945 Assert(a1 <= a2);
1946
1947 PG_RETURN_FLOAT8((double) a2 - (double) a1);
1948}
int16_t int16
Definition: c.h:483
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271

References a1, a2, Assert, PG_GETARG_INT16, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_int4()

Datum brin_minmax_multi_distance_int4 ( PG_FUNCTION_ARGS  )

Definition at line 1954 of file brin_minmax_multi.c.

1955{
1958
1959 /*
1960 * We know the values are range boundaries, but the range may be collapsed
1961 * (i.e. single points), with equal values.
1962 */
1963 Assert(a1 <= a2);
1964
1965 PG_RETURN_FLOAT8((double) a2 - (double) a1);
1966}
int32_t int32
Definition: c.h:484

References a1, a2, Assert, PG_GETARG_INT32, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_int8()

Datum brin_minmax_multi_distance_int8 ( PG_FUNCTION_ARGS  )

Definition at line 1972 of file brin_minmax_multi.c.

1973{
1976
1977 /*
1978 * We know the values are range boundaries, but the range may be collapsed
1979 * (i.e. single points), with equal values.
1980 */
1981 Assert(a1 <= a2);
1982
1983 PG_RETURN_FLOAT8((double) a2 - (double) a1);
1984}
int64_t int64
Definition: c.h:485
#define PG_GETARG_INT64(n)
Definition: fmgr.h:283

References a1, a2, Assert, PG_GETARG_INT64, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_interval()

Datum brin_minmax_multi_distance_interval ( PG_FUNCTION_ARGS  )

Definition at line 2156 of file brin_minmax_multi.c.

2157{
2158 float8 delta = 0;
2159
2162
2163 int64 dayfraction;
2164 int64 days;
2165
2166 /*
2167 * Delta is (fractional) number of days between the intervals. Assume
2168 * months have 30 days for consistency with interval_cmp_internal. We
2169 * don't need to be exact, in the worst case we'll build a bit less
2170 * efficient ranges. But we should not contradict interval_cmp.
2171 */
2172 dayfraction = (ib->time % USECS_PER_DAY) - (ia->time % USECS_PER_DAY);
2173 days = (ib->time / USECS_PER_DAY) - (ia->time / USECS_PER_DAY);
2174 days += (int64) ib->day - (int64) ia->day;
2175 days += ((int64) ib->month - (int64) ia->month) * INT64CONST(30);
2176
2177 /* convert to double precision */
2178 delta = (double) days + dayfraction / (double) USECS_PER_DAY;
2179
2180 Assert(delta >= 0);
2181
2182 PG_RETURN_FLOAT8(delta);
2183}
const char *const days[]
Definition: datetime.c:84
#define INT64CONST(x)
Definition: c.h:502
#define USECS_PER_DAY
Definition: timestamp.h:131
int32 day
Definition: timestamp.h:51
int32 month
Definition: timestamp.h:52
TimeOffset time
Definition: timestamp.h:49
#define PG_GETARG_INTERVAL_P(n)
Definition: timestamp.h:65

References Assert, Interval::day, days, INT64CONST, Interval::month, PG_GETARG_INTERVAL_P, PG_RETURN_FLOAT8, Interval::time, and USECS_PER_DAY.

◆ brin_minmax_multi_distance_macaddr()

Datum brin_minmax_multi_distance_macaddr ( PG_FUNCTION_ARGS  )

Definition at line 2213 of file brin_minmax_multi.c.

2214{
2215 float8 delta;
2216
2219
2220 delta = ((float8) b->f - (float8) a->f);
2221 delta /= 256;
2222
2223 delta += ((float8) b->e - (float8) a->e);
2224 delta /= 256;
2225
2226 delta += ((float8) b->d - (float8) a->d);
2227 delta /= 256;
2228
2229 delta += ((float8) b->c - (float8) a->c);
2230 delta /= 256;
2231
2232 delta += ((float8) b->b - (float8) a->b);
2233 delta /= 256;
2234
2235 delta += ((float8) b->a - (float8) a->a);
2236 delta /= 256;
2237
2238 Assert(delta >= 0);
2239
2240 PG_RETURN_FLOAT8(delta);
2241}
Definition: inet.h:95
#define PG_GETARG_MACADDR_P(n)
Definition: inet.h:158

References a, Assert, b, PG_GETARG_MACADDR_P, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_macaddr8()

Datum brin_minmax_multi_distance_macaddr8 ( PG_FUNCTION_ARGS  )

Definition at line 2250 of file brin_minmax_multi.c.

2251{
2252 float8 delta;
2253
2256
2257 delta = ((float8) b->h - (float8) a->h);
2258 delta /= 256;
2259
2260 delta += ((float8) b->g - (float8) a->g);
2261 delta /= 256;
2262
2263 delta += ((float8) b->f - (float8) a->f);
2264 delta /= 256;
2265
2266 delta += ((float8) b->e - (float8) a->e);
2267 delta /= 256;
2268
2269 delta += ((float8) b->d - (float8) a->d);
2270 delta /= 256;
2271
2272 delta += ((float8) b->c - (float8) a->c);
2273 delta /= 256;
2274
2275 delta += ((float8) b->b - (float8) a->b);
2276 delta /= 256;
2277
2278 delta += ((float8) b->a - (float8) a->a);
2279 delta /= 256;
2280
2281 Assert(delta >= 0);
2282
2283 PG_RETURN_FLOAT8(delta);
2284}
Definition: inet.h:108
#define PG_GETARG_MACADDR8_P(n)
Definition: inet.h:174

References a, Assert, b, PG_GETARG_MACADDR8_P, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_numeric()

Datum brin_minmax_multi_distance_numeric ( PG_FUNCTION_ARGS  )

Definition at line 2022 of file brin_minmax_multi.c.

2023{
2024 Datum d;
2027
2028 /*
2029 * We know the values are range boundaries, but the range may be collapsed
2030 * (i.e. single points), with equal values.
2031 */
2033
2034 d = DirectFunctionCall2(numeric_sub, a2, a1); /* a2 - a1 */
2035
2037}
Datum numeric_sub(PG_FUNCTION_ARGS)
Definition: numeric.c:3043
Datum numeric_le(PG_FUNCTION_ARGS)
Definition: numeric.c:2608
Datum numeric_float8(PG_FUNCTION_ARGS)
Definition: numeric.c:4746
#define DirectFunctionCall2(func, arg1, arg2)
Definition: fmgr.h:643
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641

References a1, a2, Assert, DatumGetBool(), DirectFunctionCall1, DirectFunctionCall2, numeric_float8(), numeric_le(), numeric_sub(), PG_GETARG_DATUM, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_pg_lsn()

Datum brin_minmax_multi_distance_pg_lsn ( PG_FUNCTION_ARGS  )

Definition at line 2192 of file brin_minmax_multi.c.

2193{
2194 float8 delta = 0;
2195
2196 XLogRecPtr lsna = PG_GETARG_LSN(0);
2197 XLogRecPtr lsnb = PG_GETARG_LSN(1);
2198
2199 delta = (lsnb - lsna);
2200
2201 Assert(delta >= 0);
2202
2203 PG_RETURN_FLOAT8(delta);
2204}
#define PG_GETARG_LSN(n)
Definition: pg_lsn.h:33
uint64 XLogRecPtr
Definition: xlogdefs.h:21

References Assert, PG_GETARG_LSN, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_tid()

Datum brin_minmax_multi_distance_tid ( PG_FUNCTION_ARGS  )

Definition at line 1991 of file brin_minmax_multi.c.

1992{
1993 double da1,
1994 da2;
1995
1998
1999 /*
2000 * We know the values are range boundaries, but the range may be collapsed
2001 * (i.e. single points), with equal values.
2002 */
2003 Assert(ItemPointerCompare(pa1, pa2) <= 0);
2004
2005 /*
2006 * We use the no-check variants here, because user-supplied values may
2007 * have (ip_posid == 0). See ItemPointerCompare.
2008 */
2011
2014
2015 PG_RETURN_FLOAT8(da2 - da1);
2016}
int32 ItemPointerCompare(ItemPointer arg1, ItemPointer arg2)
Definition: itemptr.c:51
static OffsetNumber ItemPointerGetOffsetNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:114
static BlockNumber ItemPointerGetBlockNumberNoCheck(const ItemPointerData *pointer)
Definition: itemptr.h:93
ItemPointerData * ItemPointer
Definition: itemptr.h:49

References Assert, ItemPointerCompare(), ItemPointerGetBlockNumberNoCheck(), ItemPointerGetOffsetNumberNoCheck(), MaxHeapTuplesPerPage, PG_GETARG_DATUM, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_time()

Datum brin_minmax_multi_distance_time ( PG_FUNCTION_ARGS  )

Definition at line 2100 of file brin_minmax_multi.c.

2101{
2102 float8 delta = 0;
2103
2104 TimeADT ta = PG_GETARG_TIMEADT(0);
2105 TimeADT tb = PG_GETARG_TIMEADT(1);
2106
2107 delta = (tb - ta);
2108
2109 Assert(delta >= 0);
2110
2111 PG_RETURN_FLOAT8(delta);
2112}
#define PG_GETARG_TIMEADT(n)
Definition: date.h:90
int64 TimeADT
Definition: date.h:25

References Assert, PG_GETARG_TIMEADT, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_timestamp()

Datum brin_minmax_multi_distance_timestamp ( PG_FUNCTION_ARGS  )

Definition at line 2138 of file brin_minmax_multi.c.

2139{
2140 float8 delta = 0;
2141
2144
2145 delta = (float8) dt2 - (float8) dt1;
2146
2147 Assert(delta >= 0);
2148
2149 PG_RETURN_FLOAT8(delta);
2150}
int64 Timestamp
Definition: timestamp.h:38
#define PG_GETARG_TIMESTAMP(n)
Definition: timestamp.h:63

References Assert, PG_GETARG_TIMESTAMP, and PG_RETURN_FLOAT8.

◆ brin_minmax_multi_distance_timetz()

Datum brin_minmax_multi_distance_timetz ( PG_FUNCTION_ARGS  )

Definition at line 2120 of file brin_minmax_multi.c.

2121{
2122 float8 delta = 0;
2123
2126
2127 delta = (tb->time - ta->time) + (tb->zone - ta->zone) * USECS_PER_SEC;
2128
2129 Assert(delta >= 0);
2130
2131 PG_RETURN_FLOAT8(delta);
2132}
#define USECS_PER_SEC
Definition: timestamp.h:134
#define PG_GETARG_TIMETZADT_P(n)
Definition: date.h:91
Definition: date.h:28
TimeADT time
Definition: date.h:29
int32 zone
Definition: date.h:30

References Assert, PG_GETARG_TIMETZADT_P, PG_RETURN_FLOAT8, TimeTzADT::time, USECS_PER_SEC, and TimeTzADT::zone.

◆ brin_minmax_multi_distance_uuid()

Datum brin_minmax_multi_distance_uuid ( PG_FUNCTION_ARGS  )

Definition at line 2048 of file brin_minmax_multi.c.

2049{
2050 int i;
2051 float8 delta = 0;
2052
2055
2056 pg_uuid_t *u1 = DatumGetUUIDP(a1);
2057 pg_uuid_t *u2 = DatumGetUUIDP(a2);
2058
2059 /*
2060 * We know the values are range boundaries, but the range may be collapsed
2061 * (i.e. single points), with equal values.
2062 */
2064
2065 /* compute approximate delta as a double precision value */
2066 for (i = UUID_LEN - 1; i >= 0; i--)
2067 {
2068 delta += (int) u2->data[i] - (int) u1->data[i];
2069 delta /= 256;
2070 }
2071
2072 Assert(delta >= 0);
2073
2074 PG_RETURN_FLOAT8(delta);
2075}
Definition: uuid.h:21
unsigned char data[UUID_LEN]
Definition: uuid.h:22
Datum uuid_le(PG_FUNCTION_ARGS)
Definition: uuid.c:215
static pg_uuid_t * DatumGetUUIDP(Datum X)
Definition: uuid.h:35
#define UUID_LEN
Definition: uuid.h:18

References a1, a2, Assert, pg_uuid_t::data, DatumGetBool(), DatumGetUUIDP(), DirectFunctionCall2, i, PG_GETARG_DATUM, PG_RETURN_FLOAT8, uuid_le(), and UUID_LEN.

◆ brin_minmax_multi_get_values()

static int brin_minmax_multi_get_values ( BrinDesc bdesc,
MinMaxMultiOptions opts 
)
static

Definition at line 2400 of file brin_minmax_multi.c.

2401{
2403}
#define MinMaxMultiGetValuesPerRange(opts)

References MinMaxMultiGetValuesPerRange, and opts.

Referenced by brin_minmax_multi_add_value().

◆ brin_minmax_multi_opcinfo()

Datum brin_minmax_multi_opcinfo ( PG_FUNCTION_ARGS  )

Definition at line 1860 of file brin_minmax_multi.c.

1861{
1862 BrinOpcInfo *result;
1863
1864 /*
1865 * opaque->strategy_procinfos is initialized lazily; here it is set to
1866 * all-uninitialized by palloc0 which sets fn_oid to InvalidOid.
1867 */
1868
1869 result = palloc0(MAXALIGN(SizeofBrinOpcInfo(1)) +
1870 sizeof(MinmaxMultiOpaque));
1871 result->oi_nstored = 1;
1872 result->oi_regular_nulls = true;
1873 result->oi_opaque = (MinmaxMultiOpaque *)
1874 MAXALIGN((char *) result + SizeofBrinOpcInfo(1));
1875 result->oi_typcache[0] = lookup_type_cache(PG_BRIN_MINMAX_MULTI_SUMMARYOID, 0);
1876
1877 PG_RETURN_POINTER(result);
1878}
#define SizeofBrinOpcInfo(ncols)
Definition: brin_internal.h:41
#define MAXALIGN(LEN)
Definition: c.h:768
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc0(Size size)
Definition: mcxt.c:1347
TypeCacheEntry * oi_typcache[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:37
uint16 oi_nstored
Definition: brin_internal.h:28
bool oi_regular_nulls
Definition: brin_internal.h:31
void * oi_opaque
Definition: brin_internal.h:34
TypeCacheEntry * lookup_type_cache(Oid type_id, int flags)
Definition: typcache.c:386

References lookup_type_cache(), MAXALIGN, BrinOpcInfo::oi_nstored, BrinOpcInfo::oi_opaque, BrinOpcInfo::oi_regular_nulls, BrinOpcInfo::oi_typcache, palloc0(), PG_RETURN_POINTER, and SizeofBrinOpcInfo.

◆ brin_minmax_multi_options()

Datum brin_minmax_multi_options ( PG_FUNCTION_ARGS  )

Definition at line 2963 of file brin_minmax_multi.c.

2964{
2966
2967 init_local_reloptions(relopts, sizeof(MinMaxMultiOptions));
2968
2969 add_local_int_reloption(relopts, "values_per_range", "desc",
2971 offsetof(MinMaxMultiOptions, valuesPerRange));
2972
2974}
#define MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE
#define PG_RETURN_VOID()
Definition: fmgr.h:349
void init_local_reloptions(local_relopts *relopts, Size relopt_struct_size)
Definition: reloptions.c:753
void add_local_int_reloption(local_relopts *relopts, const char *name, const char *desc, int default_val, int min_val, int max_val, int offset)
Definition: reloptions.c:937

References add_local_int_reloption(), init_local_reloptions(), MINMAX_MULTI_DEFAULT_VALUES_PER_PAGE, PG_GETARG_POINTER, and PG_RETURN_VOID.

◆ brin_minmax_multi_serialize()

static void brin_minmax_multi_serialize ( BrinDesc bdesc,
Datum  src,
Datum dst 
)
static

Definition at line 2381 of file brin_minmax_multi.c.

2382{
2383 Ranges *ranges = (Ranges *) DatumGetPointer(src);
2385
2386 /*
2387 * In batch mode, we need to compress the accumulated values to the
2388 * actually requested number of values/ranges.
2389 */
2390 compactify_ranges(bdesc, ranges, ranges->target_maxvalues);
2391
2392 /* At this point everything has to be fully sorted. */
2393 Assert(ranges->nsorted == ranges->nvalues);
2394
2395 s = brin_range_serialize(ranges);
2396 dst[0] = PointerGetDatum(s);
2397}
static void compactify_ranges(BrinDesc *bdesc, Ranges *ranges, int max_values)
static SerializedRanges * brin_range_serialize(Ranges *range)

References Assert, brin_range_serialize(), compactify_ranges(), DatumGetPointer(), Ranges::nsorted, Ranges::nvalues, PointerGetDatum(), and Ranges::target_maxvalues.

Referenced by brin_minmax_multi_add_value().

◆ brin_minmax_multi_summary_in()

Datum brin_minmax_multi_summary_in ( PG_FUNCTION_ARGS  )

Definition at line 2985 of file brin_minmax_multi.c.

2986{
2987 /*
2988 * brin_minmax_multi_summary stores the data in binary form and parsing
2989 * text input is not needed, so disallow this.
2990 */
2991 ereport(ERROR,
2992 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2993 errmsg("cannot accept a value of type %s", "brin_minmax_multi_summary")));
2994
2995 PG_RETURN_VOID(); /* keep compiler quiet */
2996}
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ereport(elevel,...)
Definition: elog.h:149

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ brin_minmax_multi_summary_out()

Datum brin_minmax_multi_summary_out ( PG_FUNCTION_ARGS  )

Definition at line 3007 of file brin_minmax_multi.c.

3008{
3009 int i;
3010 int idx;
3011 SerializedRanges *ranges;
3012 Ranges *ranges_deserialized;
3014 bool isvarlena;
3015 Oid outfunc;
3016 FmgrInfo fmgrinfo;
3017 ArrayBuildState *astate_values = NULL;
3018
3021
3022 /*
3023 * Detoast to get value with full 4B header (can't be stored in a toast
3024 * table, but can use 1B header).
3025 */
3027
3028 /* lookup output func for the type */
3029 getTypeOutputInfo(ranges->typid, &outfunc, &isvarlena);
3030 fmgr_info(outfunc, &fmgrinfo);
3031
3032 /* deserialize the range info easy-to-process pieces */
3033 ranges_deserialized = brin_range_deserialize(ranges->maxvalues, ranges);
3034
3035 appendStringInfo(&str, "nranges: %d nvalues: %d maxvalues: %d",
3036 ranges_deserialized->nranges,
3037 ranges_deserialized->nvalues,
3038 ranges_deserialized->maxvalues);
3039
3040 /* serialize ranges */
3041 idx = 0;
3042 for (i = 0; i < ranges_deserialized->nranges; i++)
3043 {
3044 char *a,
3045 *b;
3046 text *c;
3048
3050
3051 a = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
3052 b = OutputFunctionCall(&fmgrinfo, ranges_deserialized->values[idx++]);
3053
3054 appendStringInfo(&buf, "%s ... %s", a, b);
3055
3056 c = cstring_to_text_with_len(buf.data, buf.len);
3057
3058 astate_values = accumArrayResult(astate_values,
3060 false,
3061 TEXTOID,
3063 }
3064
3065 if (ranges_deserialized->nranges > 0)
3066 {
3067 Oid typoutput;
3068 bool typIsVarlena;
3069 Datum val;
3070 char *extval;
3071
3072 getTypeOutputInfo(ANYARRAYOID, &typoutput, &typIsVarlena);
3073
3074 val = makeArrayResult(astate_values, CurrentMemoryContext);
3075
3076 extval = OidOutputFunctionCall(typoutput, val);
3077
3078 appendStringInfo(&str, " ranges: %s", extval);
3079 }
3080
3081 /* serialize individual values */
3082 astate_values = NULL;
3083
3084 for (i = 0; i < ranges_deserialized->nvalues; i++)
3085 {
3086 Datum a;
3087 text *b;
3088
3089 a = FunctionCall1(&fmgrinfo, ranges_deserialized->values[idx++]);
3091
3092 astate_values = accumArrayResult(astate_values,
3094 false,
3095 TEXTOID,
3097 }
3098
3099 if (ranges_deserialized->nvalues > 0)
3100 {
3101 Oid typoutput;
3102 bool typIsVarlena;
3103 Datum val;
3104 char *extval;
3105
3106 getTypeOutputInfo(ANYARRAYOID, &typoutput, &typIsVarlena);
3107
3108 val = makeArrayResult(astate_values, CurrentMemoryContext);
3109
3110 extval = OidOutputFunctionCall(typoutput, val);
3111
3112 appendStringInfo(&str, " values: %s", extval);
3113 }
3114
3115
3117
3118 PG_RETURN_CSTRING(str.data);
3119}
Datum idx(PG_FUNCTION_ARGS)
Definition: _int_op.c:259
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5350
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5420
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:127
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1763
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1683
#define PG_RETURN_CSTRING(x)
Definition: fmgr.h:362
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:659
const char * str
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2934
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
static char * buf
Definition: pg_test_fsync.c:72
static char * DatumGetCString(Datum X)
Definition: postgres.h:340
char * c
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:145
void appendStringInfoChar(StringInfo str, char ch)
Definition: stringinfo.c:242
void initStringInfo(StringInfo str)
Definition: stringinfo.c:97
Definition: c.h:644
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196
text * cstring_to_text(const char *s)
Definition: varlena.c:184

References a, accumArrayResult(), appendStringInfo(), appendStringInfoChar(), b, brin_range_deserialize(), buf, cstring_to_text(), cstring_to_text_with_len(), CurrentMemoryContext, DatumGetCString(), fmgr_info(), FunctionCall1, getTypeOutputInfo(), i, idx(), initStringInfo(), makeArrayResult(), Ranges::maxvalues, SerializedRanges::maxvalues, Ranges::nranges, Ranges::nvalues, OidOutputFunctionCall(), OutputFunctionCall(), PG_DETOAST_DATUM, PG_GETARG_DATUM, PG_RETURN_CSTRING, PointerGetDatum(), str, SerializedRanges::typid, val, and Ranges::values.

◆ brin_minmax_multi_summary_recv()

Datum brin_minmax_multi_summary_recv ( PG_FUNCTION_ARGS  )

Definition at line 3126 of file brin_minmax_multi.c.

3127{
3128 ereport(ERROR,
3129 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3130 errmsg("cannot accept a value of type %s", "brin_minmax_multi_summary")));
3131
3132 PG_RETURN_VOID(); /* keep compiler quiet */
3133}

References ereport, errcode(), errmsg(), ERROR, and PG_RETURN_VOID.

◆ brin_minmax_multi_summary_send()

Datum brin_minmax_multi_summary_send ( PG_FUNCTION_ARGS  )

Definition at line 3143 of file brin_minmax_multi.c.

3144{
3145 return byteasend(fcinfo);
3146}
Datum byteasend(PG_FUNCTION_ARGS)
Definition: varlena.c:490

References byteasend().

◆ brin_minmax_multi_union()

Datum brin_minmax_multi_union ( PG_FUNCTION_ARGS  )

Definition at line 2736 of file brin_minmax_multi.c.

2737{
2738 BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
2739 BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1);
2740 BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2);
2741
2742 Oid colloid = PG_GET_COLLATION();
2743 SerializedRanges *serialized_a;
2744 SerializedRanges *serialized_b;
2745 Ranges *ranges_a;
2746 Ranges *ranges_b;
2747 AttrNumber attno;
2748 Form_pg_attribute attr;
2749 ExpandedRange *eranges;
2750 int neranges;
2751 FmgrInfo *cmpFn,
2752 *distanceFn;
2753 DistanceValue *distances;
2754 MemoryContext ctx;
2755 MemoryContext oldctx;
2756
2757 Assert(col_a->bv_attno == col_b->bv_attno);
2758 Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
2759
2760 attno = col_a->bv_attno;
2761 attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2762
2763 serialized_a = (SerializedRanges *) PG_DETOAST_DATUM(col_a->bv_values[0]);
2764 serialized_b = (SerializedRanges *) PG_DETOAST_DATUM(col_b->bv_values[0]);
2765
2766 ranges_a = brin_range_deserialize(serialized_a->maxvalues, serialized_a);
2767 ranges_b = brin_range_deserialize(serialized_b->maxvalues, serialized_b);
2768
2769 /* make sure neither of the ranges is NULL */
2770 Assert(ranges_a && ranges_b);
2771
2772 neranges = (ranges_a->nranges + ranges_a->nvalues) +
2773 (ranges_b->nranges + ranges_b->nvalues);
2774
2775 /*
2776 * The distanceFn calls (which may internally call e.g. numeric_le) may
2777 * allocate quite a bit of memory, and we must not leak it. Otherwise,
2778 * we'd have problems e.g. when building indexes. So we create a local
2779 * memory context and make sure we free the memory before leaving this
2780 * function (not after every call).
2781 */
2783 "minmax-multi context",
2785
2786 oldctx = MemoryContextSwitchTo(ctx);
2787
2788 /* allocate and fill */
2789 eranges = (ExpandedRange *) palloc0(neranges * sizeof(ExpandedRange));
2790
2791 /* fill the expanded ranges with entries for the first range */
2792 fill_expanded_ranges(eranges, ranges_a->nranges + ranges_a->nvalues,
2793 ranges_a);
2794
2795 /* and now add combine ranges for the second range */
2796 fill_expanded_ranges(&eranges[ranges_a->nranges + ranges_a->nvalues],
2797 ranges_b->nranges + ranges_b->nvalues,
2798 ranges_b);
2799
2800 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
2802
2803 /* sort the expanded ranges */
2804 neranges = sort_expanded_ranges(cmpFn, colloid, eranges, neranges);
2805
2806 /*
2807 * We've loaded two different lists of expanded ranges, so some of them
2808 * may be overlapping. So walk through them and merge them.
2809 */
2810 neranges = merge_overlapping_ranges(cmpFn, colloid, eranges, neranges);
2811
2812 /* check that the combine ranges are correct (no overlaps, ordering) */
2813 AssertCheckExpandedRanges(bdesc, colloid, attno, attr, eranges, neranges);
2814
2815 /*
2816 * If needed, reduce some of the ranges.
2817 *
2818 * XXX This may be fairly expensive, so maybe we should do it only when
2819 * it's actually needed (when we have too many ranges).
2820 */
2821
2822 /* build array of gap distances and sort them in ascending order */
2823 distanceFn = minmax_multi_get_procinfo(bdesc, attno, PROCNUM_DISTANCE);
2824 distances = build_distances(distanceFn, colloid, eranges, neranges);
2825
2826 /*
2827 * See how many values would be needed to store the current ranges, and if
2828 * needed combine as many of them to get below the threshold. The
2829 * collapsed ranges will be stored as a single value.
2830 *
2831 * XXX This does not apply the load factor, as we don't expect to add more
2832 * values to the range, so we prefer to keep as many ranges as possible.
2833 *
2834 * XXX Can the maxvalues be different in the two ranges? Perhaps we should
2835 * use maximum of those?
2836 */
2837 neranges = reduce_expanded_ranges(eranges, neranges, distances,
2838 ranges_a->maxvalues,
2839 cmpFn, colloid);
2840
2841 /* Is the result of reducing expanded ranges correct? */
2842 AssertCheckExpandedRanges(bdesc, colloid, attno, attr, eranges, neranges);
2843
2844 /* update the first range summary */
2845 store_expanded_ranges(ranges_a, eranges, neranges);
2846
2847 MemoryContextSwitchTo(oldctx);
2849
2850 /* cleanup and update the serialized value */
2851 pfree(serialized_a);
2852 col_a->bv_values[0] = PointerGetDatum(brin_range_serialize(ranges_a));
2853
2855}
static void AssertCheckExpandedRanges(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, ExpandedRange *ranges, int nranges)
static DistanceValue * build_distances(FmgrInfo *distanceFn, Oid colloid, ExpandedRange *eranges, int neranges)
static int reduce_expanded_ranges(ExpandedRange *eranges, int neranges, DistanceValue *distances, int max_values, FmgrInfo *cmp, Oid colloid)
static FmgrInfo * minmax_multi_get_procinfo(BrinDesc *bdesc, uint16 attno, uint16 procnum)
static int sort_expanded_ranges(FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
static void fill_expanded_ranges(ExpandedRange *eranges, int neranges, Ranges *ranges)
static int merge_overlapping_ranges(FmgrInfo *cmp, Oid colloid, ExpandedRange *eranges, int neranges)
static void store_expanded_ranges(Ranges *ranges, ExpandedRange *eranges, int neranges)
#define PROCNUM_DISTANCE
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AssertCheckExpandedRanges(), BrinDesc::bd_tupdesc, brin_range_deserialize(), brin_range_serialize(), BTLessStrategyNumber, build_distances(), BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_values, CurrentMemoryContext, fill_expanded_ranges(), Ranges::maxvalues, SerializedRanges::maxvalues, MemoryContextDelete(), MemoryContextSwitchTo(), merge_overlapping_ranges(), minmax_multi_get_procinfo(), minmax_multi_get_strategy_procinfo(), Ranges::nranges, Ranges::nvalues, palloc0(), pfree(), PG_DETOAST_DATUM, PG_GET_COLLATION, PG_GETARG_POINTER, PG_RETURN_VOID, PointerGetDatum(), PROCNUM_DISTANCE, reduce_expanded_ranges(), sort_expanded_ranges(), store_expanded_ranges(), and TupleDescAttr().

◆ brin_range_deserialize()

static Ranges * brin_range_deserialize ( int  maxvalues,
SerializedRanges serialized 
)
static

Definition at line 722 of file brin_minmax_multi.c.

723{
724 int i,
725 nvalues;
726 char *ptr,
727 *dataptr;
728 bool typbyval;
729 int typlen;
730 Size datalen;
731
732 Ranges *range;
733
734 Assert(serialized->nranges >= 0);
735 Assert(serialized->nvalues >= 0);
736 Assert(serialized->maxvalues > 0);
737
738 nvalues = 2 * serialized->nranges + serialized->nvalues;
739
740 Assert(nvalues <= serialized->maxvalues);
741 Assert(serialized->maxvalues <= maxvalues);
742
743 range = minmax_multi_init(maxvalues);
744
745 /* copy the header info */
746 range->nranges = serialized->nranges;
747 range->nvalues = serialized->nvalues;
748 range->nsorted = serialized->nvalues;
749 range->maxvalues = maxvalues;
750 range->target_maxvalues = serialized->maxvalues;
751
752 range->typid = serialized->typid;
753
754 typbyval = get_typbyval(serialized->typid);
755 typlen = get_typlen(serialized->typid);
756
757 /*
758 * And now deconstruct the values into Datum array. We have to copy the
759 * data because the serialized representation ignores alignment, and we
760 * don't want to rely on it being kept around anyway.
761 */
762 ptr = serialized->data;
763
764 /*
765 * We don't want to allocate many pieces, so we just allocate everything
766 * in one chunk. How much space will we need?
767 *
768 * XXX We don't need to copy simple by-value data types.
769 */
770 datalen = 0;
771 dataptr = NULL;
772 for (i = 0; (i < nvalues) && (!typbyval); i++)
773 {
774 if (typlen > 0) /* fixed-length by-ref types */
775 datalen += MAXALIGN(typlen);
776 else if (typlen == -1) /* varlena */
777 {
778 datalen += MAXALIGN(VARSIZE_ANY(ptr));
779 ptr += VARSIZE_ANY(ptr);
780 }
781 else if (typlen == -2) /* cstring */
782 {
783 Size slen = strlen(ptr) + 1;
784
785 datalen += MAXALIGN(slen);
786 ptr += slen;
787 }
788 }
789
790 if (datalen > 0)
791 dataptr = palloc(datalen);
792
793 /*
794 * Restore the source pointer (might have been modified when calculating
795 * the space we need to allocate).
796 */
797 ptr = serialized->data;
798
799 for (i = 0; i < nvalues; i++)
800 {
801 if (typbyval) /* simple by-value data types */
802 {
803 Datum v = 0;
804
805 memcpy(&v, ptr, typlen);
806
807 range->values[i] = fetch_att(&v, true, typlen);
808 ptr += typlen;
809 }
810 else if (typlen > 0) /* fixed-length by-ref types */
811 {
812 range->values[i] = PointerGetDatum(dataptr);
813
814 memcpy(dataptr, ptr, typlen);
815 dataptr += MAXALIGN(typlen);
816
817 ptr += typlen;
818 }
819 else if (typlen == -1) /* varlena */
820 {
821 range->values[i] = PointerGetDatum(dataptr);
822
823 memcpy(dataptr, ptr, VARSIZE_ANY(ptr));
824 dataptr += MAXALIGN(VARSIZE_ANY(ptr));
825 ptr += VARSIZE_ANY(ptr);
826 }
827 else if (typlen == -2) /* cstring */
828 {
829 Size slen = strlen(ptr) + 1;
830
831 range->values[i] = PointerGetDatum(dataptr);
832
833 memcpy(dataptr, ptr, slen);
834 dataptr += MAXALIGN(slen);
835 ptr += slen;
836 }
837
838 /* make sure we haven't overflown the buffer end */
839 Assert(ptr <= ((char *) serialized + VARSIZE_ANY(serialized)));
840 }
841
842 /* should have consumed the whole input value exactly */
843 Assert(ptr == ((char *) serialized + VARSIZE_ANY(serialized)));
844
845 /* return the deserialized value */
846 return range;
847}
size_t Size
Definition: c.h:562
int16 get_typlen(Oid typid)
Definition: lsyscache.c:2224
bool get_typbyval(Oid typid)
Definition: lsyscache.c:2249
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
char data[FLEXIBLE_ARRAY_MEMBER]
int nranges
Definition: regguts.h:283
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition: tupmacs.h:53
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311

References Assert, SerializedRanges::data, fetch_att(), get_typbyval(), get_typlen(), i, MAXALIGN, SerializedRanges::maxvalues, minmax_multi_init(), SerializedRanges::nranges, cvec::nranges, SerializedRanges::nvalues, palloc(), PointerGetDatum(), range(), SerializedRanges::typid, and VARSIZE_ANY.

Referenced by brin_minmax_multi_add_value(), brin_minmax_multi_consistent(), brin_minmax_multi_summary_out(), and brin_minmax_multi_union().

◆ brin_range_serialize()

static SerializedRanges * brin_range_serialize ( Ranges range)
static

Definition at line 577 of file brin_minmax_multi.c.

578{
579 Size len;
580 int nvalues;
581 SerializedRanges *serialized;
582 Oid typid;
583 int typlen;
584 bool typbyval;
585
586 char *ptr;
587
588 /* simple sanity checks */
589 Assert(range->nranges >= 0);
590 Assert(range->nsorted >= 0);
591 Assert(range->nvalues >= 0);
592 Assert(range->maxvalues > 0);
593 Assert(range->target_maxvalues > 0);
594
595 /* at this point the range should be compacted to the target size */
596 Assert(2 * range->nranges + range->nvalues <= range->target_maxvalues);
597
598 Assert(range->target_maxvalues <= range->maxvalues);
599
600 /* range boundaries are always sorted */
601 Assert(range->nvalues >= range->nsorted);
602
603 /* deduplicate values, if there's unsorted part */
605
606 /* see how many Datum values we actually have */
607 nvalues = 2 * range->nranges + range->nvalues;
608
609 typid = range->typid;
610 typbyval = get_typbyval(typid);
611 typlen = get_typlen(typid);
612
613 /* header is always needed */
614 len = offsetof(SerializedRanges, data);
615
616 /*
617 * The space needed depends on data type - for fixed-length data types
618 * (by-value and some by-reference) it's pretty simple, just multiply
619 * (attlen * nvalues) and we're done. For variable-length by-reference
620 * types we need to actually walk all the values and sum the lengths.
621 */
622 if (typlen == -1) /* varlena */
623 {
624 int i;
625
626 for (i = 0; i < nvalues; i++)
627 {
628 len += VARSIZE_ANY(range->values[i]);
629 }
630 }
631 else if (typlen == -2) /* cstring */
632 {
633 int i;
634
635 for (i = 0; i < nvalues; i++)
636 {
637 /* don't forget to include the null terminator ;-) */
638 len += strlen(DatumGetCString(range->values[i])) + 1;
639 }
640 }
641 else /* fixed-length types (even by-reference) */
642 {
643 Assert(typlen > 0);
644 len += nvalues * typlen;
645 }
646
647 /*
648 * Allocate the serialized object, copy the basic information. The
649 * serialized object is a varlena, so update the header.
650 */
651 serialized = (SerializedRanges *) palloc0(len);
652 SET_VARSIZE(serialized, len);
653
654 serialized->typid = typid;
655 serialized->nranges = range->nranges;
656 serialized->nvalues = range->nvalues;
657 serialized->maxvalues = range->target_maxvalues;
658
659 /*
660 * And now copy also the boundary values (like the length calculation this
661 * depends on the particular data type).
662 */
663 ptr = serialized->data; /* start of the serialized data */
664
665 for (int i = 0; i < nvalues; i++)
666 {
667 if (typbyval) /* simple by-value data types */
668 {
669 Datum tmp;
670
671 /*
672 * For byval types, we need to copy just the significant bytes -
673 * we can't use memcpy directly, as that assumes little-endian
674 * behavior. store_att_byval does almost what we need, but it
675 * requires a properly aligned buffer - the output buffer does not
676 * guarantee that. So we simply use a local Datum variable (which
677 * guarantees proper alignment), and then copy the value from it.
678 */
679 store_att_byval(&tmp, range->values[i], typlen);
680
681 memcpy(ptr, &tmp, typlen);
682 ptr += typlen;
683 }
684 else if (typlen > 0) /* fixed-length by-ref types */
685 {
686 memcpy(ptr, DatumGetPointer(range->values[i]), typlen);
687 ptr += typlen;
688 }
689 else if (typlen == -1) /* varlena */
690 {
691 int tmp = VARSIZE_ANY(DatumGetPointer(range->values[i]));
692
693 memcpy(ptr, DatumGetPointer(range->values[i]), tmp);
694 ptr += tmp;
695 }
696 else if (typlen == -2) /* cstring */
697 {
698 int tmp = strlen(DatumGetCString(range->values[i])) + 1;
699
700 memcpy(ptr, DatumGetCString(range->values[i]), tmp);
701 ptr += tmp;
702 }
703
704 /* make sure we haven't overflown the buffer end */
705 Assert(ptr <= ((char *) serialized + len));
706 }
707
708 /* exact size */
709 Assert(ptr == ((char *) serialized + len));
710
711 return serialized;
712}
static void range_deduplicate_values(Ranges *range)
const void * data
static void store_att_byval(void *T, Datum newdatum, int attlen)
Definition: tupmacs.h:211
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305

References Assert, SerializedRanges::data, data, DatumGetCString(), DatumGetPointer(), get_typbyval(), get_typlen(), i, len, SerializedRanges::maxvalues, SerializedRanges::nranges, cvec::nranges, SerializedRanges::nvalues, palloc0(), range(), range_deduplicate_values(), SET_VARSIZE, store_att_byval(), SerializedRanges::typid, and VARSIZE_ANY.

Referenced by brin_minmax_multi_serialize(), and brin_minmax_multi_union().

◆ build_distances()

static DistanceValue * build_distances ( FmgrInfo distanceFn,
Oid  colloid,
ExpandedRange eranges,
int  neranges 
)
static

Definition at line 1330 of file brin_minmax_multi.c.

1332{
1333 int i;
1334 int ndistances;
1335 DistanceValue *distances;
1336
1337 Assert(neranges > 0);
1338
1339 /* If there's only a single range, there's no distance to calculate. */
1340 if (neranges == 1)
1341 return NULL;
1342
1343 ndistances = (neranges - 1);
1344 distances = (DistanceValue *) palloc0(sizeof(DistanceValue) * ndistances);
1345
1346 /*
1347 * Walk through the ranges once and compute the distance between the
1348 * ranges so that we can sort them once.
1349 */
1350 for (i = 0; i < ndistances; i++)
1351 {
1352 Datum a1,
1353 a2,
1354 r;
1355
1356 a1 = eranges[i].maxval;
1357 a2 = eranges[i + 1].minval;
1358
1359 /* compute length of the gap (between max/min) */
1360 r = FunctionCall2Coll(distanceFn, colloid, a1, a2);
1361
1362 /* remember the index of the gap the distance is for */
1363 distances[i].index = i;
1364 distances[i].value = DatumGetFloat8(r);
1365 }
1366
1367 /*
1368 * Sort the distances in descending order, so that the longest gaps are at
1369 * the front.
1370 */
1371 qsort(distances, ndistances, sizeof(DistanceValue), compare_distances);
1372
1373 return distances;
1374}
static int compare_distances(const void *a, const void *b)
#define qsort(a, b, c, d)
Definition: port.h:475
static float8 DatumGetFloat8(Datum X)
Definition: postgres.h:499

References a1, a2, Assert, compare_distances(), DatumGetFloat8(), FunctionCall2Coll(), i, DistanceValue::index, ExpandedRange::maxval, ExpandedRange::minval, palloc0(), qsort, and DistanceValue::value.

Referenced by brin_minmax_multi_union(), compactify_ranges(), and ensure_free_space_in_buffer().

◆ build_expanded_ranges()

static ExpandedRange * build_expanded_ranges ( FmgrInfo cmp,
Oid  colloid,
Ranges ranges,
int *  nranges 
)
static

Definition at line 1387 of file brin_minmax_multi.c.

1389{
1390 int neranges;
1391 ExpandedRange *eranges;
1392
1393 /* both ranges and points are expanded into a separate element */
1394 neranges = ranges->nranges + ranges->nvalues;
1395
1396 eranges = (ExpandedRange *) palloc0(neranges * sizeof(ExpandedRange));
1397
1398 /* fill the expanded ranges */
1399 fill_expanded_ranges(eranges, neranges, ranges);
1400
1401 /* sort and deduplicate the expanded ranges */
1402 neranges = sort_expanded_ranges(cmp, colloid, eranges, neranges);
1403
1404 /* remember how many ranges we built */
1405 *nranges = neranges;
1406
1407 return eranges;
1408}
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743

References cmp(), fill_expanded_ranges(), Ranges::nranges, Ranges::nvalues, palloc0(), and sort_expanded_ranges().

Referenced by compactify_ranges(), and ensure_free_space_in_buffer().

◆ compactify_ranges()

static void compactify_ranges ( BrinDesc bdesc,
Ranges ranges,
int  max_values 
)
static

Definition at line 1789 of file brin_minmax_multi.c.

1790{
1791 FmgrInfo *cmpFn,
1792 *distanceFn;
1793
1794 /* expanded ranges */
1795 ExpandedRange *eranges;
1796 int neranges;
1797 DistanceValue *distances;
1798
1799 MemoryContext ctx;
1800 MemoryContext oldctx;
1801
1802 /*
1803 * Do we need to actually compactify anything?
1804 *
1805 * There are two reasons why compaction may be needed - firstly, there may
1806 * be too many values, or some of the values may be unsorted.
1807 */
1808 if ((ranges->nranges * 2 + ranges->nvalues <= max_values) &&
1809 (ranges->nsorted == ranges->nvalues))
1810 return;
1811
1812 /* we'll certainly need the comparator, so just look it up now */
1813 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, ranges->attno, ranges->typid,
1815
1816 /* and we'll also need the 'distance' procedure */
1817 distanceFn = minmax_multi_get_procinfo(bdesc, ranges->attno, PROCNUM_DISTANCE);
1818
1819 /*
1820 * The distanceFn calls (which may internally call e.g. numeric_le) may
1821 * allocate quite a bit of memory, and we must not leak it. Otherwise,
1822 * we'd have problems e.g. when building indexes. So we create a local
1823 * memory context and make sure we free the memory before leaving this
1824 * function (not after every call).
1825 */
1827 "minmax-multi context",
1829
1830 oldctx = MemoryContextSwitchTo(ctx);
1831
1832 /* build the expanded ranges */
1833 eranges = build_expanded_ranges(cmpFn, ranges->colloid, ranges, &neranges);
1834
1835 /* build array of gap distances and sort them in ascending order */
1836 distances = build_distances(distanceFn, ranges->colloid,
1837 eranges, neranges);
1838
1839 /*
1840 * Combine ranges until we get below max_values. We don't use any scale
1841 * factor, because this is used during serialization, and we don't expect
1842 * more tuples to be inserted anytime soon.
1843 */
1844 neranges = reduce_expanded_ranges(eranges, neranges, distances,
1845 max_values, cmpFn, ranges->colloid);
1846
1847 Assert(count_values(eranges, neranges) <= max_values);
1848
1849 /* transform back into regular ranges and single values */
1850 store_expanded_ranges(ranges, eranges, neranges);
1851
1852 /* check all the range invariants */
1853 AssertCheckRanges(ranges, cmpFn, ranges->colloid);
1854
1855 MemoryContextSwitchTo(oldctx);
1857}
static ExpandedRange * build_expanded_ranges(FmgrInfo *cmp, Oid colloid, Ranges *ranges, int *nranges)
static void AssertCheckRanges(Ranges *ranges, FmgrInfo *cmpFn, Oid colloid)

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AssertCheckRanges(), Ranges::attno, BTLessStrategyNumber, build_distances(), build_expanded_ranges(), Ranges::colloid, CurrentMemoryContext, MemoryContextDelete(), MemoryContextSwitchTo(), minmax_multi_get_procinfo(), minmax_multi_get_strategy_procinfo(), Ranges::nranges, Ranges::nsorted, Ranges::nvalues, PROCNUM_DISTANCE, reduce_expanded_ranges(), store_expanded_ranges(), and Ranges::typid.

Referenced by brin_minmax_multi_serialize().

◆ compare_distances()

static int compare_distances ( const void *  a,
const void *  b 
)
static

Definition at line 1306 of file brin_minmax_multi.c.

1307{
1308 DistanceValue *da = (DistanceValue *) a;
1309 DistanceValue *db = (DistanceValue *) b;
1310
1311 if (da->value < db->value)
1312 return 1;
1313 else if (da->value > db->value)
1314 return -1;
1315
1316 return 0;
1317}

References a, b, and DistanceValue::value.

Referenced by build_distances().

◆ compare_expanded_ranges()

static int compare_expanded_ranges ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 859 of file brin_minmax_multi.c.

860{
861 ExpandedRange *ra = (ExpandedRange *) a;
862 ExpandedRange *rb = (ExpandedRange *) b;
863 Datum r;
864
866
867 /* first compare minvals */
868 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, ra->minval, rb->minval);
869
870 if (DatumGetBool(r))
871 return -1;
872
873 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, rb->minval, ra->minval);
874
875 if (DatumGetBool(r))
876 return 1;
877
878 /* then compare maxvals */
879 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, ra->maxval, rb->maxval);
880
881 if (DatumGetBool(r))
882 return -1;
883
884 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, rb->maxval, ra->maxval);
885
886 if (DatumGetBool(r))
887 return 1;
888
889 return 0;
890}
void * arg

References a, arg, b, compare_context::cmpFn, compare_context::colloid, DatumGetBool(), FunctionCall2Coll(), ExpandedRange::maxval, and ExpandedRange::minval.

Referenced by sort_expanded_ranges().

◆ compare_values()

static int compare_values ( const void *  a,
const void *  b,
void *  arg 
)
static

Definition at line 897 of file brin_minmax_multi.c.

898{
899 Datum *da = (Datum *) a;
900 Datum *db = (Datum *) b;
901 Datum r;
902
904
905 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, *da, *db);
906
907 if (DatumGetBool(r))
908 return -1;
909
910 r = FunctionCall2Coll(cxt->cmpFn, cxt->colloid, *db, *da);
911
912 if (DatumGetBool(r))
913 return 1;
914
915 return 0;
916}

References a, arg, b, compare_context::cmpFn, compare_context::colloid, DatumGetBool(), and FunctionCall2Coll().

Referenced by AssertCheckRanges(), range_contains_value(), range_deduplicate_values(), and reduce_expanded_ranges().

◆ ensure_free_space_in_buffer()

static bool ensure_free_space_in_buffer ( BrinDesc bdesc,
Oid  colloid,
AttrNumber  attno,
Form_pg_attribute  attr,
Ranges range 
)
static

Definition at line 1602 of file brin_minmax_multi.c.

1605{
1606 MemoryContext ctx;
1607 MemoryContext oldctx;
1608
1609 FmgrInfo *cmpFn,
1610 *distanceFn;
1611
1612 /* expanded ranges */
1613 ExpandedRange *eranges;
1614 int neranges;
1615 DistanceValue *distances;
1616
1617 /*
1618 * If there is free space in the buffer, we're done without having to
1619 * modify anything.
1620 */
1621 if (2 * range->nranges + range->nvalues < range->maxvalues)
1622 return false;
1623
1624 /* we'll certainly need the comparator, so just look it up now */
1625 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
1627
1628 /* deduplicate values, if there's an unsorted part */
1630
1631 /*
1632 * Did we reduce enough free space by just the deduplication?
1633 *
1634 * We don't simply check against range->maxvalues again. The deduplication
1635 * might have freed very little space (e.g. just one value), forcing us to
1636 * do deduplication very often. In that case, it's better to do the
1637 * compaction and reduce more space.
1638 */
1639 if (2 * range->nranges + range->nvalues <= range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR)
1640 return true;
1641
1642 /*
1643 * We need to combine some of the existing ranges, to reduce the number of
1644 * values we have to store.
1645 *
1646 * The distanceFn calls (which may internally call e.g. numeric_le) may
1647 * allocate quite a bit of memory, and we must not leak it (we might have
1648 * to do this repeatedly, even for a single BRIN page range). Otherwise
1649 * we'd have problems e.g. when building new indexes. So we use a memory
1650 * context and make sure we free the memory at the end (so if we call the
1651 * distance function many times, it might be an issue, but meh).
1652 */
1654 "minmax-multi context",
1656
1657 oldctx = MemoryContextSwitchTo(ctx);
1658
1659 /* build the expanded ranges */
1660 eranges = build_expanded_ranges(cmpFn, colloid, range, &neranges);
1661
1662 /* Is the expanded representation of ranges correct? */
1663 AssertCheckExpandedRanges(bdesc, colloid, attno, attr, eranges, neranges);
1664
1665 /* and we'll also need the 'distance' procedure */
1666 distanceFn = minmax_multi_get_procinfo(bdesc, attno, PROCNUM_DISTANCE);
1667
1668 /* build array of gap distances and sort them in ascending order */
1669 distances = build_distances(distanceFn, colloid, eranges, neranges);
1670
1671 /*
1672 * Combine ranges until we release at least 50% of the space. This
1673 * threshold is somewhat arbitrary, perhaps needs tuning. We must not use
1674 * too low or high value.
1675 */
1676 neranges = reduce_expanded_ranges(eranges, neranges, distances,
1677 range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR,
1678 cmpFn, colloid);
1679
1680 /* Is the result of reducing expanded ranges correct? */
1681 AssertCheckExpandedRanges(bdesc, colloid, attno, attr, eranges, neranges);
1682
1683 /* Make sure we've sufficiently reduced the number of ranges. */
1684 Assert(count_values(eranges, neranges) <= range->maxvalues * MINMAX_BUFFER_LOAD_FACTOR);
1685
1686 /* decompose the expanded ranges into regular ranges and single values */
1687 store_expanded_ranges(range, eranges, neranges);
1688
1689 MemoryContextSwitchTo(oldctx);
1691
1692 /* Did we break the ranges somehow? */
1693 AssertCheckRanges(range, cmpFn, colloid);
1694
1695 return true;
1696}
#define MINMAX_BUFFER_LOAD_FACTOR

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AssertCheckExpandedRanges(), AssertCheckRanges(), BTLessStrategyNumber, build_distances(), build_expanded_ranges(), CurrentMemoryContext, MemoryContextDelete(), MemoryContextSwitchTo(), MINMAX_BUFFER_LOAD_FACTOR, minmax_multi_get_procinfo(), minmax_multi_get_strategy_procinfo(), cvec::nranges, PROCNUM_DISTANCE, range(), range_deduplicate_values(), reduce_expanded_ranges(), and store_expanded_ranges().

Referenced by range_add_value().

◆ fill_expanded_ranges()

static void fill_expanded_ranges ( ExpandedRange eranges,
int  neranges,
Ranges ranges 
)
static

Definition at line 1135 of file brin_minmax_multi.c.

1136{
1137 int idx;
1138 int i;
1139
1140 /* Check that the output array has the right size. */
1141 Assert(neranges == (ranges->nranges + ranges->nvalues));
1142
1143 idx = 0;
1144 for (i = 0; i < ranges->nranges; i++)
1145 {
1146 eranges[idx].minval = ranges->values[2 * i];
1147 eranges[idx].maxval = ranges->values[2 * i + 1];
1148 eranges[idx].collapsed = false;
1149 idx++;
1150
1151 Assert(idx <= neranges);
1152 }
1153
1154 for (i = 0; i < ranges->nvalues; i++)
1155 {
1156 eranges[idx].minval = ranges->values[2 * ranges->nranges + i];
1157 eranges[idx].maxval = ranges->values[2 * ranges->nranges + i];
1158 eranges[idx].collapsed = true;
1159 idx++;
1160
1161 Assert(idx <= neranges);
1162 }
1163
1164 /* Did we produce the expected number of elements? */
1165 Assert(idx == neranges);
1166
1167 return;
1168}

References Assert, ExpandedRange::collapsed, i, idx(), ExpandedRange::maxval, ExpandedRange::minval, Ranges::nranges, Ranges::nvalues, and Ranges::values.

Referenced by brin_minmax_multi_union(), and build_expanded_ranges().

◆ has_matching_range()

static bool has_matching_range ( BrinDesc bdesc,
Oid  colloid,
Ranges ranges,
Datum  newval,
AttrNumber  attno,
Oid  typid 
)
static

Definition at line 922 of file brin_minmax_multi.c.

924{
925 Datum compar;
926
927 Datum minvalue;
928 Datum maxvalue;
929
930 FmgrInfo *cmpLessFn;
931 FmgrInfo *cmpGreaterFn;
932
933 /* binary search on ranges */
934 int start,
935 end;
936
937 if (ranges->nranges == 0)
938 return false;
939
940 minvalue = ranges->values[0];
941 maxvalue = ranges->values[2 * ranges->nranges - 1];
942
943 /*
944 * Otherwise, need to compare the new value with boundaries of all the
945 * ranges. First check if it's less than the absolute minimum, which is
946 * the first value in the array.
947 */
948 cmpLessFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
950 compar = FunctionCall2Coll(cmpLessFn, colloid, newval, minvalue);
951
952 /* smaller than the smallest value in the range list */
953 if (DatumGetBool(compar))
954 return false;
955
956 /*
957 * And now compare it to the existing maximum (last value in the data
958 * array). But only if we haven't already ruled out a possible match in
959 * the minvalue check.
960 */
961 cmpGreaterFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
963 compar = FunctionCall2Coll(cmpGreaterFn, colloid, newval, maxvalue);
964
965 if (DatumGetBool(compar))
966 return false;
967
968 /*
969 * So we know it's in the general min/max, the question is whether it
970 * falls in one of the ranges or gaps. We'll do a binary search on
971 * individual ranges - for each range we check equality (value falls into
972 * the range), and then check ranges either above or below the current
973 * range.
974 */
975 start = 0; /* first range */
976 end = (ranges->nranges - 1); /* last range */
977 while (true)
978 {
979 int midpoint = (start + end) / 2;
980
981 /* this means we ran out of ranges in the last step */
982 if (start > end)
983 return false;
984
985 /* copy the min/max values from the ranges */
986 minvalue = ranges->values[2 * midpoint];
987 maxvalue = ranges->values[2 * midpoint + 1];
988
989 /*
990 * Is the value smaller than the minval? If yes, we'll recurse to the
991 * left side of range array.
992 */
993 compar = FunctionCall2Coll(cmpLessFn, colloid, newval, minvalue);
994
995 /* smaller than the smallest value in this range */
996 if (DatumGetBool(compar))
997 {
998 end = (midpoint - 1);
999 continue;
1000 }
1001
1002 /*
1003 * Is the value greater than the minval? If yes, we'll recurse to the
1004 * right side of range array.
1005 */
1006 compar = FunctionCall2Coll(cmpGreaterFn, colloid, newval, maxvalue);
1007
1008 /* larger than the largest value in this range */
1009 if (DatumGetBool(compar))
1010 {
1011 start = (midpoint + 1);
1012 continue;
1013 }
1014
1015 /* hey, we found a matching range */
1016 return true;
1017 }
1018
1019 return false;
1020}

References BTGreaterStrategyNumber, BTLessStrategyNumber, DatumGetBool(), FunctionCall2Coll(), minmax_multi_get_strategy_procinfo(), newval, Ranges::nranges, start, and Ranges::values.

Referenced by range_contains_value().

◆ merge_overlapping_ranges()

static int merge_overlapping_ranges ( FmgrInfo cmp,
Oid  colloid,
ExpandedRange eranges,
int  neranges 
)
static

Definition at line 1232 of file brin_minmax_multi.c.

1234{
1235 int idx;
1236
1237 /* Merge ranges (idx) and (idx+1) if they overlap. */
1238 idx = 0;
1239 while (idx < (neranges - 1))
1240 {
1241 Datum r;
1242
1243 /*
1244 * comparing [?,maxval] vs. [minval,?] - the ranges overlap if (minval
1245 * < maxval)
1246 */
1247 r = FunctionCall2Coll(cmp, colloid,
1248 eranges[idx].maxval,
1249 eranges[idx + 1].minval);
1250
1251 /*
1252 * Nope, maxval < minval, so no overlap. And we know the ranges are
1253 * ordered, so there are no more overlaps, because all the remaining
1254 * ranges have greater or equal minval.
1255 */
1256 if (DatumGetBool(r))
1257 {
1258 /* proceed to the next range */
1259 idx += 1;
1260 continue;
1261 }
1262
1263 /*
1264 * So ranges 'idx' and 'idx+1' do overlap, but we don't know if
1265 * 'idx+1' is contained in 'idx', or if they overlap only partially.
1266 * So compare the upper bounds and keep the larger one.
1267 */
1268 r = FunctionCall2Coll(cmp, colloid,
1269 eranges[idx].maxval,
1270 eranges[idx + 1].maxval);
1271
1272 if (DatumGetBool(r))
1273 eranges[idx].maxval = eranges[idx + 1].maxval;
1274
1275 /*
1276 * The range certainly is no longer collapsed (irrespectively of the
1277 * previous state).
1278 */
1279 eranges[idx].collapsed = false;
1280
1281 /*
1282 * Now get rid of the (idx+1) range entirely by shifting the remaining
1283 * ranges by 1. There are neranges elements, and we need to move
1284 * elements from (idx+2). That means the number of elements to move is
1285 * [ncranges - (idx+2)].
1286 */
1287 memmove(&eranges[idx + 1], &eranges[idx + 2],
1288 (neranges - (idx + 2)) * sizeof(ExpandedRange));
1289
1290 /*
1291 * Decrease the number of ranges, and repeat (with the same range, as
1292 * it might overlap with additional ranges thanks to the merge).
1293 */
1294 neranges--;
1295 }
1296
1297 return neranges;
1298}

References cmp(), ExpandedRange::collapsed, DatumGetBool(), FunctionCall2Coll(), idx(), and ExpandedRange::maxval.

Referenced by brin_minmax_multi_union().

◆ minmax_multi_get_procinfo()

static FmgrInfo * minmax_multi_get_procinfo ( BrinDesc bdesc,
uint16  attno,
uint16  procnum 
)
static

Definition at line 2864 of file brin_minmax_multi.c.

2865{
2866 MinmaxMultiOpaque *opaque;
2867 uint16 basenum = procnum - PROCNUM_BASE;
2868
2869 /*
2870 * We cache these in the opaque struct, to avoid repetitive syscache
2871 * lookups.
2872 */
2873 opaque = (MinmaxMultiOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
2874
2875 /*
2876 * If we already searched for this proc and didn't find it, don't bother
2877 * searching again.
2878 */
2879 if (opaque->extra_proc_missing[basenum])
2880 return NULL;
2881
2882 if (opaque->extra_procinfos[basenum].fn_oid == InvalidOid)
2883 {
2885 procnum)))
2886 {
2887 fmgr_info_copy(&opaque->extra_procinfos[basenum],
2888 index_getprocinfo(bdesc->bd_index, attno, procnum),
2889 bdesc->bd_context);
2890 }
2891 else
2892 {
2893 opaque->extra_proc_missing[basenum] = true;
2894 return NULL;
2895 }
2896 }
2897
2898 return &opaque->extra_procinfos[basenum];
2899}
#define PROCNUM_BASE
#define RegProcedureIsValid(p)
Definition: c.h:734
uint16_t uint16
Definition: c.h:487
void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo, MemoryContext destcxt)
Definition: fmgr.c:580
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:862
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition: indexam.c:828
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:76
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
MemoryContext bd_context
Definition: brin_internal.h:47
Oid fn_oid
Definition: fmgr.h:59
bool extra_proc_missing[MINMAX_MAX_PROCNUMS]
FmgrInfo extra_procinfos[MINMAX_MAX_PROCNUMS]

References BrinDesc::bd_context, BrinDesc::bd_index, BrinDesc::bd_info, MinmaxMultiOpaque::extra_proc_missing, MinmaxMultiOpaque::extra_procinfos, fmgr_info_copy(), FmgrInfo::fn_oid, if(), index_getprocid(), index_getprocinfo(), InvalidOid, BrinOpcInfo::oi_opaque, PROCNUM_BASE, and RegProcedureIsValid.

Referenced by brin_minmax_multi_union(), compactify_ranges(), and ensure_free_space_in_buffer().

◆ minmax_multi_get_strategy_procinfo()

static FmgrInfo * minmax_multi_get_strategy_procinfo ( BrinDesc bdesc,
uint16  attno,
Oid  subtype,
uint16  strategynum 
)
static

Definition at line 2908 of file brin_minmax_multi.c.

2910{
2911 MinmaxMultiOpaque *opaque;
2912
2913 Assert(strategynum >= 1 &&
2914 strategynum <= BTMaxStrategyNumber);
2915
2916 opaque = (MinmaxMultiOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
2917
2918 /*
2919 * We cache the procedures for the previous subtype in the opaque struct,
2920 * to avoid repetitive syscache lookups. If the subtype changed,
2921 * invalidate all the cached entries.
2922 */
2923 if (opaque->cached_subtype != subtype)
2924 {
2925 uint16 i;
2926
2927 for (i = 1; i <= BTMaxStrategyNumber; i++)
2928 opaque->strategy_procinfos[i - 1].fn_oid = InvalidOid;
2929 opaque->cached_subtype = subtype;
2930 }
2931
2932 if (opaque->strategy_procinfos[strategynum - 1].fn_oid == InvalidOid)
2933 {
2934 Form_pg_attribute attr;
2935 HeapTuple tuple;
2936 Oid opfamily,
2937 oprid;
2938
2939 opfamily = bdesc->bd_index->rd_opfamily[attno - 1];
2940 attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
2941 tuple = SearchSysCache4(AMOPSTRATEGY, ObjectIdGetDatum(opfamily),
2942 ObjectIdGetDatum(attr->atttypid),
2943 ObjectIdGetDatum(subtype),
2944 Int16GetDatum(strategynum));
2945 if (!HeapTupleIsValid(tuple))
2946 elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
2947 strategynum, attr->atttypid, subtype, opfamily);
2948
2949 oprid = DatumGetObjectId(SysCacheGetAttrNotNull(AMOPSTRATEGY, tuple,
2950 Anum_pg_amop_amopopr));
2951 ReleaseSysCache(tuple);
2953
2955 &opaque->strategy_procinfos[strategynum - 1],
2956 bdesc->bd_context);
2957 }
2958
2959 return &opaque->strategy_procinfos[strategynum - 1];
2960}
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1312
Oid oprid(Operator op)
Definition: parse_oper.c:238
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:247
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:177
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
#define BTMaxStrategyNumber
Definition: stratnum.h:35
FmgrInfo strategy_procinfos[BTMaxStrategyNumber]
Oid * rd_opfamily
Definition: rel.h:207
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache4(int cacheId, Datum key1, Datum key2, Datum key3, Datum key4)
Definition: syscache.c:254
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631

References Assert, BrinDesc::bd_context, BrinDesc::bd_index, BrinDesc::bd_info, BrinDesc::bd_tupdesc, BTMaxStrategyNumber, MinmaxMultiOpaque::cached_subtype, DatumGetObjectId(), elog, ERROR, fmgr_info_cxt(), FmgrInfo::fn_oid, get_opcode(), HeapTupleIsValid, i, if(), Int16GetDatum(), InvalidOid, ObjectIdGetDatum(), BrinOpcInfo::oi_opaque, oprid(), RelationData::rd_opfamily, RegProcedureIsValid, ReleaseSysCache(), SearchSysCache4(), MinmaxMultiOpaque::strategy_procinfos, SysCacheGetAttrNotNull(), and TupleDescAttr().

Referenced by AssertCheckExpandedRanges(), brin_minmax_multi_add_value(), brin_minmax_multi_consistent(), brin_minmax_multi_union(), compactify_ranges(), ensure_free_space_in_buffer(), has_matching_range(), range_add_value(), and range_contains_value().

◆ minmax_multi_init()

static Ranges * minmax_multi_init ( int  maxvalues)
static

Definition at line 487 of file brin_minmax_multi.c.

488{
489 Size len;
490 Ranges *ranges;
491
492 Assert(maxvalues > 0);
493
494 len = offsetof(Ranges, values); /* fixed header */
495 len += maxvalues * sizeof(Datum); /* Datum values */
496
497 ranges = (Ranges *) palloc0(len);
498
499 ranges->maxvalues = maxvalues;
500
501 return ranges;
502}
static Datum values[MAXATTR]
Definition: bootstrap.c:151

References Assert, len, Ranges::maxvalues, palloc0(), and values.

Referenced by brin_minmax_multi_add_value(), and brin_range_deserialize().

◆ range_add_value()

static bool range_add_value ( BrinDesc bdesc,
Oid  colloid,
AttrNumber  attno,
Form_pg_attribute  attr,
Ranges ranges,
Datum  newval 
)
static

Definition at line 1703 of file brin_minmax_multi.c.

1706{
1707 FmgrInfo *cmpFn;
1708 bool modified = false;
1709
1710 /* we'll certainly need the comparator, so just look it up now */
1711 cmpFn = minmax_multi_get_strategy_procinfo(bdesc, attno, attr->atttypid,
1713
1714 /* comprehensive checks of the input ranges */
1715 AssertCheckRanges(ranges, cmpFn, colloid);
1716
1717 /*
1718 * Make sure there's enough free space in the buffer. We only trigger this
1719 * when the buffer is full, which means it had to be modified as we size
1720 * it to be larger than what is stored on disk.
1721 *
1722 * This needs to happen before we check if the value is contained in the
1723 * range, because the value might be in the unsorted part, and we don't
1724 * check that in range_contains_value. The deduplication would then move
1725 * it to the sorted part, and we'd add the value too, which violates the
1726 * rule that we never have duplicates with the ranges or sorted values.
1727 *
1728 * We might also deduplicate and recheck if the value is contained, but
1729 * that seems like overkill. We'd need to deduplicate anyway, so why not
1730 * do it now.
1731 */
1732 modified = ensure_free_space_in_buffer(bdesc, colloid,
1733 attno, attr, ranges);
1734
1735 /*
1736 * Bail out if the value already is covered by the range.
1737 *
1738 * We could also add values until we hit values_per_range, and then do the
1739 * deduplication in a batch, hoping for better efficiency. But that would
1740 * mean we actually modify the range every time, which means having to
1741 * serialize the value, which does palloc, walks the values, copies them,
1742 * etc. Not exactly cheap.
1743 *
1744 * So instead we do the check, which should be fairly cheap - assuming the
1745 * comparator function is not very expensive.
1746 *
1747 * This also implies the values array can't contain duplicate values.
1748 */
1749 if (range_contains_value(bdesc, colloid, attno, attr, ranges, newval, false))
1750 return modified;
1751
1752 /* Make a copy of the value, if needed. */
1753 newval = datumCopy(newval, attr->attbyval, attr->attlen);
1754
1755 /*
1756 * If there's space in the values array, copy it in and we're done.
1757 *
1758 * We do want to keep the values sorted (to speed up searches), so we do a
1759 * simple insertion sort. We could do something more elaborate, e.g. by
1760 * sorting the values only now and then, but for small counts (e.g. when
1761 * maxvalues is 64) this should be fine.
1762 */
1763 ranges->values[2 * ranges->nranges + ranges->nvalues] = newval;
1764 ranges->nvalues++;
1765
1766 /* If we added the first value, we can consider it as sorted. */
1767 if (ranges->nvalues == 1)
1768 ranges->nsorted = 1;
1769
1770 /*
1771 * Check we haven't broken the ordering of boundary values (checks both
1772 * parts, but that doesn't hurt).
1773 */
1774 AssertCheckRanges(ranges, cmpFn, colloid);
1775
1776 /* Check the range contains the value we just added. */
1777 Assert(range_contains_value(bdesc, colloid, attno, attr, ranges, newval, true));
1778
1779 /* yep, we've modified the range */
1780 return true;
1781}
static bool ensure_free_space_in_buffer(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *range)
static bool range_contains_value(BrinDesc *bdesc, Oid colloid, AttrNumber attno, Form_pg_attribute attr, Ranges *ranges, Datum newval, bool full)
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132

References Assert, AssertCheckRanges(), BTLessStrategyNumber, datumCopy(), ensure_free_space_in_buffer(), minmax_multi_get_strategy_procinfo(), newval, Ranges::nranges, Ranges::nsorted, Ranges::nvalues, range_contains_value(), and Ranges::values.

Referenced by brin_minmax_multi_add_value().

◆ range_contains_value()

static bool range_contains_value ( BrinDesc bdesc,
Oid  colloid,
AttrNumber  attno,
Form_pg_attribute  attr,
Ranges ranges,
Datum  newval,
bool  full 
)
static

Definition at line 1046 of file brin_minmax_multi.c.

1049{
1050 int i;
1051 FmgrInfo *cmpEqualFn;
1052 Oid typid = attr->atttypid;
1053
1054 /*
1055 * First inspect the ranges, if there are any. We first check the whole
1056 * range, and only when there's still a chance of getting a match we
1057 * inspect the individual ranges.
1058 */
1059 if (has_matching_range(bdesc, colloid, ranges, newval, attno, typid))
1060 return true;
1061
1062 cmpEqualFn = minmax_multi_get_strategy_procinfo(bdesc, attno, typid,
1064
1065 /*
1066 * There is no matching range, so let's inspect the sorted values.
1067 *
1068 * We do a sequential search for small numbers of values, and binary
1069 * search once we have more than 16 values. This threshold is somewhat
1070 * arbitrary, as it depends on how expensive the comparison function is.
1071 *
1072 * XXX If we use the threshold here, maybe we should do the same thing in
1073 * has_matching_range? Or maybe we should do the bin search all the time?
1074 *
1075 * XXX We could use the same optimization as for ranges, to check if the
1076 * value is between min/max, to maybe rule out all sorted values without
1077 * having to inspect all of them.
1078 */
1079 if (ranges->nsorted >= 16)
1080 {
1081 compare_context cxt;
1082
1083 cxt.colloid = ranges->colloid;
1084 cxt.cmpFn = ranges->cmp;
1085
1086 if (bsearch_arg(&newval, &ranges->values[2 * ranges->nranges],
1087 ranges->nsorted, sizeof(Datum),
1088 compare_values, &cxt) != NULL)
1089 return true;
1090 }
1091 else
1092 {
1093 for (i = 2 * ranges->nranges; i < 2 * ranges->nranges + ranges->nsorted; i++)
1094 {
1095 Datum compar;
1096
1097 compar = FunctionCall2Coll(cmpEqualFn, colloid, newval, ranges->values[i]);
1098
1099 /* found an exact match */
1100 if (DatumGetBool(compar))
1101 return true;
1102 }
1103 }
1104
1105 /* If not asked to inspect the unsorted part, we're done. */
1106 if (!full)
1107 return false;
1108
1109 /* Inspect the unsorted part. */
1110 for (i = 2 * ranges->nranges + ranges->nsorted; i < 2 * ranges->nranges + ranges->nvalues; i++)
1111 {
1112 Datum compar;
1113
1114 compar = FunctionCall2Coll(cmpEqualFn, colloid, newval, ranges->values[i]);
1115
1116 /* found an exact match */
1117 if (DatumGetBool(compar))
1118 return true;
1119 }
1120
1121 /* the value is not covered by this BRIN tuple */
1122 return false;
1123}
static bool has_matching_range(BrinDesc *bdesc, Oid colloid, Ranges *ranges, Datum newval, AttrNumber attno, Oid typid)

References bsearch_arg(), BTEqualStrategyNumber, Ranges::cmp, compare_context::cmpFn, Ranges::colloid, compare_context::colloid, compare_values(), DatumGetBool(), FunctionCall2Coll(), has_matching_range(), i, minmax_multi_get_strategy_procinfo(), newval, Ranges::nranges, Ranges::nsorted, Ranges::nvalues, and Ranges::values.

Referenced by range_add_value().

◆ range_deduplicate_values()

static void range_deduplicate_values ( Ranges range)
static

Definition at line 517 of file brin_minmax_multi.c.

518{
519 int i,
520 n;
521 int start;
522 compare_context cxt;
523
524 /*
525 * If there are no unsorted values, we're done (this probably can't
526 * happen, as we're adding values to unsorted part).
527 */
528 if (range->nsorted == range->nvalues)
529 return;
530
531 /* sort the values */
532 cxt.colloid = range->colloid;
533 cxt.cmpFn = range->cmp;
534
535 /* the values start right after the ranges (which are always sorted) */
536 start = 2 * range->nranges;
537
538 /*
539 * XXX This might do a merge sort, to leverage that the first part of the
540 * array is already sorted. If the sorted part is large, it might be quite
541 * a bit faster.
542 */
543 qsort_arg(&range->values[start],
544 range->nvalues, sizeof(Datum),
545 compare_values, &cxt);
546
547 n = 1;
548 for (i = 1; i < range->nvalues; i++)
549 {
550 /* same as preceding value, so store it */
551 if (compare_values(&range->values[start + i - 1],
552 &range->values[start + i],
553 &cxt) == 0)
554 continue;
555
556 range->values[start + n] = range->values[start + i];
557
558 n++;
559 }
560
561 /* now all the values are sorted */
562 range->nvalues = n;
563 range->nsorted = n;
564
565 AssertCheckRanges(range, range->cmp, range->colloid);
566}
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)

References AssertCheckRanges(), compare_context::cmpFn, compare_context::colloid, compare_values(), i, cvec::nranges, qsort_arg(), range(), and start.

Referenced by brin_range_serialize(), and ensure_free_space_in_buffer().

◆ reduce_expanded_ranges()

static int reduce_expanded_ranges ( ExpandedRange eranges,
int  neranges,
DistanceValue distances,
int  max_values,
FmgrInfo cmp,
Oid  colloid 
)
static

Definition at line 1477 of file brin_minmax_multi.c.

1480{
1481 int i;
1482 int nvalues;
1483 Datum *values;
1484
1485 compare_context cxt;
1486
1487 /* total number of gaps between ranges */
1488 int ndistances = (neranges - 1);
1489
1490 /* number of gaps to keep */
1491 int keep = (max_values / 2 - 1);
1492
1493 /*
1494 * Maybe we have a sufficiently low number of ranges already?
1495 *
1496 * XXX This should happen before we actually do the expensive stuff like
1497 * sorting, so maybe this should be just an assert.
1498 */
1499 if (keep >= ndistances)
1500 return neranges;
1501
1502 /* sort the values */
1503 cxt.colloid = colloid;
1504 cxt.cmpFn = cmp;
1505
1506 /* allocate space for the boundary values */
1507 nvalues = 0;
1508 values = (Datum *) palloc(sizeof(Datum) * max_values);
1509
1510 /* add the global min/max values, from the first/last range */
1511 values[nvalues++] = eranges[0].minval;
1512 values[nvalues++] = eranges[neranges - 1].maxval;
1513
1514 /* add boundary values for enough gaps */
1515 for (i = 0; i < keep; i++)
1516 {
1517 /* index of the gap between (index) and (index+1) ranges */
1518 int index = distances[i].index;
1519
1520 Assert((index >= 0) && ((index + 1) < neranges));
1521
1522 /* add max from the preceding range, minval from the next one */
1523 values[nvalues++] = eranges[index].maxval;
1524 values[nvalues++] = eranges[index + 1].minval;
1525
1526 Assert(nvalues <= max_values);
1527 }
1528
1529 /* We should have an even number of range values. */
1530 Assert(nvalues % 2 == 0);
1531
1532 /*
1533 * Sort the values using the comparator function, and form ranges from the
1534 * sorted result.
1535 */
1536 qsort_arg(values, nvalues, sizeof(Datum),
1537 compare_values, &cxt);
1538
1539 /* We have nvalues boundary values, which means nvalues/2 ranges. */
1540 for (i = 0; i < (nvalues / 2); i++)
1541 {
1542 eranges[i].minval = values[2 * i];
1543 eranges[i].maxval = values[2 * i + 1];
1544
1545 /* if the boundary values are the same, it's a collapsed range */
1546 eranges[i].collapsed = (compare_values(&values[2 * i],
1547 &values[2 * i + 1],
1548 &cxt) == 0);
1549 }
1550
1551 return (nvalues / 2);
1552}
Definition: type.h:96

References Assert, cmp(), compare_context::cmpFn, ExpandedRange::collapsed, compare_context::colloid, compare_values(), i, DistanceValue::index, ExpandedRange::maxval, ExpandedRange::minval, palloc(), qsort_arg(), and values.

Referenced by brin_minmax_multi_union(), compactify_ranges(), and ensure_free_space_in_buffer().

◆ sort_expanded_ranges()

static int sort_expanded_ranges ( FmgrInfo cmp,
Oid  colloid,
ExpandedRange eranges,
int  neranges 
)
static

Definition at line 1180 of file brin_minmax_multi.c.

1182{
1183 int n;
1184 int i;
1185 compare_context cxt;
1186
1187 Assert(neranges > 0);
1188
1189 /* sort the values */
1190 cxt.colloid = colloid;
1191 cxt.cmpFn = cmp;
1192
1193 /*
1194 * XXX We do qsort on all the values, but we could also leverage the fact
1195 * that some of the input data is already sorted (all the ranges and maybe
1196 * some of the points) and do merge sort.
1197 */
1198 qsort_arg(eranges, neranges, sizeof(ExpandedRange),
1200
1201 /*
1202 * Deduplicate the ranges - simply compare each range to the preceding
1203 * one, and skip the duplicate ones.
1204 */
1205 n = 1;
1206 for (i = 1; i < neranges; i++)
1207 {
1208 /* if the current range is equal to the preceding one, do nothing */
1209 if (!compare_expanded_ranges(&eranges[i - 1], &eranges[i], &cxt))
1210 continue;
1211
1212 /* otherwise, copy it to n-th place (if not already there) */
1213 if (i != n)
1214 memcpy(&eranges[n], &eranges[i], sizeof(ExpandedRange));
1215
1216 n++;
1217 }
1218
1219 Assert((n > 0) && (n <= neranges));
1220
1221 return n;
1222}
static int compare_expanded_ranges(const void *a, const void *b, void *arg)

References Assert, cmp(), compare_context::cmpFn, compare_context::colloid, compare_expanded_ranges(), i, and qsort_arg().

Referenced by brin_minmax_multi_union(), and build_expanded_ranges().

◆ store_expanded_ranges()

static void store_expanded_ranges ( Ranges ranges,
ExpandedRange eranges,
int  neranges 
)
static

Definition at line 1559 of file brin_minmax_multi.c.

1560{
1561 int i;
1562 int idx = 0;
1563
1564 /* first copy in the regular ranges */
1565 ranges->nranges = 0;
1566 for (i = 0; i < neranges; i++)
1567 {
1568 if (!eranges[i].collapsed)
1569 {
1570 ranges->values[idx++] = eranges[i].minval;
1571 ranges->values[idx++] = eranges[i].maxval;
1572 ranges->nranges++;
1573 }
1574 }
1575
1576 /* now copy in the collapsed ones */
1577 ranges->nvalues = 0;
1578 for (i = 0; i < neranges; i++)
1579 {
1580 if (eranges[i].collapsed)
1581 {
1582 ranges->values[idx++] = eranges[i].minval;
1583 ranges->nvalues++;
1584 }
1585 }
1586
1587 /* all the values are sorted */
1588 ranges->nsorted = ranges->nvalues;
1589
1590 Assert(count_values(eranges, neranges) == 2 * ranges->nranges + ranges->nvalues);
1591 Assert(2 * ranges->nranges + ranges->nvalues <= ranges->maxvalues);
1592}

References Assert, i, idx(), ExpandedRange::maxval, Ranges::maxvalues, ExpandedRange::minval, Ranges::nranges, Ranges::nsorted, Ranges::nvalues, and Ranges::values.

Referenced by brin_minmax_multi_union(), compactify_ranges(), and ensure_free_space_in_buffer().