PostgreSQL Source Code  git master
brin_minmax.c File Reference
#include "postgres.h"
#include "access/brin_internal.h"
#include "access/brin_tuple.h"
#include "access/genam.h"
#include "access/stratnum.h"
#include "catalog/pg_amop.h"
#include "catalog/pg_type.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for brin_minmax.c:

Go to the source code of this file.

Data Structures

struct  MinmaxOpaque
 

Typedefs

typedef struct MinmaxOpaque MinmaxOpaque
 

Functions

static FmgrInfominmax_get_strategy_procinfo (BrinDesc *bdesc, uint16 attno, Oid subtype, uint16 strategynum)
 
Datum brin_minmax_opcinfo (PG_FUNCTION_ARGS)
 
Datum brin_minmax_add_value (PG_FUNCTION_ARGS)
 
Datum brin_minmax_consistent (PG_FUNCTION_ARGS)
 
Datum brin_minmax_union (PG_FUNCTION_ARGS)
 

Typedef Documentation

◆ MinmaxOpaque

typedef struct MinmaxOpaque MinmaxOpaque

Function Documentation

◆ brin_minmax_add_value()

Datum brin_minmax_add_value ( PG_FUNCTION_ARGS  )

Definition at line 66 of file brin_minmax.c.

67 {
68  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
69  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
72  Oid colloid = PG_GET_COLLATION();
73  FmgrInfo *cmpFn;
74  Datum compar;
75  bool updated = false;
76  Form_pg_attribute attr;
77  AttrNumber attno;
78 
79  Assert(!isnull);
80 
81  attno = column->bv_attno;
82  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
83 
84  /*
85  * If the recorded value is null, store the new value (which we know to be
86  * not null) as both minimum and maximum, and we're done.
87  */
88  if (column->bv_allnulls)
89  {
90  column->bv_values[0] = datumCopy(newval, attr->attbyval, attr->attlen);
91  column->bv_values[1] = datumCopy(newval, attr->attbyval, attr->attlen);
92  column->bv_allnulls = false;
93  PG_RETURN_BOOL(true);
94  }
95 
96  /*
97  * Otherwise, need to compare the new value with the existing boundaries
98  * and update them accordingly. First check if it's less than the
99  * existing minimum.
100  */
101  cmpFn = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid,
103  compar = FunctionCall2Coll(cmpFn, colloid, newval, column->bv_values[0]);
104  if (DatumGetBool(compar))
105  {
106  if (!attr->attbyval)
107  pfree(DatumGetPointer(column->bv_values[0]));
108  column->bv_values[0] = datumCopy(newval, attr->attbyval, attr->attlen);
109  updated = true;
110  }
111 
112  /*
113  * And now compare it to the existing maximum.
114  */
115  cmpFn = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid,
117  compar = FunctionCall2Coll(cmpFn, colloid, newval, column->bv_values[1]);
118  if (DatumGetBool(compar))
119  {
120  if (!attr->attbyval)
121  pfree(DatumGetPointer(column->bv_values[1]));
122  column->bv_values[1] = datumCopy(newval, attr->attbyval, attr->attlen);
123  updated = true;
124  }
125 
126  PG_RETURN_BOOL(updated);
127 }
int16 AttrNumber
Definition: attnum.h:21
static FmgrInfo * minmax_get_strategy_procinfo(BrinDesc *bdesc, uint16 attno, Oid subtype, uint16 strategynum)
Definition: brin_minmax.c:263
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition: datum.c:132
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition: fmgr.c:1120
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
#define PG_GETARG_DATUM(n)
Definition: fmgr.h:268
#define PG_GET_COLLATION()
Definition: fmgr.h:198
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
#define newval
Assert(fmt[strlen(fmt) - 1] !='\n')
void pfree(void *pointer)
Definition: mcxt.c:1456
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:209
static bool DatumGetBool(Datum X)
Definition: postgres.h:90
uintptr_t Datum
Definition: postgres.h:64
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
unsigned int Oid
Definition: postgres_ext.h:31
#define BTGreaterStrategyNumber
Definition: stratnum.h:33
#define BTLessStrategyNumber
Definition: stratnum.h:29
TupleDesc bd_tupdesc
Definition: brin_internal.h:53
Datum * bv_values
Definition: brin_tuple.h:34
AttrNumber bv_attno
Definition: brin_tuple.h:31
bool bv_allnulls
Definition: brin_tuple.h:33
Definition: fmgr.h:57
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References Assert(), BrinDesc::bd_tupdesc, BTGreaterStrategyNumber, BTLessStrategyNumber, BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_values, datumCopy(), DatumGetBool(), DatumGetPointer(), FunctionCall2Coll(), minmax_get_strategy_procinfo(), newval, pfree(), PG_GET_COLLATION, PG_GETARG_DATUM, PG_GETARG_POINTER, PG_RETURN_BOOL, PG_USED_FOR_ASSERTS_ONLY, and TupleDescAttr.

