@@ -178,11 +178,12 @@ extern void fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
178
178
* The resulting datum can be accessed using VARSIZE_ANY() and VARDATA_ANY()
179
179
* (beware of multiple evaluations in those macros!)
180
180
*
181
- * WARNING: It is only safe to use pg_detoast_datum_packed() and
182
- * VARDATA_ANY() if you really don't care about the alignment. Either because
183
- * you're working with something like text where the alignment doesn't matter
184
- * or because you're not going to access its constituent parts and just use
185
- * things like memcpy on it anyways.
181
+ * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(),
182
+ * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call
183
+ * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16,
184
+ * int32 or wider field in the struct representing the datum layout requires
185
+ * aligned data. memcpy() is alignment-oblivious, as are most operations on
186
+ * datatypes, such as text, whose layout struct contains only char fields.
186
187
*
187
188
* Note: it'd be nice if these could be macros, but I see no way to do that
188
189
* without evaluating the arguments multiple times, which is NOT acceptable.
@@ -243,13 +244,9 @@ extern struct varlena *pg_detoast_datum_packed(struct varlena * datum);
243
244
/* and this if you can handle 1-byte-header datums: */
244
245
#define PG_GETARG_VARLENA_PP (n ) PG_DETOAST_DATUM_PACKED(PG_GETARG_DATUM(n))
245
246
/* DatumGetFoo macros for varlena types will typically look like this: */
246
- #define DatumGetByteaP (X ) ((bytea *) PG_DETOAST_DATUM(X))
247
247
#define DatumGetByteaPP (X ) ((bytea *) PG_DETOAST_DATUM_PACKED(X))
248
- #define DatumGetTextP (X ) ((text *) PG_DETOAST_DATUM(X))
249
248
#define DatumGetTextPP (X ) ((text *) PG_DETOAST_DATUM_PACKED(X))
250
- #define DatumGetBpCharP (X ) ((BpChar *) PG_DETOAST_DATUM(X))
251
249
#define DatumGetBpCharPP (X ) ((BpChar *) PG_DETOAST_DATUM_PACKED(X))
252
- #define DatumGetVarCharP (X ) ((VarChar *) PG_DETOAST_DATUM(X))
253
250
#define DatumGetVarCharPP (X ) ((VarChar *) PG_DETOAST_DATUM_PACKED(X))
254
251
#define DatumGetHeapTupleHeader (X ) ((HeapTupleHeader) PG_DETOAST_DATUM(X))
255
252
/* And we also offer variants that return an OK-to-write copy */
@@ -264,13 +261,9 @@ extern struct varlena *pg_detoast_datum_packed(struct varlena * datum);
264
261
#define DatumGetBpCharPSlice (X ,m ,n ) ((BpChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
265
262
#define DatumGetVarCharPSlice (X ,m ,n ) ((VarChar *) PG_DETOAST_DATUM_SLICE(X,m,n))
266
263
/* GETARG macros for varlena types will typically look like this: */
267
- #define PG_GETARG_BYTEA_P (n ) DatumGetByteaP(PG_GETARG_DATUM(n))
268
264
#define PG_GETARG_BYTEA_PP (n ) DatumGetByteaPP(PG_GETARG_DATUM(n))
269
- #define PG_GETARG_TEXT_P (n ) DatumGetTextP(PG_GETARG_DATUM(n))
270
265
#define PG_GETARG_TEXT_PP (n ) DatumGetTextPP(PG_GETARG_DATUM(n))
271
- #define PG_GETARG_BPCHAR_P (n ) DatumGetBpCharP(PG_GETARG_DATUM(n))
272
266
#define PG_GETARG_BPCHAR_PP (n ) DatumGetBpCharPP(PG_GETARG_DATUM(n))
273
- #define PG_GETARG_VARCHAR_P (n ) DatumGetVarCharP(PG_GETARG_DATUM(n))
274
267
#define PG_GETARG_VARCHAR_PP (n ) DatumGetVarCharPP(PG_GETARG_DATUM(n))
275
268
#define PG_GETARG_HEAPTUPLEHEADER (n ) DatumGetHeapTupleHeader(PG_GETARG_DATUM(n))
276
269
/* And we also offer variants that return an OK-to-write copy */
@@ -284,6 +277,21 @@ extern struct varlena *pg_detoast_datum_packed(struct varlena * datum);
284
277
#define PG_GETARG_TEXT_P_SLICE (n ,a ,b ) DatumGetTextPSlice(PG_GETARG_DATUM(n),a,b)
285
278
#define PG_GETARG_BPCHAR_P_SLICE (n ,a ,b ) DatumGetBpCharPSlice(PG_GETARG_DATUM(n),a,b)
286
279
#define PG_GETARG_VARCHAR_P_SLICE (n ,a ,b ) DatumGetVarCharPSlice(PG_GETARG_DATUM(n),a,b)
280
+ /*
281
+ * Obsolescent variants that guarantee INT alignment for the return value.
282
+ * Few operations on these particular types need alignment, mainly operations
283
+ * that cast the VARDATA pointer to a type like int16[]. Most code should use
284
+ * the ...PP(X) counterpart. Nonetheless, these appear frequently in code
285
+ * predating the PostgreSQL 8.3 introduction of the ...PP(X) variants.
286
+ */
287
+ #define DatumGetByteaP (X ) ((bytea *) PG_DETOAST_DATUM(X))
288
+ #define DatumGetTextP (X ) ((text *) PG_DETOAST_DATUM(X))
289
+ #define DatumGetBpCharP (X ) ((BpChar *) PG_DETOAST_DATUM(X))
290
+ #define DatumGetVarCharP (X ) ((VarChar *) PG_DETOAST_DATUM(X))
291
+ #define PG_GETARG_BYTEA_P (n ) DatumGetByteaP(PG_GETARG_DATUM(n))
292
+ #define PG_GETARG_TEXT_P (n ) DatumGetTextP(PG_GETARG_DATUM(n))
293
+ #define PG_GETARG_BPCHAR_P (n ) DatumGetBpCharP(PG_GETARG_DATUM(n))
294
+ #define PG_GETARG_VARCHAR_P (n ) DatumGetVarCharP(PG_GETARG_DATUM(n))
287
295
288
296
/* To return a NULL do this: */
289
297
#define PG_RETURN_NULL () \
0 commit comments