1 /*------------------------------------------------------------------------- 2 * 3 * array.h 4 * Declarations for Postgres arrays. 5 * 6 * A standard varlena array has the following internal structure: 7 * <vl_len_> - standard varlena header word 8 * <ndim> - number of dimensions of the array 9 * <dataoffset> - offset to stored data, or 0 if no nulls bitmap 10 * <elemtype> - element type OID 11 * <dimensions> - length of each array axis (C array of int) 12 * <lower bnds> - lower boundary of each dimension (C array of int) 13 * <null bitmap> - bitmap showing locations of nulls (OPTIONAL) 14 * <actual data> - whatever is the stored data 15 * 16 * The <dimensions> and <lower bnds> arrays each have ndim elements. 17 * 18 * The <null bitmap> may be omitted if the array contains no NULL elements. 19 * If it is absent, the <dataoffset> field is zero and the offset to the 20 * stored data must be computed on-the-fly. If the bitmap is present, 21 * <dataoffset> is nonzero and is equal to the offset from the array start 22 * to the first data element (including any alignment padding). The bitmap 23 * follows the same conventions as tuple null bitmaps, ie, a 1 indicates 24 * a non-null entry and the LSB of each bitmap byte is used first. 25 * 26 * The actual data starts on a MAXALIGN boundary. Individual items in the 27 * array are aligned as specified by the array element type. They are 28 * stored in row-major order (last subscript varies most rapidly). 29 * 30 * NOTE: it is important that array elements of toastable datatypes NOT be 31 * toasted, since the tupletoaster won't know they are there. (We could 32 * support compressed toasted items; only out-of-line items are dangerous. 33 * However, it seems preferable to store such items uncompressed and allow 34 * the toaster to compress the whole array as one input.) 35 * 36 * 37 * The OIDVECTOR and INT2VECTOR datatypes are storage-compatible with 38 * generic arrays, but they support only one-dimensional arrays with no 39 * nulls (and no null bitmap). They don't support being toasted, either. 40 * 41 * There are also some "fixed-length array" datatypes, such as NAME and 42 * POINT. These are simply a sequence of a fixed number of items each 43 * of a fixed-length datatype, with no overhead; the item size must be 44 * a multiple of its alignment requirement, because we do no padding. 45 * We support subscripting on these types, but array_in() and array_out() 46 * only work with varlena arrays. 47 * 48 * In addition, arrays are a major user of the "expanded object" TOAST 49 * infrastructure. This allows a varlena array to be converted to a 50 * separate representation that may include "deconstructed" Datum/isnull 51 * arrays holding the elements. 52 * 53 * 54 * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group 55 * Portions Copyright (c) 1994, Regents of the University of California 56 * 57 * src/include/utils/array.h 58 * 59 *------------------------------------------------------------------------- 60 */ 61 #ifndef ARRAY_H 62 #define ARRAY_H 63 64 #include "fmgr.h" 65 #include "utils/expandeddatum.h" 66 67 68 /* 69 * Arrays are varlena objects, so must meet the varlena convention that 70 * the first int32 of the object contains the total object size in bytes. 71 * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though! 72 * 73 * CAUTION: if you change the header for ordinary arrays you will also 74 * need to change the headers for oidvector and int2vector! 75 */ 76 typedef struct 77 { 78 int32 vl_len_; /* varlena header (do not touch directly!) */ 79 int ndim; /* # of dimensions */ 80 int32 dataoffset; /* offset to data, or 0 if no bitmap */ 81 Oid elemtype; /* element type OID */ 82 } ArrayType; 83 84 /* 85 * An expanded array is contained within a private memory context (as 86 * all expanded objects must be) and has a control structure as below. 87 * 88 * The expanded array might contain a regular "flat" array if that was the 89 * original input and we've not modified it significantly. Otherwise, the 90 * contents are represented by Datum/isnull arrays plus dimensionality and 91 * type information. We could also have both forms, if we've deconstructed 92 * the original array for access purposes but not yet changed it. For pass- 93 * by-reference element types, the Datums would point into the flat array in 94 * this situation. Once we start modifying array elements, new pass-by-ref 95 * elements are separately palloc'd within the memory context. 96 */ 97 #define EA_MAGIC 689375833 /* ID for debugging crosschecks */ 98 99 typedef struct ExpandedArrayHeader 100 { 101 /* Standard header for expanded objects */ 102 ExpandedObjectHeader hdr; 103 104 /* Magic value identifying an expanded array (for debugging only) */ 105 int ea_magic; 106 107 /* Dimensionality info (always valid) */ 108 int ndims; /* # of dimensions */ 109 int *dims; /* array dimensions */ 110 int *lbound; /* index lower bounds for each dimension */ 111 112 /* Element type info (always valid) */ 113 Oid element_type; /* element type OID */ 114 int16 typlen; /* needed info about element datatype */ 115 bool typbyval; 116 char typalign; 117 118 /* 119 * If we have a Datum-array representation of the array, it's kept here; 120 * else dvalues/dnulls are NULL. The dvalues and dnulls arrays are always 121 * palloc'd within the object private context, but may change size from 122 * time to time. For pass-by-ref element types, dvalues entries might 123 * point either into the fstartptr..fendptr area, or to separately 124 * palloc'd chunks. Elements should always be fully detoasted, as they 125 * are in the standard flat representation. 126 * 127 * Even when dvalues is valid, dnulls can be NULL if there are no null 128 * elements. 129 */ 130 Datum *dvalues; /* array of Datums */ 131 bool *dnulls; /* array of is-null flags for Datums */ 132 int dvalueslen; /* allocated length of above arrays */ 133 int nelems; /* number of valid entries in above arrays */ 134 135 /* 136 * flat_size is the current space requirement for the flat equivalent of 137 * the expanded array, if known; otherwise it's 0. We store this to make 138 * consecutive calls of get_flat_size cheap. 139 */ 140 Size flat_size; 141 142 /* 143 * fvalue points to the flat representation if it is valid, else it is 144 * NULL. If we have or ever had a flat representation then 145 * fstartptr/fendptr point to the start and end+1 of its data area; this 146 * is so that we can tell which Datum pointers point into the flat 147 * representation rather than being pointers to separately palloc'd data. 148 */ 149 ArrayType *fvalue; /* must be a fully detoasted array */ 150 char *fstartptr; /* start of its data area */ 151 char *fendptr; /* end+1 of its data area */ 152 } ExpandedArrayHeader; 153 154 /* 155 * Functions that can handle either a "flat" varlena array or an expanded 156 * array use this union to work with their input. Don't refer to "flt"; 157 * instead, cast to ArrayType. This struct nominally requires 8-byte 158 * alignment on 64-bit, but it's often used for an ArrayType having 4-byte 159 * alignment. UBSan complains about referencing "flt" in such cases. 160 */ 161 typedef union AnyArrayType 162 { 163 ArrayType flt; 164 ExpandedArrayHeader xpn; 165 } AnyArrayType; 166 167 /* 168 * working state for accumArrayResult() and friends 169 * note that the input must be scalars (legal array elements) 170 */ 171 typedef struct ArrayBuildState 172 { 173 MemoryContext mcontext; /* where all the temp stuff is kept */ 174 Datum *dvalues; /* array of accumulated Datums */ 175 bool *dnulls; /* array of is-null flags for Datums */ 176 int alen; /* allocated length of above arrays */ 177 int nelems; /* number of valid entries in above arrays */ 178 Oid element_type; /* data type of the Datums */ 179 int16 typlen; /* needed info about datatype */ 180 bool typbyval; 181 char typalign; 182 bool private_cxt; /* use private memory context */ 183 } ArrayBuildState; 184 185 /* 186 * working state for accumArrayResultArr() and friends 187 * note that the input must be arrays, and the same array type is returned 188 */ 189 typedef struct ArrayBuildStateArr 190 { 191 MemoryContext mcontext; /* where all the temp stuff is kept */ 192 char *data; /* accumulated data */ 193 bits8 *nullbitmap; /* bitmap of is-null flags, or NULL if none */ 194 int abytes; /* allocated length of "data" */ 195 int nbytes; /* number of bytes used so far */ 196 int aitems; /* allocated length of bitmap (in elements) */ 197 int nitems; /* total number of elements in result */ 198 int ndims; /* current dimensions of result */ 199 int dims[MAXDIM]; 200 int lbs[MAXDIM]; 201 Oid array_type; /* data type of the arrays */ 202 Oid element_type; /* data type of the array elements */ 203 bool private_cxt; /* use private memory context */ 204 } ArrayBuildStateArr; 205 206 /* 207 * working state for accumArrayResultAny() and friends 208 * these functions handle both cases 209 */ 210 typedef struct ArrayBuildStateAny 211 { 212 /* Exactly one of these is not NULL: */ 213 ArrayBuildState *scalarstate; 214 ArrayBuildStateArr *arraystate; 215 } ArrayBuildStateAny; 216 217 /* 218 * structure to cache type metadata needed for array manipulation 219 */ 220 typedef struct ArrayMetaState 221 { 222 Oid element_type; 223 int16 typlen; 224 bool typbyval; 225 char typalign; 226 char typdelim; 227 Oid typioparam; 228 Oid typiofunc; 229 FmgrInfo proc; 230 } ArrayMetaState; 231 232 /* 233 * private state needed by array_map (here because caller must provide it) 234 */ 235 typedef struct ArrayMapState 236 { 237 ArrayMetaState inp_extra; 238 ArrayMetaState ret_extra; 239 } ArrayMapState; 240 241 /* ArrayIteratorData is private in arrayfuncs.c */ 242 typedef struct ArrayIteratorData *ArrayIterator; 243 244 /* fmgr macros for regular varlena array objects */ 245 #define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X)) 246 #define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X)) 247 #define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n)) 248 #define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n)) 249 #define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x) 250 251 /* fmgr macros for expanded array objects */ 252 #define PG_GETARG_EXPANDED_ARRAY(n) DatumGetExpandedArray(PG_GETARG_DATUM(n)) 253 #define PG_GETARG_EXPANDED_ARRAYX(n, metacache) \ 254 DatumGetExpandedArrayX(PG_GETARG_DATUM(n), metacache) 255 #define PG_RETURN_EXPANDED_ARRAY(x) PG_RETURN_DATUM(EOHPGetRWDatum(&(x)->hdr)) 256 257 /* fmgr macros for AnyArrayType (ie, get either varlena or expanded form) */ 258 #define PG_GETARG_ANY_ARRAY(n) DatumGetAnyArray(PG_GETARG_DATUM(n)) 259 260 /* 261 * Access macros for varlena array header fields. 262 * 263 * ARR_DIMS returns a pointer to an array of array dimensions (number of 264 * elements along the various array axes). 265 * 266 * ARR_LBOUND returns a pointer to an array of array lower bounds. 267 * 268 * That is: if the third axis of an array has elements 5 through 8, then 269 * ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5. 270 * 271 * Unlike C, the default lower bound is 1. 272 */ 273 #define ARR_SIZE(a) VARSIZE(a) 274 #define ARR_NDIM(a) ((a)->ndim) 275 #define ARR_HASNULL(a) ((a)->dataoffset != 0) 276 #define ARR_ELEMTYPE(a) ((a)->elemtype) 277 278 #define ARR_DIMS(a) \ 279 ((int *) (((char *) (a)) + sizeof(ArrayType))) 280 #define ARR_LBOUND(a) \ 281 ((int *) (((char *) (a)) + sizeof(ArrayType) + \ 282 sizeof(int) * ARR_NDIM(a))) 283 284 #define ARR_NULLBITMAP(a) \ 285 (ARR_HASNULL(a) ? \ 286 (bits8 *) (((char *) (a)) + sizeof(ArrayType) + \ 287 2 * sizeof(int) * ARR_NDIM(a)) \ 288 : (bits8 *) NULL) 289 290 /* 291 * The total array header size (in bytes) for an array with the specified 292 * number of dimensions and total number of items. 293 */ 294 #define ARR_OVERHEAD_NONULLS(ndims) \ 295 MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims)) 296 #define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \ 297 MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \ 298 ((nitems) + 7) / 8) 299 300 #define ARR_DATA_OFFSET(a) \ 301 (ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a))) 302 303 /* 304 * Returns a pointer to the actual array data. 305 */ 306 #define ARR_DATA_PTR(a) \ 307 (((char *) (a)) + ARR_DATA_OFFSET(a)) 308 309 /* 310 * Macros for working with AnyArrayType inputs. Beware multiple references! 311 */ 312 #define AARR_NDIM(a) \ 313 (VARATT_IS_EXPANDED_HEADER(a) ? \ 314 (a)->xpn.ndims : ARR_NDIM((ArrayType *) (a))) 315 #define AARR_HASNULL(a) \ 316 (VARATT_IS_EXPANDED_HEADER(a) ? \ 317 ((a)->xpn.dvalues != NULL ? (a)->xpn.dnulls != NULL : ARR_HASNULL((a)->xpn.fvalue)) : \ 318 ARR_HASNULL((ArrayType *) (a))) 319 #define AARR_ELEMTYPE(a) \ 320 (VARATT_IS_EXPANDED_HEADER(a) ? \ 321 (a)->xpn.element_type : ARR_ELEMTYPE((ArrayType *) (a))) 322 #define AARR_DIMS(a) \ 323 (VARATT_IS_EXPANDED_HEADER(a) ? \ 324 (a)->xpn.dims : ARR_DIMS((ArrayType *) (a))) 325 #define AARR_LBOUND(a) \ 326 (VARATT_IS_EXPANDED_HEADER(a) ? \ 327 (a)->xpn.lbound : ARR_LBOUND((ArrayType *) (a))) 328 329 330 /* 331 * GUC parameter 332 */ 333 extern bool Array_nulls; 334 335 /* 336 * prototypes for functions defined in arrayfuncs.c 337 */ 338 extern void CopyArrayEls(ArrayType *array, 339 Datum *values, 340 bool *nulls, 341 int nitems, 342 int typlen, 343 bool typbyval, 344 char typalign, 345 bool freedata); 346 347 extern Datum array_get_element(Datum arraydatum, int nSubscripts, int *indx, 348 int arraytyplen, int elmlen, bool elmbyval, char elmalign, 349 bool *isNull); 350 extern Datum array_set_element(Datum arraydatum, int nSubscripts, int *indx, 351 Datum dataValue, bool isNull, 352 int arraytyplen, int elmlen, bool elmbyval, char elmalign); 353 extern Datum array_get_slice(Datum arraydatum, int nSubscripts, 354 int *upperIndx, int *lowerIndx, 355 bool *upperProvided, bool *lowerProvided, 356 int arraytyplen, int elmlen, bool elmbyval, char elmalign); 357 extern Datum array_set_slice(Datum arraydatum, int nSubscripts, 358 int *upperIndx, int *lowerIndx, 359 bool *upperProvided, bool *lowerProvided, 360 Datum srcArrayDatum, bool isNull, 361 int arraytyplen, int elmlen, bool elmbyval, char elmalign); 362 363 extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx, 364 int arraytyplen, int elmlen, bool elmbyval, char elmalign, 365 bool *isNull); 366 extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx, 367 Datum dataValue, bool isNull, 368 int arraytyplen, int elmlen, bool elmbyval, char elmalign); 369 370 extern Datum array_map(FunctionCallInfo fcinfo, Oid retType, 371 ArrayMapState *amstate); 372 373 extern void array_bitmap_copy(bits8 *destbitmap, int destoffset, 374 const bits8 *srcbitmap, int srcoffset, 375 int nitems); 376 377 extern ArrayType *construct_array(Datum *elems, int nelems, 378 Oid elmtype, 379 int elmlen, bool elmbyval, char elmalign); 380 extern ArrayType *construct_md_array(Datum *elems, 381 bool *nulls, 382 int ndims, 383 int *dims, 384 int *lbs, 385 Oid elmtype, int elmlen, bool elmbyval, char elmalign); 386 extern ArrayType *construct_empty_array(Oid elmtype); 387 extern ExpandedArrayHeader *construct_empty_expanded_array(Oid element_type, 388 MemoryContext parentcontext, 389 ArrayMetaState *metacache); 390 extern void deconstruct_array(ArrayType *array, 391 Oid elmtype, 392 int elmlen, bool elmbyval, char elmalign, 393 Datum **elemsp, bool **nullsp, int *nelemsp); 394 extern bool array_contains_nulls(ArrayType *array); 395 396 extern ArrayBuildState *initArrayResult(Oid element_type, 397 MemoryContext rcontext, bool subcontext); 398 extern ArrayBuildState *accumArrayResult(ArrayBuildState *astate, 399 Datum dvalue, bool disnull, 400 Oid element_type, 401 MemoryContext rcontext); 402 extern Datum makeArrayResult(ArrayBuildState *astate, 403 MemoryContext rcontext); 404 extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, 405 int *dims, int *lbs, MemoryContext rcontext, bool release); 406 407 extern ArrayBuildStateArr *initArrayResultArr(Oid array_type, Oid element_type, 408 MemoryContext rcontext, bool subcontext); 409 extern ArrayBuildStateArr *accumArrayResultArr(ArrayBuildStateArr *astate, 410 Datum dvalue, bool disnull, 411 Oid array_type, 412 MemoryContext rcontext); 413 extern Datum makeArrayResultArr(ArrayBuildStateArr *astate, 414 MemoryContext rcontext, bool release); 415 416 extern ArrayBuildStateAny *initArrayResultAny(Oid input_type, 417 MemoryContext rcontext, bool subcontext); 418 extern ArrayBuildStateAny *accumArrayResultAny(ArrayBuildStateAny *astate, 419 Datum dvalue, bool disnull, 420 Oid input_type, 421 MemoryContext rcontext); 422 extern Datum makeArrayResultAny(ArrayBuildStateAny *astate, 423 MemoryContext rcontext, bool release); 424 425 extern ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim, ArrayMetaState *mstate); 426 extern bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull); 427 extern void array_free_iterator(ArrayIterator iterator); 428 429 /* 430 * prototypes for functions defined in arrayutils.c 431 */ 432 433 extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx); 434 extern int ArrayGetOffset0(int n, const int *tup, const int *scale); 435 extern int ArrayGetNItems(int ndim, const int *dims); 436 extern void ArrayCheckBounds(int ndim, const int *dims, const int *lb); 437 extern void mda_get_range(int n, int *span, const int *st, const int *endp); 438 extern void mda_get_prod(int n, const int *range, int *prod); 439 extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span); 440 extern int mda_next_tuple(int n, int *curr, const int *span); 441 extern int32 *ArrayGetIntegerTypmods(ArrayType *arr, int *n); 442 443 /* 444 * prototypes for functions defined in array_expanded.c 445 */ 446 extern Datum expand_array(Datum arraydatum, MemoryContext parentcontext, 447 ArrayMetaState *metacache); 448 extern ExpandedArrayHeader *DatumGetExpandedArray(Datum d); 449 extern ExpandedArrayHeader *DatumGetExpandedArrayX(Datum d, 450 ArrayMetaState *metacache); 451 extern AnyArrayType *DatumGetAnyArray(Datum d); 452 extern void deconstruct_expanded_array(ExpandedArrayHeader *eah); 453 454 #endif /* ARRAY_H */ 455