◆ brin_minmax_consistent()

Datum brin_minmax_consistent ( PG_FUNCTION_ARGS  )

Definition at line 139 of file brin_minmax.c.

140 {
141  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
142  BrinValues *column = (BrinValues *) PG_GETARG_POINTER(1);
144  Oid colloid = PG_GET_COLLATION(),
145  subtype;
146  AttrNumber attno;
147  Datum value;
148  Datum matches;
149  FmgrInfo *finfo;
150 
151  /* This opclass uses the old signature with only three arguments. */
152  Assert(PG_NARGS() == 3);
153 
154  /* Should not be dealing with all-NULL ranges. */
155  Assert(!column->bv_allnulls);
156 
157  attno = key->sk_attno;
158  subtype = key->sk_subtype;
159  value = key->sk_argument;
160  switch (key->sk_strategy)
161  {
164  finfo = minmax_get_strategy_procinfo(bdesc, attno, subtype,
165  key->sk_strategy);
166  matches = FunctionCall2Coll(finfo, colloid, column->bv_values[0],
167  value);
168  break;
170 
171  /*
172  * In the equality case (WHERE col = someval), we want to return
173  * the current page range if the minimum value in the range <=
174  * scan key, and the maximum value >= scan key.
175  */
176  finfo = minmax_get_strategy_procinfo(bdesc, attno, subtype,
178  matches = FunctionCall2Coll(finfo, colloid, column->bv_values[0],
179  value);
180  if (!DatumGetBool(matches))
181  break;
182  /* max() >= scankey */
183  finfo = minmax_get_strategy_procinfo(bdesc, attno, subtype,
185  matches = FunctionCall2Coll(finfo, colloid, column->bv_values[1],
186  value);
187  break;
190  finfo = minmax_get_strategy_procinfo(bdesc, attno, subtype,
191  key->sk_strategy);
192  matches = FunctionCall2Coll(finfo, colloid, column->bv_values[1],
193  value);
194  break;
195  default:
196  /* shouldn't happen */
197  elog(ERROR, "invalid strategy number %d", key->sk_strategy);
198  matches = 0;
199  break;
200  }
201 
202  PG_RETURN_DATUM(matches);
203 }
#define ERROR
Definition: elog.h:39
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
static struct @147 value
ScanKeyData * ScanKey
Definition: skey.h:75
#define BTEqualStrategyNumber
Definition: stratnum.h:31
#define BTLessEqualStrategyNumber
Definition: stratnum.h:30
#define BTGreaterEqualStrategyNumber
Definition: stratnum.h:32

References Assert(), BTEqualStrategyNumber, BTGreaterEqualStrategyNumber, BTGreaterStrategyNumber, BTLessEqualStrategyNumber, BTLessStrategyNumber, BrinValues::bv_allnulls, BrinValues::bv_values, DatumGetBool(), elog(), ERROR, FunctionCall2Coll(), sort-test::key, minmax_get_strategy_procinfo(), PG_GET_COLLATION, PG_GETARG_POINTER, PG_NARGS, PG_RETURN_DATUM, and value.

◆ brin_minmax_opcinfo()

Datum brin_minmax_opcinfo ( PG_FUNCTION_ARGS  )

Definition at line 36 of file brin_minmax.c.

37 {
38  Oid typoid = PG_GETARG_OID(0);
39  BrinOpcInfo *result;
40 
41  /*
42  * opaque->strategy_procinfos is initialized lazily; here it is set to
43  * all-uninitialized by palloc0 which sets fn_oid to InvalidOid.
44  */
45 
46  result = palloc0(MAXALIGN(SizeofBrinOpcInfo(2)) +
47  sizeof(MinmaxOpaque));
48  result->oi_nstored = 2;
49  result->oi_regular_nulls = true;
50  result->oi_opaque = (MinmaxOpaque *)
51  MAXALIGN((char *) result + SizeofBrinOpcInfo(2));
52  result->oi_typcache[0] = result->oi_typcache[1] =
53  lookup_type_cache(typoid, 0);
54 
55  PG_RETURN_POINTER(result);
56 }
#define SizeofBrinOpcInfo(ncols)
Definition: brin_internal.h:41
#define MAXALIGN(LEN)
Definition: c.h:795
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:361
void * palloc0(Size size)
Definition: mcxt.c:1257
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:339

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

◆ brin_minmax_union()

Datum brin_minmax_union ( PG_FUNCTION_ARGS  )

Definition at line 210 of file brin_minmax.c.

211 {
212  BrinDesc *bdesc = (BrinDesc *) PG_GETARG_POINTER(0);
213  BrinValues *col_a = (BrinValues *) PG_GETARG_POINTER(1);
214  BrinValues *col_b = (BrinValues *) PG_GETARG_POINTER(2);
215  Oid colloid = PG_GET_COLLATION();
216  AttrNumber attno;
217  Form_pg_attribute attr;
218  FmgrInfo *finfo;
219  bool needsadj;
220 
221  Assert(col_a->bv_attno == col_b->bv_attno);
222  Assert(!col_a->bv_allnulls && !col_b->bv_allnulls);
223 
224  attno = col_a->bv_attno;
225  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
226 
227  /* Adjust minimum, if B's min is less than A's min */
228  finfo = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid,
230  needsadj = FunctionCall2Coll(finfo, colloid, col_b->bv_values[0],
231  col_a->bv_values[0]);
232  if (needsadj)
233  {
234  if (!attr->attbyval)
235  pfree(DatumGetPointer(col_a->bv_values[0]));
236  col_a->bv_values[0] = datumCopy(col_b->bv_values[0],
237  attr->attbyval, attr->attlen);
238  }
239 
240  /* Adjust maximum, if B's max is greater than A's max */
241  finfo = minmax_get_strategy_procinfo(bdesc, attno, attr->atttypid,
243  needsadj = FunctionCall2Coll(finfo, colloid, col_b->bv_values[1],
244  col_a->bv_values[1]);
245  if (needsadj)
246  {
247  if (!attr->attbyval)
248  pfree(DatumGetPointer(col_a->bv_values[1]));
249  col_a->bv_values[1] = datumCopy(col_b->bv_values[1],
250  attr->attbyval, attr->attlen);
251  }
252 
253  PG_RETURN_VOID();
254 }
#define PG_RETURN_VOID()
Definition: fmgr.h:349

References Assert(), BrinDesc::bd_tupdesc, BTGreaterStrategyNumber, BTLessStrategyNumber, BrinValues::bv_allnulls, BrinValues::bv_attno, BrinValues::bv_values, datumCopy(), DatumGetPointer(), FunctionCall2Coll(), minmax_get_strategy_procinfo(), pfree(), PG_GET_COLLATION, PG_GETARG_POINTER, PG_RETURN_VOID, and TupleDescAttr.

◆ minmax_get_strategy_procinfo()

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

Definition at line 263 of file brin_minmax.c.

265 {
266  MinmaxOpaque *opaque;
267 
268  Assert(strategynum >= 1 &&
269  strategynum <= BTMaxStrategyNumber);
270 
271  opaque = (MinmaxOpaque *) bdesc->bd_info[attno - 1]->oi_opaque;
272 
273  /*
274  * We cache the procedures for the previous subtype in the opaque struct,
275  * to avoid repetitive syscache lookups. If the subtype changed,
276  * invalidate all the cached entries.
277  */
278  if (opaque->cached_subtype != subtype)
279  {
280  uint16 i;
281 
282  for (i = 1; i <= BTMaxStrategyNumber; i++)
283  opaque->strategy_procinfos[i - 1].fn_oid = InvalidOid;
284  opaque->cached_subtype = subtype;
285  }
286 
287  if (opaque->strategy_procinfos[strategynum - 1].fn_oid == InvalidOid)
288  {
289  Form_pg_attribute attr;
290  HeapTuple tuple;
291  Oid opfamily,
292  oprid;
293 
294  opfamily = bdesc->bd_index->rd_opfamily[attno - 1];
295  attr = TupleDescAttr(bdesc->bd_tupdesc, attno - 1);
296  tuple = SearchSysCache4(AMOPSTRATEGY, ObjectIdGetDatum(opfamily),
297  ObjectIdGetDatum(attr->atttypid),
298  ObjectIdGetDatum(subtype),
299  Int16GetDatum(strategynum));
300 
301  if (!HeapTupleIsValid(tuple))
302  elog(ERROR, "missing operator %d(%u,%u) in opfamily %u",
303  strategynum, attr->atttypid, subtype, opfamily);
304 
306  Anum_pg_amop_amopopr));
307  ReleaseSysCache(tuple);
309 
311  &opaque->strategy_procinfos[strategynum - 1],
312  bdesc->bd_context);
313  }
314 
315  return &opaque->strategy_procinfos[strategynum - 1];
316 }
unsigned short uint16
Definition: c.h:489
#define RegProcedureIsValid(p)
Definition: c.h:761
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:137
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
RegProcedure get_opcode(Oid opno)
Definition: lsyscache.c:1267
Oid oprid(Operator op)
Definition: parse_oper.c:250
static Oid DatumGetObjectId(Datum X)
Definition: postgres.h:242
static Datum Int16GetDatum(int16 X)
Definition: postgres.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
#define BTMaxStrategyNumber
Definition: stratnum.h:35
BrinOpcInfo * bd_info[FLEXIBLE_ARRAY_MEMBER]
Definition: brin_internal.h:62
Relation bd_index
Definition: brin_internal.h:50
MemoryContext bd_context
Definition: brin_internal.h:47
Oid fn_oid
Definition: fmgr.h:59
FmgrInfo strategy_procinfos[BTMaxStrategyNumber]
Definition: brin_minmax.c:28
Oid cached_subtype
Definition: brin_minmax.c:27
Oid * rd_opfamily
Definition: rel.h:206
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:866
HeapTuple SearchSysCache4(int cacheId, Datum key1, Datum key2, Datum key3, Datum key4)
Definition: syscache.c:851
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:1110
@ AMOPSTRATEGY
Definition: syscache.h:38

References AMOPSTRATEGY, Assert(), BrinDesc::bd_context, BrinDesc::bd_index, BrinDesc::bd_info, BrinDesc::bd_tupdesc, BTMaxStrategyNumber, MinmaxOpaque::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(), MinmaxOpaque::strategy_procinfos, SysCacheGetAttrNotNull(), and TupleDescAttr.

Referenced by brin_minmax_add_value(), brin_minmax_consistent(), and brin_minmax_union().