mirror of https://github.com/postgres/postgres
This patch introduces the ability for complex datatypes to have an in-memory representation that is different from their on-disk format. On-disk formats are typically optimized for minimal size, and in any case they can't contain pointers, so they are often not well-suited for computation. Now a datatype can invent an "expanded" in-memory format that is better suited for its operations, and then pass that around among the C functions that operate on the datatype. There are also provisions (rudimentary as yet) to allow an expanded object to be modified in-place under suitable conditions, so that operations like assignment to an element of an array need not involve copying the entire array. The initial application for this feature is arrays, but it is not hard to foresee using it for other container types like JSON, XML and hstore. I have hopes that it will be useful to PostGIS as well. In this initial implementation, a few heuristics have been hard-wired into plpgsql to improve performance for arrays that are stored in plpgsql variables. We would like to generalize those hacks so that other datatypes can obtain similar improvements, but figuring out some appropriate APIs is left as a task for future work. (The heuristics themselves are probably not optimal yet, either, as they sometimes force expansion of arrays that would be better left alone.) Preliminary performance testing shows impressive speed gains for plpgsql functions that do element-by-element access or update of large arrays. There are other cases that get a little slower, as a result of added array format conversions; but we can hope to improve anything that's annoyingly bad. In any case most applications should see a net win. Tom Lane, reviewed by Andres Freundpull/14/head
parent
8a2e1edd2b
commit
1dc5ebc907
@ -0,0 +1,455 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* array_expanded.c |
||||
* Basic functions for manipulating expanded arrays. |
||||
* |
||||
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/utils/adt/array_expanded.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "access/tupmacs.h" |
||||
#include "utils/array.h" |
||||
#include "utils/lsyscache.h" |
||||
#include "utils/memutils.h" |
||||
|
||||
|
||||
/* "Methods" required for an expanded object */ |
||||
static Size EA_get_flat_size(ExpandedObjectHeader *eohptr); |
||||
static void EA_flatten_into(ExpandedObjectHeader *eohptr, |
||||
void *result, Size allocated_size); |
||||
|
||||
static const ExpandedObjectMethods EA_methods = |
||||
{ |
||||
EA_get_flat_size, |
||||
EA_flatten_into |
||||
}; |
||||
|
||||
/* Other local functions */ |
||||
static void copy_byval_expanded_array(ExpandedArrayHeader *eah, |
||||
ExpandedArrayHeader *oldeah); |
||||
|
||||
|
||||
/*
|
||||
* expand_array: convert an array Datum into an expanded array |
||||
* |
||||
* The expanded object will be a child of parentcontext. |
||||
* |
||||
* Some callers can provide cache space to avoid repeated lookups of element |
||||
* type data across calls; if so, pass a metacache pointer, making sure that |
||||
* metacache->element_type is initialized to InvalidOid before first call. |
||||
* If no cross-call caching is required, pass NULL for metacache. |
||||
*/ |
||||
Datum |
||||
expand_array(Datum arraydatum, MemoryContext parentcontext, |
||||
ArrayMetaState *metacache) |
||||
{ |
||||
ArrayType *array; |
||||
ExpandedArrayHeader *eah; |
||||
MemoryContext objcxt; |
||||
MemoryContext oldcxt; |
||||
ArrayMetaState fakecache; |
||||
|
||||
/*
|
||||
* Allocate private context for expanded object. We start by assuming |
||||
* that the array won't be very large; but if it does grow a lot, don't |
||||
* constrain aset.c's large-context behavior. |
||||
*/ |
||||
objcxt = AllocSetContextCreate(parentcontext, |
||||
"expanded array", |
||||
ALLOCSET_SMALL_MINSIZE, |
||||
ALLOCSET_SMALL_INITSIZE, |
||||
ALLOCSET_DEFAULT_MAXSIZE); |
||||
|
||||
/* Set up expanded array header */ |
||||
eah = (ExpandedArrayHeader *) |
||||
MemoryContextAlloc(objcxt, sizeof(ExpandedArrayHeader)); |
||||
|
||||
EOH_init_header(&eah->hdr, &EA_methods, objcxt); |
||||
eah->ea_magic = EA_MAGIC; |
||||
|
||||
/* If the source is an expanded array, we may be able to optimize */ |
||||
if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(arraydatum))) |
||||
{ |
||||
ExpandedArrayHeader *oldeah = (ExpandedArrayHeader *) DatumGetEOHP(arraydatum); |
||||
|
||||
Assert(oldeah->ea_magic == EA_MAGIC); |
||||
|
||||
/*
|
||||
* Update caller's cache if provided; we don't need it this time, but |
||||
* next call might be for a non-expanded source array. Furthermore, |
||||
* if the caller didn't provide a cache area, use some local storage |
||||
* to cache anyway, thereby avoiding a catalog lookup in the case |
||||
* where we fall through to the flat-copy code path. |
||||
*/ |
||||
if (metacache == NULL) |
||||
metacache = &fakecache; |
||||
metacache->element_type = oldeah->element_type; |
||||
metacache->typlen = oldeah->typlen; |
||||
metacache->typbyval = oldeah->typbyval; |
||||
metacache->typalign = oldeah->typalign; |
||||
|
||||
/*
|
||||
* If element type is pass-by-value and we have a Datum-array |
||||
* representation, just copy the source's metadata and Datum/isnull |
||||
* arrays. The original flat array, if present at all, adds no |
||||
* additional information so we need not copy it. |
||||
*/ |
||||
if (oldeah->typbyval && oldeah->dvalues != NULL) |
||||
{ |
||||
copy_byval_expanded_array(eah, oldeah); |
||||
/* return a R/W pointer to the expanded array */ |
||||
return EOHPGetRWDatum(&eah->hdr); |
||||
} |
||||
|
||||
/*
|
||||
* Otherwise, either we have only a flat representation or the |
||||
* elements are pass-by-reference. In either case, the best thing |
||||
* seems to be to copy the source as a flat representation and then |
||||
* deconstruct that later if necessary. For the pass-by-ref case, we |
||||
* could perhaps save some cycles with custom code that generates the |
||||
* deconstructed representation in parallel with copying the values, |
||||
* but it would be a lot of extra code for fairly marginal gain. So, |
||||
* fall through into the flat-source code path. |
||||
*/ |
||||
} |
||||
|
||||
/*
|
||||
* Detoast and copy source array into private context, as a flat array. |
||||
* |
||||
* Note that this coding risks leaking some memory in the private context |
||||
* if we have to fetch data from a TOAST table; however, experimentation |
||||
* says that the leak is minimal. Doing it this way saves a copy step, |
||||
* which seems worthwhile, especially if the array is large enough to need |
||||
* external storage. |
||||
*/ |
||||
oldcxt = MemoryContextSwitchTo(objcxt); |
||||
array = DatumGetArrayTypePCopy(arraydatum); |
||||
MemoryContextSwitchTo(oldcxt); |
||||
|
||||
eah->ndims = ARR_NDIM(array); |
||||
/* note these pointers point into the fvalue header! */ |
||||
eah->dims = ARR_DIMS(array); |
||||
eah->lbound = ARR_LBOUND(array); |
||||
|
||||
/* Save array's element-type data for possible use later */ |
||||
eah->element_type = ARR_ELEMTYPE(array); |
||||
if (metacache && metacache->element_type == eah->element_type) |
||||
{ |
||||
/* We have a valid cache of representational data */ |
||||
eah->typlen = metacache->typlen; |
||||
eah->typbyval = metacache->typbyval; |
||||
eah->typalign = metacache->typalign; |
||||
} |
||||
else |
||||
{ |
||||
/* No, so look it up */ |
||||
get_typlenbyvalalign(eah->element_type, |
||||
&eah->typlen, |
||||
&eah->typbyval, |
||||
&eah->typalign); |
||||
/* Update cache if provided */ |
||||
if (metacache) |
||||
{ |
||||
metacache->element_type = eah->element_type; |
||||
metacache->typlen = eah->typlen; |
||||
metacache->typbyval = eah->typbyval; |
||||
metacache->typalign = eah->typalign; |
||||
} |
||||
} |
||||
|
||||
/* we don't make a deconstructed representation now */ |
||||
eah->dvalues = NULL; |
||||
eah->dnulls = NULL; |
||||
eah->dvalueslen = 0; |
||||
eah->nelems = 0; |
||||
eah->flat_size = 0; |
||||
|
||||
/* remember we have a flat representation */ |
||||
eah->fvalue = array; |
||||
eah->fstartptr = ARR_DATA_PTR(array); |
||||
eah->fendptr = ((char *) array) + ARR_SIZE(array); |
||||
|
||||
/* return a R/W pointer to the expanded array */ |
||||
return EOHPGetRWDatum(&eah->hdr); |
||||
} |
||||
|
||||
/*
|
||||
* helper for expand_array(): copy pass-by-value Datum-array representation |
||||
*/ |
||||
static void |
||||
copy_byval_expanded_array(ExpandedArrayHeader *eah, |
||||
ExpandedArrayHeader *oldeah) |
||||
{ |
||||
MemoryContext objcxt = eah->hdr.eoh_context; |
||||
int ndims = oldeah->ndims; |
||||
int dvalueslen = oldeah->dvalueslen; |
||||
|
||||
/* Copy array dimensionality information */ |
||||
eah->ndims = ndims; |
||||
/* We can alloc both dimensionality arrays with one palloc */ |
||||
eah->dims = (int *) MemoryContextAlloc(objcxt, ndims * 2 * sizeof(int)); |
||||
eah->lbound = eah->dims + ndims; |
||||
/* .. but don't assume the source's arrays are contiguous */ |
||||
memcpy(eah->dims, oldeah->dims, ndims * sizeof(int)); |
||||
memcpy(eah->lbound, oldeah->lbound, ndims * sizeof(int)); |
||||
|
||||
/* Copy element-type data */ |
||||
eah->element_type = oldeah->element_type; |
||||
eah->typlen = oldeah->typlen; |
||||
eah->typbyval = oldeah->typbyval; |
||||
eah->typalign = oldeah->typalign; |
||||
|
||||
/* Copy the deconstructed representation */ |
||||
eah->dvalues = (Datum *) MemoryContextAlloc(objcxt, |
||||
dvalueslen * sizeof(Datum)); |
||||
memcpy(eah->dvalues, oldeah->dvalues, dvalueslen * sizeof(Datum)); |
||||
if (oldeah->dnulls) |
||||
{ |
||||
eah->dnulls = (bool *) MemoryContextAlloc(objcxt, |
||||
dvalueslen * sizeof(bool)); |
||||
memcpy(eah->dnulls, oldeah->dnulls, dvalueslen * sizeof(bool)); |
||||
} |
||||
else |
||||
eah->dnulls = NULL; |
||||
eah->dvalueslen = dvalueslen; |
||||
eah->nelems = oldeah->nelems; |
||||
eah->flat_size = oldeah->flat_size; |
||||
|
||||
/* we don't make a flat representation */ |
||||
eah->fvalue = NULL; |
||||
eah->fstartptr = NULL; |
||||
eah->fendptr = NULL; |
||||
} |
||||
|
||||
/*
|
||||
* get_flat_size method for expanded arrays |
||||
*/ |
||||
static Size |
||||
EA_get_flat_size(ExpandedObjectHeader *eohptr) |
||||
{ |
||||
ExpandedArrayHeader *eah = (ExpandedArrayHeader *) eohptr; |
||||
int nelems; |
||||
int ndims; |
||||
Datum *dvalues; |
||||
bool *dnulls; |
||||
Size nbytes; |
||||
int i; |
||||
|
||||
Assert(eah->ea_magic == EA_MAGIC); |
||||
|
||||
/* Easy if we have a valid flattened value */ |
||||
if (eah->fvalue) |
||||
return ARR_SIZE(eah->fvalue); |
||||
|
||||
/* If we have a cached size value, believe that */ |
||||
if (eah->flat_size) |
||||
return eah->flat_size; |
||||
|
||||
/*
|
||||
* Compute space needed by examining dvalues/dnulls. Note that the result |
||||
* array will have a nulls bitmap if dnulls isn't NULL, even if the array |
||||
* doesn't actually contain any nulls now. |
||||
*/ |
||||
nelems = eah->nelems; |
||||
ndims = eah->ndims; |
||||
Assert(nelems == ArrayGetNItems(ndims, eah->dims)); |
||||
dvalues = eah->dvalues; |
||||
dnulls = eah->dnulls; |
||||
nbytes = 0; |
||||
for (i = 0; i < nelems; i++) |
||||
{ |
||||
if (dnulls && dnulls[i]) |
||||
continue; |
||||
nbytes = att_addlength_datum(nbytes, eah->typlen, dvalues[i]); |
||||
nbytes = att_align_nominal(nbytes, eah->typalign); |
||||
/* check for overflow of total request */ |
||||
if (!AllocSizeIsValid(nbytes)) |
||||
ereport(ERROR, |
||||
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), |
||||
errmsg("array size exceeds the maximum allowed (%d)", |
||||
(int) MaxAllocSize))); |
||||
} |
||||
|
||||
if (dnulls) |
||||
nbytes += ARR_OVERHEAD_WITHNULLS(ndims, nelems); |
||||
else |
||||
nbytes += ARR_OVERHEAD_NONULLS(ndims); |
||||
|
||||
/* cache for next time */ |
||||
eah->flat_size = nbytes; |
||||
|
||||
return nbytes; |
||||
} |
||||
|
||||
/*
|
||||
* flatten_into method for expanded arrays |
||||
*/ |
||||
static void |
||||
EA_flatten_into(ExpandedObjectHeader *eohptr, |
||||
void *result, Size allocated_size) |
||||
{ |
||||
ExpandedArrayHeader *eah = (ExpandedArrayHeader *) eohptr; |
||||
ArrayType *aresult = (ArrayType *) result; |
||||
int nelems; |
||||
int ndims; |
||||
int32 dataoffset; |
||||
|
||||
Assert(eah->ea_magic == EA_MAGIC); |
||||
|
||||
/* Easy if we have a valid flattened value */ |
||||
if (eah->fvalue) |
||||
{ |
||||
Assert(allocated_size == ARR_SIZE(eah->fvalue)); |
||||
memcpy(result, eah->fvalue, allocated_size); |
||||
return; |
||||
} |
||||
|
||||
/* Else allocation should match previous get_flat_size result */ |
||||
Assert(allocated_size == eah->flat_size); |
||||
|
||||
/* Fill result array from dvalues/dnulls */ |
||||
nelems = eah->nelems; |
||||
ndims = eah->ndims; |
||||
|
||||
if (eah->dnulls) |
||||
dataoffset = ARR_OVERHEAD_WITHNULLS(ndims, nelems); |
||||
else |
||||
dataoffset = 0; /* marker for no null bitmap */ |
||||
|
||||
/* We must ensure that any pad space is zero-filled */ |
||||
memset(aresult, 0, allocated_size); |
||||
|
||||
SET_VARSIZE(aresult, allocated_size); |
||||
aresult->ndim = ndims; |
||||
aresult->dataoffset = dataoffset; |
||||
aresult->elemtype = eah->element_type; |
||||
memcpy(ARR_DIMS(aresult), eah->dims, ndims * sizeof(int)); |
||||
memcpy(ARR_LBOUND(aresult), eah->lbound, ndims * sizeof(int)); |
||||
|
||||
CopyArrayEls(aresult, |
||||
eah->dvalues, eah->dnulls, nelems, |
||||
eah->typlen, eah->typbyval, eah->typalign, |
||||
false); |
||||
} |
||||
|
||||
/*
|
||||
* Argument fetching support code |
||||
*/ |
||||
|
||||
/*
|
||||
* DatumGetExpandedArray: get a writable expanded array from an input argument |
||||
* |
||||
* Caution: if the input is a read/write pointer, this returns the input |
||||
* argument; so callers must be sure that their changes are "safe", that is |
||||
* they cannot leave the array in a corrupt state. |
||||
*/ |
||||
ExpandedArrayHeader * |
||||
DatumGetExpandedArray(Datum d) |
||||
{ |
||||
/* If it's a writable expanded array already, just return it */ |
||||
if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))) |
||||
{ |
||||
ExpandedArrayHeader *eah = (ExpandedArrayHeader *) DatumGetEOHP(d); |
||||
|
||||
Assert(eah->ea_magic == EA_MAGIC); |
||||
return eah; |
||||
} |
||||
|
||||
/* Else expand the hard way */ |
||||
d = expand_array(d, CurrentMemoryContext, NULL); |
||||
return (ExpandedArrayHeader *) DatumGetEOHP(d); |
||||
} |
||||
|
||||
/*
|
||||
* As above, when caller has the ability to cache element type info |
||||
*/ |
||||
ExpandedArrayHeader * |
||||
DatumGetExpandedArrayX(Datum d, ArrayMetaState *metacache) |
||||
{ |
||||
/* If it's a writable expanded array already, just return it */ |
||||
if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))) |
||||
{ |
||||
ExpandedArrayHeader *eah = (ExpandedArrayHeader *) DatumGetEOHP(d); |
||||
|
||||
Assert(eah->ea_magic == EA_MAGIC); |
||||
/* Update cache if provided */ |
||||
if (metacache) |
||||
{ |
||||
metacache->element_type = eah->element_type; |
||||
metacache->typlen = eah->typlen; |
||||
metacache->typbyval = eah->typbyval; |
||||
metacache->typalign = eah->typalign; |
||||
} |
||||
return eah; |
||||
} |
||||
|
||||
/* Else expand using caller's cache if any */ |
||||
d = expand_array(d, CurrentMemoryContext, metacache); |
||||
return (ExpandedArrayHeader *) DatumGetEOHP(d); |
||||
} |
||||
|
||||
/*
|
||||
* DatumGetAnyArray: return either an expanded array or a detoasted varlena |
||||
* array. The result must not be modified in-place. |
||||
*/ |
||||
AnyArrayType * |
||||
DatumGetAnyArray(Datum d) |
||||
{ |
||||
ExpandedArrayHeader *eah; |
||||
|
||||
/*
|
||||
* If it's an expanded array (RW or RO), return the header pointer. |
||||
*/ |
||||
if (VARATT_IS_EXTERNAL_EXPANDED(DatumGetPointer(d))) |
||||
{ |
||||
eah = (ExpandedArrayHeader *) DatumGetEOHP(d); |
||||
Assert(eah->ea_magic == EA_MAGIC); |
||||
return (AnyArrayType *) eah; |
||||
} |
||||
|
||||
/* Else do regular detoasting as needed */ |
||||
return (AnyArrayType *) PG_DETOAST_DATUM(d); |
||||
} |
||||
|
||||
/*
|
||||
* Create the Datum/isnull representation of an expanded array object |
||||
* if we didn't do so previously |
||||
*/ |
||||
void |
||||
deconstruct_expanded_array(ExpandedArrayHeader *eah) |
||||
{ |
||||
if (eah->dvalues == NULL) |
||||
{ |
||||
MemoryContext oldcxt = MemoryContextSwitchTo(eah->hdr.eoh_context); |
||||
Datum *dvalues; |
||||
bool *dnulls; |
||||
int nelems; |
||||
|
||||
dnulls = NULL; |
||||
deconstruct_array(eah->fvalue, |
||||
eah->element_type, |
||||
eah->typlen, eah->typbyval, eah->typalign, |
||||
&dvalues, |
||||
ARR_HASNULL(eah->fvalue) ? &dnulls : NULL, |
||||
&nelems); |
||||
|
||||
/*
|
||||
* Update header only after successful completion of this step. If |
||||
* deconstruct_array fails partway through, worst consequence is some |
||||
* leaked memory in the object's context. If the caller fails at a |
||||
* later point, that's fine, since the deconstructed representation is |
||||
* valid anyhow. |
||||
*/ |
||||
eah->dvalues = dvalues; |
||||
eah->dnulls = dnulls; |
||||
eah->dvalueslen = eah->nelems = nelems; |
||||
MemoryContextSwitchTo(oldcxt); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,163 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* expandeddatum.c |
||||
* Support functions for "expanded" value representations. |
||||
* |
||||
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* |
||||
* IDENTIFICATION |
||||
* src/backend/utils/adt/expandeddatum.c |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#include "postgres.h" |
||||
|
||||
#include "utils/expandeddatum.h" |
||||
#include "utils/memutils.h" |
||||
|
||||
/*
|
||||
* DatumGetEOHP |
||||
* |
||||
* Given a Datum that is an expanded-object reference, extract the pointer. |
||||
* |
||||
* This is a bit tedious since the pointer may not be properly aligned; |
||||
* compare VARATT_EXTERNAL_GET_POINTER(). |
||||
*/ |
||||
ExpandedObjectHeader * |
||||
DatumGetEOHP(Datum d) |
||||
{ |
||||
varattrib_1b_e *datum = (varattrib_1b_e *) DatumGetPointer(d); |
||||
varatt_expanded ptr; |
||||
|
||||
Assert(VARATT_IS_EXTERNAL_EXPANDED(datum)); |
||||
memcpy(&ptr, VARDATA_EXTERNAL(datum), sizeof(ptr)); |
||||
Assert(VARATT_IS_EXPANDED_HEADER(ptr.eohptr)); |
||||
return ptr.eohptr; |
||||
} |
||||
|
||||
/*
|
||||
* EOH_init_header |
||||
* |
||||
* Initialize the common header of an expanded object. |
||||
* |
||||
* The main thing this encapsulates is initializing the TOAST pointers. |
||||
*/ |
||||
void |
||||
EOH_init_header(ExpandedObjectHeader *eohptr, |
||||
const ExpandedObjectMethods *methods, |
||||
MemoryContext obj_context) |
||||
{ |
||||
varatt_expanded ptr; |
||||
|
||||
eohptr->vl_len_ = EOH_HEADER_MAGIC; |
||||
eohptr->eoh_methods = methods; |
||||
eohptr->eoh_context = obj_context; |
||||
|
||||
ptr.eohptr = eohptr; |
||||
|
||||
SET_VARTAG_EXTERNAL(eohptr->eoh_rw_ptr, VARTAG_EXPANDED_RW); |
||||
memcpy(VARDATA_EXTERNAL(eohptr->eoh_rw_ptr), &ptr, sizeof(ptr)); |
||||
|
||||
SET_VARTAG_EXTERNAL(eohptr->eoh_ro_ptr, VARTAG_EXPANDED_RO); |
||||
memcpy(VARDATA_EXTERNAL(eohptr->eoh_ro_ptr), &ptr, sizeof(ptr)); |
||||
} |
||||
|
||||
/*
|
||||
* EOH_get_flat_size |
||||
* EOH_flatten_into |
||||
* |
||||
* Convenience functions for invoking the "methods" of an expanded object. |
||||
*/ |
||||
|
||||
Size |
||||
EOH_get_flat_size(ExpandedObjectHeader *eohptr) |
||||
{ |
||||
return (*eohptr->eoh_methods->get_flat_size) (eohptr); |
||||
} |
||||
|
||||
void |
||||
EOH_flatten_into(ExpandedObjectHeader *eohptr, |
||||
void *result, Size allocated_size) |
||||
{ |
||||
(*eohptr->eoh_methods->flatten_into) (eohptr, result, allocated_size); |
||||
} |
||||
|
||||
/*
|
||||
* Does the Datum represent a writable expanded object? |
||||
*/ |
||||
bool |
||||
DatumIsReadWriteExpandedObject(Datum d, bool isnull, int16 typlen) |
||||
{ |
||||
/* Reject if it's NULL or not a varlena type */ |
||||
if (isnull || typlen != -1) |
||||
return false; |
||||
|
||||
/* Reject if not a read-write expanded-object pointer */ |
||||
if (!VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))) |
||||
return false; |
||||
|
||||
return true; |
||||
} |
||||
|
||||
/*
|
||||
* If the Datum represents a R/W expanded object, change it to R/O. |
||||
* Otherwise return the original Datum. |
||||
*/ |
||||
Datum |
||||
MakeExpandedObjectReadOnly(Datum d, bool isnull, int16 typlen) |
||||
{ |
||||
ExpandedObjectHeader *eohptr; |
||||
|
||||
/* Nothing to do if it's NULL or not a varlena type */ |
||||
if (isnull || typlen != -1) |
||||
return d; |
||||
|
||||
/* Nothing to do if not a read-write expanded-object pointer */ |
||||
if (!VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))) |
||||
return d; |
||||
|
||||
/* Now safe to extract the object pointer */ |
||||
eohptr = DatumGetEOHP(d); |
||||
|
||||
/* Return the built-in read-only pointer instead of given pointer */ |
||||
return EOHPGetRODatum(eohptr); |
||||
} |
||||
|
||||
/*
|
||||
* Transfer ownership of an expanded object to a new parent memory context. |
||||
* The object must be referenced by a R/W pointer, and what we return is |
||||
* always its "standard" R/W pointer, which is certain to have the same |
||||
* lifespan as the object itself. (The passed-in pointer might not, and |
||||
* in any case wouldn't provide a unique identifier if it's not that one.) |
||||
*/ |
||||
Datum |
||||
TransferExpandedObject(Datum d, MemoryContext new_parent) |
||||
{ |
||||
ExpandedObjectHeader *eohptr = DatumGetEOHP(d); |
||||
|
||||
/* Assert caller gave a R/W pointer */ |
||||
Assert(VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))); |
||||
|
||||
/* Transfer ownership */ |
||||
MemoryContextSetParent(eohptr->eoh_context, new_parent); |
||||
|
||||
/* Return the object's standard read-write pointer */ |
||||
return EOHPGetRWDatum(eohptr); |
||||
} |
||||
|
||||
/*
|
||||
* Delete an expanded object (must be referenced by a R/W pointer). |
||||
*/ |
||||
void |
||||
DeleteExpandedObject(Datum d) |
||||
{ |
||||
ExpandedObjectHeader *eohptr = DatumGetEOHP(d); |
||||
|
||||
/* Assert caller gave a R/W pointer */ |
||||
Assert(VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d))); |
||||
|
||||
/* Kill it */ |
||||
MemoryContextDelete(eohptr->eoh_context); |
||||
} |
@ -0,0 +1,133 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* arrayaccess.h |
||||
* Declarations for element-by-element access to Postgres arrays. |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/utils/arrayaccess.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef ARRAYACCESS_H |
||||
#define ARRAYACCESS_H |
||||
|
||||
#include "access/tupmacs.h" |
||||
#include "utils/array.h" |
||||
|
||||
|
||||
/*
|
||||
* Functions for iterating through elements of a flat or expanded array. |
||||
* These require a state struct "array_iter iter". |
||||
* |
||||
* Use "array_iter_setup(&iter, arrayptr);" to prepare to iterate, and |
||||
* "datumvar = array_iter_next(&iter, &isnullvar, index, ...);" to fetch |
||||
* the next element into datumvar/isnullvar. |
||||
* "index" must be the zero-origin element number; we make caller provide |
||||
* this since caller is generally counting the elements anyway. Despite |
||||
* that, these functions can only fetch elements sequentially. |
||||
*/ |
||||
|
||||
typedef struct array_iter |
||||
{ |
||||
/* datumptr being NULL or not tells if we have flat or expanded array */ |
||||
|
||||
/* Fields used when we have an expanded array */ |
||||
Datum *datumptr; /* Pointer to Datum array */ |
||||
bool *isnullptr; /* Pointer to isnull array */ |
||||
|
||||
/* Fields used when we have a flat array */ |
||||
char *dataptr; /* Current spot in the data area */ |
||||
bits8 *bitmapptr; /* Current byte of the nulls bitmap, or NULL */ |
||||
int bitmask; /* mask for current bit in nulls bitmap */ |
||||
} array_iter; |
||||
|
||||
/*
|
||||
* We want the functions below to be inline; but if the compiler doesn't |
||||
* support that, fall back on providing them as regular functions. See |
||||
* STATIC_IF_INLINE in c.h. |
||||
*/ |
||||
#ifndef PG_USE_INLINE |
||||
extern void array_iter_setup(array_iter *it, AnyArrayType *a); |
||||
extern Datum array_iter_next(array_iter *it, bool *isnull, int i, |
||||
int elmlen, bool elmbyval, char elmalign); |
||||
#endif /* !PG_USE_INLINE */ |
||||
|
||||
#if defined(PG_USE_INLINE) || defined(ARRAYACCESS_INCLUDE_DEFINITIONS) |
||||
|
||||
STATIC_IF_INLINE void |
||||
array_iter_setup(array_iter *it, AnyArrayType *a) |
||||
{ |
||||
if (VARATT_IS_EXPANDED_HEADER(a)) |
||||
{ |
||||
if (a->xpn.dvalues) |
||||
{ |
||||
it->datumptr = a->xpn.dvalues; |
||||
it->isnullptr = a->xpn.dnulls; |
||||
/* we must fill all fields to prevent compiler warnings */ |
||||
it->dataptr = NULL; |
||||
it->bitmapptr = NULL; |
||||
} |
||||
else |
||||
{ |
||||
/* Work with flat array embedded in the expanded datum */ |
||||
it->datumptr = NULL; |
||||
it->isnullptr = NULL; |
||||
it->dataptr = ARR_DATA_PTR(a->xpn.fvalue); |
||||
it->bitmapptr = ARR_NULLBITMAP(a->xpn.fvalue); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
it->datumptr = NULL; |
||||
it->isnullptr = NULL; |
||||
it->dataptr = ARR_DATA_PTR(&a->flt); |
||||
it->bitmapptr = ARR_NULLBITMAP(&a->flt); |
||||
} |
||||
it->bitmask = 1; |
||||
} |
||||
|
||||
STATIC_IF_INLINE Datum |
||||
array_iter_next(array_iter *it, bool *isnull, int i, |
||||
int elmlen, bool elmbyval, char elmalign) |
||||
{ |
||||
Datum ret; |
||||
|
||||
if (it->datumptr) |
||||
{ |
||||
ret = it->datumptr[i]; |
||||
*isnull = it->isnullptr ? it->isnullptr[i] : false; |
||||
} |
||||
else |
||||
{ |
||||
if (it->bitmapptr && (*(it->bitmapptr) & it->bitmask) == 0) |
||||
{ |
||||
*isnull = true; |
||||
ret = (Datum) 0; |
||||
} |
||||
else |
||||
{ |
||||
*isnull = false; |
||||
ret = fetch_att(it->dataptr, elmbyval, elmlen); |
||||
it->dataptr = att_addlength_pointer(it->dataptr, elmlen, |
||||
it->dataptr); |
||||
it->dataptr = (char *) att_align_nominal(it->dataptr, elmalign); |
||||
} |
||||
it->bitmask <<= 1; |
||||
if (it->bitmask == 0x100) |
||||
{ |
||||
if (it->bitmapptr) |
||||
it->bitmapptr++; |
||||
it->bitmask = 1; |
||||
} |
||||
} |
||||
|
||||
return ret; |
||||
} |
||||
|
||||
#endif /* defined(PG_USE_INLINE) || |
||||
* defined(ARRAYACCESS_INCLUDE_DEFINITIONS) */ |
||||
|
||||
#endif /* ARRAYACCESS_H */ |
@ -0,0 +1,151 @@ |
||||
/*-------------------------------------------------------------------------
|
||||
* |
||||
* expandeddatum.h |
||||
* Declarations for access to "expanded" value representations. |
||||
* |
||||
* Complex data types, particularly container types such as arrays and |
||||
* records, usually have on-disk representations that are compact but not |
||||
* especially convenient to modify. What's more, when we do modify them, |
||||
* having to recopy all the rest of the value can be extremely inefficient. |
||||
* Therefore, we provide a notion of an "expanded" representation that is used |
||||
* only in memory and is optimized more for computation than storage. |
||||
* The format appearing on disk is called the data type's "flattened" |
||||
* representation, since it is required to be a contiguous blob of bytes -- |
||||
* but the type can have an expanded representation that is not. Data types |
||||
* must provide means to translate an expanded representation back to |
||||
* flattened form. |
||||
* |
||||
* An expanded object is meant to survive across multiple operations, but |
||||
* not to be enormously long-lived; for example it might be a local variable |
||||
* in a PL/pgSQL procedure. So its extra bulk compared to the on-disk format |
||||
* is a worthwhile trade-off. |
||||
* |
||||
* References to expanded objects are a type of TOAST pointer. |
||||
* Because of longstanding conventions in Postgres, this means that the |
||||
* flattened form of such an object must always be a varlena object. |
||||
* Fortunately that's no restriction in practice. |
||||
* |
||||
* There are actually two kinds of TOAST pointers for expanded objects: |
||||
* read-only and read-write pointers. Possession of one of the latter |
||||
* authorizes a function to modify the value in-place rather than copying it |
||||
* as would normally be required. Functions should always return a read-write |
||||
* pointer to any new expanded object they create. Functions that modify an |
||||
* argument value in-place must take care that they do not corrupt the old |
||||
* value if they fail partway through. |
||||
* |
||||
* |
||||
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group |
||||
* Portions Copyright (c) 1994, Regents of the University of California |
||||
* |
||||
* src/include/utils/expandeddatum.h |
||||
* |
||||
*------------------------------------------------------------------------- |
||||
*/ |
||||
#ifndef EXPANDEDDATUM_H |
||||
#define EXPANDEDDATUM_H |
||||
|
||||
/* Size of an EXTERNAL datum that contains a pointer to an expanded object */ |
||||
#define EXPANDED_POINTER_SIZE (VARHDRSZ_EXTERNAL + sizeof(varatt_expanded)) |
||||
|
||||
/*
|
||||
* "Methods" that must be provided for any expanded object. |
||||
* |
||||
* get_flat_size: compute space needed for flattened representation (total, |
||||
* including header). |
||||
* |
||||
* flatten_into: construct flattened representation in the caller-allocated |
||||
* space at *result, of size allocated_size (which will always be the result |
||||
* of a preceding get_flat_size call; it's passed for cross-checking). |
||||
* |
||||
* The flattened representation must be a valid in-line, non-compressed, |
||||
* 4-byte-header varlena object. |
||||
* |
||||
* Note: construction of a heap tuple from an expanded datum calls |
||||
* get_flat_size twice, so it's worthwhile to make sure that that doesn't |
||||
* incur too much overhead. |
||||
*/ |
||||
typedef Size (*EOM_get_flat_size_method) (ExpandedObjectHeader *eohptr); |
||||
typedef void (*EOM_flatten_into_method) (ExpandedObjectHeader *eohptr, |
||||
void *result, Size allocated_size); |
||||
|
||||
/* Struct of function pointers for an expanded object's methods */ |
||||
typedef struct ExpandedObjectMethods |
||||
{ |
||||
EOM_get_flat_size_method get_flat_size; |
||||
EOM_flatten_into_method flatten_into; |
||||
} ExpandedObjectMethods; |
||||
|
||||
/*
|
||||
* Every expanded object must contain this header; typically the header |
||||
* is embedded in some larger struct that adds type-specific fields. |
||||
* |
||||
* It is presumed that the header object and all subsidiary data are stored |
||||
* in eoh_context, so that the object can be freed by deleting that context, |
||||
* or its storage lifespan can be altered by reparenting the context. |
||||
* (In principle the object could own additional resources, such as malloc'd |
||||
* storage, and use a memory context reset callback to free them upon reset or |
||||
* deletion of eoh_context.) |
||||
* |
||||
* We set up two TOAST pointers within the standard header, one read-write |
||||
* and one read-only. This allows functions to return either kind of pointer |
||||
* without making an additional allocation, and in particular without worrying |
||||
* whether a separately palloc'd object would have sufficient lifespan. |
||||
* But note that these pointers are just a convenience; a pointer object |
||||
* appearing somewhere else would still be legal. |
||||
* |
||||
* The typedef declaration for this appears in postgres.h. |
||||
*/ |
||||
struct ExpandedObjectHeader |
||||
{ |
||||
/* Phony varlena header */ |
||||
int32 vl_len_; /* always EOH_HEADER_MAGIC, see below */ |
||||
|
||||
/* Pointer to methods required for object type */ |
||||
const ExpandedObjectMethods *eoh_methods; |
||||
|
||||
/* Memory context containing this header and subsidiary data */ |
||||
MemoryContext eoh_context; |
||||
|
||||
/* Standard R/W TOAST pointer for this object is kept here */ |
||||
char eoh_rw_ptr[EXPANDED_POINTER_SIZE]; |
||||
|
||||
/* Standard R/O TOAST pointer for this object is kept here */ |
||||
char eoh_ro_ptr[EXPANDED_POINTER_SIZE]; |
||||
}; |
||||
|
||||
/*
|
||||
* Particularly for read-only functions, it is handy to be able to work with |
||||
* either regular "flat" varlena inputs or expanded inputs of the same data |
||||
* type. To allow determining which case an argument-fetching function has |
||||
* returned, the first int32 of an ExpandedObjectHeader always contains -1 |
||||
* (EOH_HEADER_MAGIC to the code). This works since no 4-byte-header varlena |
||||
* could have that as its first 4 bytes. Caution: we could not reliably tell |
||||
* the difference between an ExpandedObjectHeader and a short-header object |
||||
* with this trick. However, it works fine if the argument fetching code |
||||
* always returns either a 4-byte-header flat object or an expanded object. |
||||
*/ |
||||
#define EOH_HEADER_MAGIC (-1) |
||||
#define VARATT_IS_EXPANDED_HEADER(PTR) \ |
||||
(((ExpandedObjectHeader *) (PTR))->vl_len_ == EOH_HEADER_MAGIC) |
||||
|
||||
/*
|
||||
* Generic support functions for expanded objects. |
||||
* (More of these might be worth inlining later.) |
||||
*/ |
||||
|
||||
#define EOHPGetRWDatum(eohptr) PointerGetDatum((eohptr)->eoh_rw_ptr) |
||||
#define EOHPGetRODatum(eohptr) PointerGetDatum((eohptr)->eoh_ro_ptr) |
||||
|
||||
extern ExpandedObjectHeader *DatumGetEOHP(Datum d); |
||||
extern void EOH_init_header(ExpandedObjectHeader *eohptr, |
||||
const ExpandedObjectMethods *methods, |
||||
MemoryContext obj_context); |
||||
extern Size EOH_get_flat_size(ExpandedObjectHeader *eohptr); |
||||
extern void EOH_flatten_into(ExpandedObjectHeader *eohptr, |
||||
void *result, Size allocated_size); |
||||
extern bool DatumIsReadWriteExpandedObject(Datum d, bool isnull, int16 typlen); |
||||
extern Datum MakeExpandedObjectReadOnly(Datum d, bool isnull, int16 typlen); |
||||
extern Datum TransferExpandedObject(Datum d, MemoryContext new_parent); |
||||
extern void DeleteExpandedObject(Datum d); |
||||
|
||||
#endif /* EXPANDEDDATUM_H */ |
Loading…
Reference in new issue