1996-08-27 23:50:29 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* tupmacs.h
|
1996-08-27 23:50:29 +02:00
|
|
|
* Tuple macros used by both index tuples and heap tuples.
|
|
|
|
*
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/include/access/tupmacs.h
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef TUPMACS_H
|
|
|
|
#define TUPMACS_H
|
|
|
|
|
2020-03-04 16:34:25 +01:00
|
|
|
#include "catalog/pg_type_d.h" /* for TYPALIGN macros */
|
|
|
|
|
1999-07-15 17:21:54 +02:00
|
|
|
|
1996-08-27 23:50:29 +02:00
|
|
|
/*
|
2019-05-13 19:13:24 +02:00
|
|
|
* Check a tuple's null bitmap to determine whether the attribute is null.
|
|
|
|
* Note that a 0 in the null bitmap indicates a null, while 1 indicates
|
|
|
|
* non-null.
|
1996-08-27 23:50:29 +02:00
|
|
|
*/
|
2022-07-18 07:43:39 +02:00
|
|
|
static inline bool
|
|
|
|
att_isnull(int ATT, const bits8 *BITS)
|
|
|
|
{
|
|
|
|
return !(BITS[ATT >> 3] & (1 << (ATT & 0x07)));
|
|
|
|
}
|
1996-08-27 23:50:29 +02:00
|
|
|
|
2022-07-18 07:43:39 +02:00
|
|
|
#ifndef FRONTEND
|
1996-08-27 23:50:29 +02:00
|
|
|
/*
|
2000-12-28 00:59:14 +01:00
|
|
|
* Given a Form_pg_attribute and a pointer into a tuple's data area,
|
|
|
|
* return the correct value or pointer.
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2000-12-28 00:59:14 +01:00
|
|
|
* We return a Datum value in all cases. If the attribute has "byval" false,
|
|
|
|
* we return the same pointer into the tuple data area that we're passed.
|
|
|
|
* Otherwise, we return the correct number of bytes fetched from the data
|
|
|
|
* area and extended to Datum form.
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2000-12-28 00:59:14 +01:00
|
|
|
* On machines where Datum is 8 bytes, we support fetching 8-byte byval
|
|
|
|
* attributes; otherwise, only 1, 2, and 4-byte values are supported.
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2000-12-28 00:59:14 +01:00
|
|
|
* Note that T must already be properly aligned for this to work correctly.
|
|
|
|
*/
|
|
|
|
#define fetchatt(A,T) fetch_att(T, (A)->attbyval, (A)->attlen)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Same, but work from byval/len parameters rather than Form_pg_attribute.
|
1996-08-27 23:50:29 +02:00
|
|
|
*/
|
2022-07-18 07:43:39 +02:00
|
|
|
static inline Datum
|
|
|
|
fetch_att(const void *T, bool attbyval, int attlen)
|
|
|
|
{
|
|
|
|
if (attbyval)
|
|
|
|
{
|
|
|
|
switch (attlen)
|
|
|
|
{
|
|
|
|
case sizeof(char):
|
|
|
|
return CharGetDatum(*((const char *) T));
|
|
|
|
case sizeof(int16):
|
|
|
|
return Int16GetDatum(*((const int16 *) T));
|
|
|
|
case sizeof(int32):
|
|
|
|
return Int32GetDatum(*((const int32 *) T));
|
2000-12-28 00:59:14 +01:00
|
|
|
#if SIZEOF_DATUM == 8
|
2022-07-18 07:43:39 +02:00
|
|
|
case sizeof(Datum):
|
|
|
|
return *((const Datum *) T);
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
elog(ERROR, "unsupported byval length: %d", attlen);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return PointerGetDatum(T);
|
|
|
|
}
|
|
|
|
#endif /* FRONTEND */
|
2000-12-28 00:59:14 +01:00
|
|
|
|
|
|
|
/*
|
2007-04-06 06:21:44 +02:00
|
|
|
* att_align_datum aligns the given offset as needed for a datum of alignment
|
|
|
|
* requirement attalign and typlen attlen. attdatum is the Datum variable
|
|
|
|
* we intend to pack into a tuple (it's only accessed if we are dealing with
|
|
|
|
* a varlena type). Note that this assumes the Datum will be stored as-is;
|
|
|
|
* callers that are intending to convert non-short varlena datums to short
|
|
|
|
* format have to account for that themselves.
|
1999-03-25 04:49:34 +01:00
|
|
|
*/
|
2007-04-06 06:21:44 +02:00
|
|
|
#define att_align_datum(cur_offset, attalign, attlen, attdatum) \
|
|
|
|
( \
|
2009-12-31 20:41:37 +01:00
|
|
|
((attlen) == -1 && VARATT_IS_SHORT(DatumGetPointer(attdatum))) ? \
|
2013-10-21 03:04:52 +02:00
|
|
|
(uintptr_t) (cur_offset) : \
|
2007-04-06 06:21:44 +02:00
|
|
|
att_align_nominal(cur_offset, attalign) \
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* att_align_pointer performs the same calculation as att_align_datum,
|
|
|
|
* but is used when walking a tuple. attptr is the current actual data
|
|
|
|
* pointer; when accessing a varlena field we have to "peek" to see if we
|
|
|
|
* are looking at a pad byte or the first byte of a 1-byte-header datum.
|
|
|
|
* (A zero byte must be either a pad byte, or the first byte of a correctly
|
|
|
|
* aligned 4-byte length word; in either case we can align safely. A non-zero
|
|
|
|
* byte must be either a 1-byte length word, or the first byte of a correctly
|
|
|
|
* aligned 4-byte length word; in either case we need not align.)
|
|
|
|
*
|
|
|
|
* Note: some callers pass a "char *" pointer for cur_offset. This is
|
2013-10-21 03:04:52 +02:00
|
|
|
* a bit of a hack but should work all right as long as uintptr_t is the
|
2009-12-31 20:41:37 +01:00
|
|
|
* correct width.
|
2007-04-06 06:21:44 +02:00
|
|
|
*/
|
|
|
|
#define att_align_pointer(cur_offset, attalign, attlen, attptr) \
|
|
|
|
( \
|
2009-12-31 20:41:37 +01:00
|
|
|
((attlen) == -1 && VARATT_NOT_PAD_BYTE(attptr)) ? \
|
2013-10-21 03:04:52 +02:00
|
|
|
(uintptr_t) (cur_offset) : \
|
2007-04-06 06:21:44 +02:00
|
|
|
att_align_nominal(cur_offset, attalign) \
|
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* att_align_nominal aligns the given offset as needed for a datum of alignment
|
|
|
|
* requirement attalign, ignoring any consideration of packed varlena datums.
|
|
|
|
* There are three main use cases for using this macro directly:
|
|
|
|
* * we know that the att in question is not varlena (attlen != -1);
|
|
|
|
* in this case it is cheaper than the above macros and just as good.
|
|
|
|
* * we need to estimate alignment padding cost abstractly, ie without
|
|
|
|
* reference to a real tuple. We must assume the worst case that
|
|
|
|
* all varlenas are aligned.
|
Multirange datatypes
Multiranges are basically sorted arrays of non-overlapping ranges with
set-theoretic operations defined over them.
Since v14, each range type automatically gets a corresponding multirange
datatype. There are both manual and automatic mechanisms for naming multirange
types. Once can specify multirange type name using multirange_type_name
attribute in CREATE TYPE. Otherwise, a multirange type name is generated
automatically. If the range type name contains "range" then we change that to
"multirange". Otherwise, we add "_multirange" to the end.
Implementation of multiranges comes with a space-efficient internal
representation format, which evades extra paddings and duplicated storage of
oids. Altogether this format allows fetching a particular range by its index
in O(n).
Statistic gathering and selectivity estimation are implemented for multiranges.
For this purpose, stored multirange is approximated as union range without gaps.
This field will likely need improvements in the future.
Catversion is bumped.
Discussion: https://postgr.es/m/CALNJ-vSUpQ_Y%3DjXvTxt1VYFztaBSsWVXeF1y6gTYQ4bOiWDLgQ%40mail.gmail.com
Discussion: https://postgr.es/m/a0b8026459d1e6167933be2104a6174e7d40d0ab.camel%40j-davis.com#fe7218c83b08068bfffb0c5293eceda0
Author: Paul Jungwirth, revised by me
Reviewed-by: David Fetter, Corey Huinker, Jeff Davis, Pavel Stehule
Reviewed-by: Alvaro Herrera, Tom Lane, Isaac Morland, David G. Johnston
Reviewed-by: Zhihong Yu, Alexander Korotkov
2020-12-20 05:20:33 +01:00
|
|
|
* * within arrays and multiranges, we unconditionally align varlenas (XXX this
|
|
|
|
* should be revisited, probably).
|
2007-04-06 06:21:44 +02:00
|
|
|
*
|
|
|
|
* The attalign cases are tested in what is hopefully something like their
|
|
|
|
* frequency of occurrence.
|
|
|
|
*/
|
|
|
|
#define att_align_nominal(cur_offset, attalign) \
|
1998-09-07 07:35:48 +02:00
|
|
|
( \
|
2020-03-04 16:34:25 +01:00
|
|
|
((attalign) == TYPALIGN_INT) ? INTALIGN(cur_offset) : \
|
|
|
|
(((attalign) == TYPALIGN_CHAR) ? (uintptr_t) (cur_offset) : \
|
|
|
|
(((attalign) == TYPALIGN_DOUBLE) ? DOUBLEALIGN(cur_offset) : \
|
2007-04-06 06:21:44 +02:00
|
|
|
( \
|
2020-03-04 16:34:25 +01:00
|
|
|
AssertMacro((attalign) == TYPALIGN_SHORT), \
|
1999-03-25 04:49:34 +01:00
|
|
|
SHORTALIGN(cur_offset) \
|
2007-04-06 06:21:44 +02:00
|
|
|
))) \
|
1998-09-07 07:35:48 +02:00
|
|
|
)
|
1999-03-25 04:49:34 +01:00
|
|
|
|
2000-12-28 00:59:14 +01:00
|
|
|
/*
|
2007-04-06 06:21:44 +02:00
|
|
|
* att_addlength_datum increments the given offset by the space needed for
|
|
|
|
* the given Datum variable. attdatum is only accessed if we are dealing
|
|
|
|
* with a variable-length attribute.
|
|
|
|
*/
|
|
|
|
#define att_addlength_datum(cur_offset, attlen, attdatum) \
|
|
|
|
att_addlength_pointer(cur_offset, attlen, DatumGetPointer(attdatum))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* att_addlength_pointer performs the same calculation as att_addlength_datum,
|
|
|
|
* but is used when walking a tuple --- attptr is the pointer to the field
|
|
|
|
* within the tuple.
|
|
|
|
*
|
|
|
|
* Note: some callers pass a "char *" pointer for cur_offset. This is
|
|
|
|
* actually perfectly OK, but probably should be cleaned up along with
|
|
|
|
* the same practice for att_align_pointer.
|
2000-12-28 00:59:14 +01:00
|
|
|
*/
|
2007-04-06 06:21:44 +02:00
|
|
|
#define att_addlength_pointer(cur_offset, attlen, attptr) \
|
1998-09-07 07:35:48 +02:00
|
|
|
( \
|
2002-08-24 17:00:47 +02:00
|
|
|
((attlen) > 0) ? \
|
1998-09-07 07:35:48 +02:00
|
|
|
( \
|
|
|
|
(cur_offset) + (attlen) \
|
|
|
|
) \
|
2002-08-24 17:00:47 +02:00
|
|
|
: (((attlen) == -1) ? \
|
1998-09-07 07:35:48 +02:00
|
|
|
( \
|
2007-04-06 06:21:44 +02:00
|
|
|
(cur_offset) + VARSIZE_ANY(attptr) \
|
1998-09-07 07:35:48 +02:00
|
|
|
) \
|
2002-08-24 17:00:47 +02:00
|
|
|
: \
|
|
|
|
( \
|
|
|
|
AssertMacro((attlen) == -2), \
|
2007-04-06 06:21:44 +02:00
|
|
|
(cur_offset) + (strlen((char *) (attptr)) + 1) \
|
2002-08-24 17:00:47 +02:00
|
|
|
)) \
|
1998-09-07 07:35:48 +02:00
|
|
|
)
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2022-07-18 07:43:39 +02:00
|
|
|
#ifndef FRONTEND
|
2000-12-28 00:59:14 +01:00
|
|
|
/*
|
|
|
|
* store_att_byval is a partial inverse of fetch_att: store a given Datum
|
|
|
|
* value into a tuple data area at the specified address. However, it only
|
|
|
|
* handles the byval case, because in typical usage the caller needs to
|
2022-07-18 07:43:39 +02:00
|
|
|
* distinguish by-val and by-ref cases anyway, and so a do-it-all function
|
2000-12-28 00:59:14 +01:00
|
|
|
* wouldn't be convenient.
|
|
|
|
*/
|
2022-07-18 07:43:39 +02:00
|
|
|
static inline void
|
|
|
|
store_att_byval(void *T, Datum newdatum, int attlen)
|
|
|
|
{
|
|
|
|
switch (attlen)
|
|
|
|
{
|
|
|
|
case sizeof(char):
|
|
|
|
*(char *) T = DatumGetChar(newdatum);
|
|
|
|
break;
|
|
|
|
case sizeof(int16):
|
|
|
|
*(int16 *) T = DatumGetInt16(newdatum);
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
*(int32 *) T = DatumGetInt32(newdatum);
|
|
|
|
break;
|
2000-12-28 00:59:14 +01:00
|
|
|
#if SIZEOF_DATUM == 8
|
2022-07-18 07:43:39 +02:00
|
|
|
case sizeof(Datum):
|
|
|
|
*(Datum *) T = newdatum;
|
|
|
|
break;
|
1996-08-27 23:50:29 +02:00
|
|
|
#endif
|
2022-07-18 07:43:39 +02:00
|
|
|
default:
|
|
|
|
elog(ERROR, "unsupported byval length: %d", attlen);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* FRONTEND */
|
|
|
|
|
|
|
|
#endif /* TUPMACS_H */
|