PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
array.h
Go to the documentation of this file.
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).
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  *
49  * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
50  * Portions Copyright (c) 1994, Regents of the University of California
51  *
52  * src/include/utils/array.h
53  *
54  *-------------------------------------------------------------------------
55  */
56 #ifndef ARRAY_H
57 #define ARRAY_H
58 
59 #include "fmgr.h"
60 
61 /*
62  * Arrays are varlena objects, so must meet the varlena convention that
63  * the first int32 of the object contains the total object size in bytes.
64  * Be sure to use VARSIZE() and SET_VARSIZE() to access it, though!
65  *
66  * CAUTION: if you change the header for ordinary arrays you will also
67  * need to change the headers for oidvector and int2vector!
68  */
69 typedef struct
70 {
71  int32 vl_len_; /* varlena header (do not touch directly!) */
72  int ndim; /* # of dimensions */
73  int32 dataoffset; /* offset to data, or 0 if no bitmap */
74  Oid elemtype; /* element type OID */
75 } ArrayType;
76 
77 /*
78  * working state for accumArrayResult() and friends
79  * note that the input must be scalars (legal array elements)
80  */
81 typedef struct ArrayBuildState
82 {
83  MemoryContext mcontext; /* where all the temp stuff is kept */
84  Datum *dvalues; /* array of accumulated Datums */
85  bool *dnulls; /* array of is-null flags for Datums */
86  int alen; /* allocated length of above arrays */
87  int nelems; /* number of valid entries in above arrays */
88  Oid element_type; /* data type of the Datums */
89  int16 typlen; /* needed info about datatype */
90  bool typbyval;
91  char typalign;
93 
94 /*
95  * working state for accumArrayResultArr() and friends
96  * note that the input must be arrays, and the same array type is returned
97  */
98 typedef struct ArrayBuildStateArr
99 {
100  MemoryContext mcontext; /* where all the temp stuff is kept */
101  char *data; /* accumulated data */
102  bits8 *nullbitmap; /* bitmap of is-null flags, or NULL if none */
103  int abytes; /* allocated length of "data" */
104  int nbytes; /* number of bytes used so far */
105  int aitems; /* allocated length of bitmap (in elements) */
106  int nitems; /* total number of elements in result */
107  int ndims; /* current dimensions of result */
108  int dims[MAXDIM];
109  int lbs[MAXDIM];
110  Oid array_type; /* data type of the arrays */
111  Oid element_type; /* data type of the array elements */
113 
114 /*
115  * working state for accumArrayResultAny() and friends
116  * these functions handle both cases
117  */
118 typedef struct ArrayBuildStateAny
119 {
120  /* Exactly one of these is not NULL: */
124 
125 /*
126  * structure to cache type metadata needed for array manipulation
127  */
128 typedef struct ArrayMetaState
129 {
132  bool typbyval;
133  char typalign;
134  char typdelim;
139 
140 /*
141  * private state needed by array_map (here because caller must provide it)
142  */
143 typedef struct ArrayMapState
144 {
147 } ArrayMapState;
148 
149 /* ArrayIteratorData is private in arrayfuncs.c */
151 
152 /*
153  * fmgr macros for array objects
154  */
155 #define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X))
156 #define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X))
157 #define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n))
158 #define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n))
159 #define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x)
160 
161 /*
162  * Access macros for array header fields.
163  *
164  * ARR_DIMS returns a pointer to an array of array dimensions (number of
165  * elements along the various array axes).
166  *
167  * ARR_LBOUND returns a pointer to an array of array lower bounds.
168  *
169  * That is: if the third axis of an array has elements 5 through 8, then
170  * ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
171  *
172  * Unlike C, the default lower bound is 1.
173  */
174 #define ARR_SIZE(a) VARSIZE(a)
175 #define ARR_NDIM(a) ((a)->ndim)
176 #define ARR_HASNULL(a) ((a)->dataoffset != 0)
177 #define ARR_ELEMTYPE(a) ((a)->elemtype)
178 
179 #define ARR_DIMS(a) \
180  ((int *) (((char *) (a)) + sizeof(ArrayType)))
181 #define ARR_LBOUND(a) \
182  ((int *) (((char *) (a)) + sizeof(ArrayType) + \
183  sizeof(int) * ARR_NDIM(a)))
184 
185 #define ARR_NULLBITMAP(a) \
186  (ARR_HASNULL(a) ? \
187  (bits8 *) (((char *) (a)) + sizeof(ArrayType) + \
188  2 * sizeof(int) * ARR_NDIM(a)) \
189  : (bits8 *) NULL)
190 
191 /*
192  * The total array header size (in bytes) for an array with the specified
193  * number of dimensions and total number of items.
194  */
195 #define ARR_OVERHEAD_NONULLS(ndims) \
196  MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims))
197 #define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \
198  MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \
199  ((nitems) + 7) / 8)
200 
201 #define ARR_DATA_OFFSET(a) \
202  (ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a)))
203 
204 /*
205  * Returns a pointer to the actual array data.
206  */
207 #define ARR_DATA_PTR(a) \
208  (((char *) (a)) + ARR_DATA_OFFSET(a))
209 
210 
211 /*
212  * GUC parameter
213  */
214 extern bool Array_nulls;
215 
216 /*
217  * prototypes for functions defined in arrayfuncs.c
218  */
250 
251 extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
252  int arraytyplen, int elmlen, bool elmbyval, char elmalign,
253  bool *isNull);
254 extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
255  Datum dataValue, bool isNull,
256  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
257 extern ArrayType *array_get_slice(ArrayType *array, int nSubscripts,
258  int *upperIndx, int *lowerIndx,
259  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
260 extern ArrayType *array_set_slice(ArrayType *array, int nSubscripts,
261  int *upperIndx, int *lowerIndx,
262  ArrayType *srcArray, bool isNull,
263  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
264 
265 extern Datum array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
266  ArrayMapState *amstate);
267 
268 extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
269  const bits8 *srcbitmap, int srcoffset,
270  int nitems);
271 
272 extern ArrayType *construct_array(Datum *elems, int nelems,
273  Oid elmtype,
274  int elmlen, bool elmbyval, char elmalign);
275 extern ArrayType *construct_md_array(Datum *elems,
276  bool *nulls,
277  int ndims,
278  int *dims,
279  int *lbs,
280  Oid elmtype, int elmlen, bool elmbyval, char elmalign);
281 extern ArrayType *construct_empty_array(Oid elmtype);
282 extern void deconstruct_array(ArrayType *array,
283  Oid elmtype,
284  int elmlen, bool elmbyval, char elmalign,
285  Datum **elemsp, bool **nullsp, int *nelemsp);
286 extern bool array_contains_nulls(ArrayType *array);
287 
288 extern ArrayBuildState *initArrayResult(Oid element_type,
289  MemoryContext rcontext);
291  Datum dvalue, bool disnull,
292  Oid element_type,
293  MemoryContext rcontext);
294 extern Datum makeArrayResult(ArrayBuildState *astate,
295  MemoryContext rcontext);
296 extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
297  int *dims, int *lbs, MemoryContext rcontext, bool release);
298 
299 extern ArrayBuildStateArr *initArrayResultArr(Oid array_type, Oid element_type,
300  MemoryContext rcontext);
302  Datum dvalue, bool disnull,
303  Oid array_type,
304  MemoryContext rcontext);
306  MemoryContext rcontext, bool release);
307 
308 extern ArrayBuildStateAny *initArrayResultAny(Oid input_type,
309  MemoryContext rcontext);
311  Datum dvalue, bool disnull,
312  Oid input_type,
313  MemoryContext rcontext);
315  MemoryContext rcontext, bool release);
316 
317 extern ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim);
318 extern bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull);
319 extern void array_free_iterator(ArrayIterator iterator);
320 
321 /*
322  * prototypes for functions defined in arrayutils.c
323  */
324 
325 extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
326 extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
327 extern int ArrayGetNItems(int ndim, const int *dims);
328 extern void mda_get_range(int n, int *span, const int *st, const int *endp);
329 extern void mda_get_prod(int n, const int *range, int *prod);
330 extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span);
331 extern int mda_next_tuple(int n, int *curr, const int *span);
332 extern int32 *ArrayGetIntegerTypmods(ArrayType *arr, int *n);
333 
334 /*
335  * prototypes for functions defined in array_userfuncs.c
336  */
339 
341  Oid element_type,
342  Datum element,
343  bool isNull,
344  int ndims);
345 
350 
351 /*
352  * prototypes for functions defined in array_typanalyze.c
353  */
355 
356 #endif /* ARRAY_H */