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  */
80 typedef struct ArrayBuildState
81 {
82  MemoryContext mcontext; /* where all the temp stuff is kept */
83  Datum *dvalues; /* array of accumulated Datums */
84  bool *dnulls; /* array of is-null flags for Datums */
85  int alen; /* allocated length of above arrays */
86  int nelems; /* number of valid entries in above arrays */
87  Oid element_type; /* data type of the Datums */
88  int16 typlen; /* needed info about datatype */
89  bool typbyval;
90  char typalign;
92 
93 /*
94  * structure to cache type metadata needed for array manipulation
95  */
96 typedef struct ArrayMetaState
97 {
100  bool typbyval;
101  char typalign;
102  char typdelim;
107 
108 /*
109  * private state needed by array_map (here because caller must provide it)
110  */
111 typedef struct ArrayMapState
112 {
115 } ArrayMapState;
116 
117 /* ArrayIteratorData is private in arrayfuncs.c */
119 
120 /*
121  * fmgr macros for array objects
122  */
123 #define DatumGetArrayTypeP(X) ((ArrayType *) PG_DETOAST_DATUM(X))
124 #define DatumGetArrayTypePCopy(X) ((ArrayType *) PG_DETOAST_DATUM_COPY(X))
125 #define PG_GETARG_ARRAYTYPE_P(n) DatumGetArrayTypeP(PG_GETARG_DATUM(n))
126 #define PG_GETARG_ARRAYTYPE_P_COPY(n) DatumGetArrayTypePCopy(PG_GETARG_DATUM(n))
127 #define PG_RETURN_ARRAYTYPE_P(x) PG_RETURN_POINTER(x)
128 
129 /*
130  * Access macros for array header fields.
131  *
132  * ARR_DIMS returns a pointer to an array of array dimensions (number of
133  * elements along the various array axes).
134  *
135  * ARR_LBOUND returns a pointer to an array of array lower bounds.
136  *
137  * That is: if the third axis of an array has elements 5 through 8, then
138  * ARR_DIMS(a)[2] == 4 and ARR_LBOUND(a)[2] == 5.
139  *
140  * Unlike C, the default lower bound is 1.
141  */
142 #define ARR_SIZE(a) VARSIZE(a)
143 #define ARR_NDIM(a) ((a)->ndim)
144 #define ARR_HASNULL(a) ((a)->dataoffset != 0)
145 #define ARR_ELEMTYPE(a) ((a)->elemtype)
146 
147 #define ARR_DIMS(a) \
148  ((int *) (((char *) (a)) + sizeof(ArrayType)))
149 #define ARR_LBOUND(a) \
150  ((int *) (((char *) (a)) + sizeof(ArrayType) + \
151  sizeof(int) * ARR_NDIM(a)))
152 
153 #define ARR_NULLBITMAP(a) \
154  (ARR_HASNULL(a) ? \
155  (bits8 *) (((char *) (a)) + sizeof(ArrayType) + \
156  2 * sizeof(int) * ARR_NDIM(a)) \
157  : (bits8 *) NULL)
158 
159 /*
160  * The total array header size (in bytes) for an array with the specified
161  * number of dimensions and total number of items.
162  */
163 #define ARR_OVERHEAD_NONULLS(ndims) \
164  MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims))
165 #define ARR_OVERHEAD_WITHNULLS(ndims, nitems) \
166  MAXALIGN(sizeof(ArrayType) + 2 * sizeof(int) * (ndims) + \
167  ((nitems) + 7) / 8)
168 
169 #define ARR_DATA_OFFSET(a) \
170  (ARR_HASNULL(a) ? (a)->dataoffset : ARR_OVERHEAD_NONULLS(ARR_NDIM(a)))
171 
172 /*
173  * Returns a pointer to the actual array data.
174  */
175 #define ARR_DATA_PTR(a) \
176  (((char *) (a)) + ARR_DATA_OFFSET(a))
177 
178 
179 /*
180  * GUC parameter
181  */
182 extern bool Array_nulls;
183 
184 /*
185  * prototypes for functions defined in arrayfuncs.c
186  */
218 
219 extern Datum array_ref(ArrayType *array, int nSubscripts, int *indx,
220  int arraytyplen, int elmlen, bool elmbyval, char elmalign,
221  bool *isNull);
222 extern ArrayType *array_set(ArrayType *array, int nSubscripts, int *indx,
223  Datum dataValue, bool isNull,
224  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
225 extern ArrayType *array_get_slice(ArrayType *array, int nSubscripts,
226  int *upperIndx, int *lowerIndx,
227  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
228 extern ArrayType *array_set_slice(ArrayType *array, int nSubscripts,
229  int *upperIndx, int *lowerIndx,
230  ArrayType *srcArray, bool isNull,
231  int arraytyplen, int elmlen, bool elmbyval, char elmalign);
232 
233 extern Datum array_map(FunctionCallInfo fcinfo, Oid inpType, Oid retType,
234  ArrayMapState *amstate);
235 
236 extern void array_bitmap_copy(bits8 *destbitmap, int destoffset,
237  const bits8 *srcbitmap, int srcoffset,
238  int nitems);
239 
240 extern ArrayType *construct_array(Datum *elems, int nelems,
241  Oid elmtype,
242  int elmlen, bool elmbyval, char elmalign);
243 extern ArrayType *construct_md_array(Datum *elems,
244  bool *nulls,
245  int ndims,
246  int *dims,
247  int *lbs,
248  Oid elmtype, int elmlen, bool elmbyval, char elmalign);
249 extern ArrayType *construct_empty_array(Oid elmtype);
250 extern void deconstruct_array(ArrayType *array,
251  Oid elmtype,
252  int elmlen, bool elmbyval, char elmalign,
253  Datum **elemsp, bool **nullsp, int *nelemsp);
254 extern bool array_contains_nulls(ArrayType *array);
256  Datum dvalue, bool disnull,
257  Oid element_type,
258  MemoryContext rcontext);
259 extern Datum makeArrayResult(ArrayBuildState *astate,
260  MemoryContext rcontext);
261 extern Datum makeMdArrayResult(ArrayBuildState *astate, int ndims,
262  int *dims, int *lbs, MemoryContext rcontext, bool release);
263 
264 extern ArrayIterator array_create_iterator(ArrayType *arr, int slice_ndim);
265 extern bool array_iterate(ArrayIterator iterator, Datum *value, bool *isnull);
266 extern void array_free_iterator(ArrayIterator iterator);
267 
268 /*
269  * prototypes for functions defined in arrayutils.c
270  */
271 
272 extern int ArrayGetOffset(int n, const int *dim, const int *lb, const int *indx);
273 extern int ArrayGetOffset0(int n, const int *tup, const int *scale);
274 extern int ArrayGetNItems(int ndim, const int *dims);
275 extern void mda_get_range(int n, int *span, const int *st, const int *endp);
276 extern void mda_get_prod(int n, const int *range, int *prod);
277 extern void mda_get_offset_values(int n, int *dist, const int *prod, const int *span);
278 extern int mda_next_tuple(int n, int *curr, const int *span);
279 extern int32 *ArrayGetIntegerTypmods(ArrayType *arr, int *n);
280 
281 /*
282  * prototypes for functions defined in array_userfuncs.c
283  */
286 
288  Oid element_type,
289  Datum element,
290  bool isNull,
291  int ndims);
292 
295 
296 /*
297  * prototypes for functions defined in array_typanalyze.c
298  */
300 
301 #endif /* ARRAY_H */