1999-12-21 01:06:44 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* tuptoaster.c
|
|
|
|
* Support routines for external and compressed storage of
|
2000-04-12 19:17:23 +02:00
|
|
|
* variable size attributes.
|
1999-12-21 01:06:44 +01:00
|
|
|
*
|
2003-08-05 01:59:41 +02:00
|
|
|
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
|
1999-12-21 01:06:44 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2004-06-05 03:55:05 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/access/heap/tuptoaster.c,v 1.43 2004/06/05 01:55:04 tgl Exp $
|
1999-12-21 01:06:44 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* INTERFACE ROUTINES
|
|
|
|
* heap_tuple_toast_attrs -
|
|
|
|
* Try to make a given tuple fit into one page by compressing
|
|
|
|
* or moving off attributes
|
|
|
|
*
|
2000-07-04 01:10:14 +02:00
|
|
|
* heap_tuple_untoast_attr -
|
|
|
|
* Fetch back a given value from the "secondary" relation
|
|
|
|
*
|
1999-12-21 01:06:44 +01:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2000-08-04 06:16:17 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
1999-12-21 01:06:44 +01:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/genam.h"
|
|
|
|
#include "access/tuptoaster.h"
|
|
|
|
#include "catalog/catalog.h"
|
|
|
|
#include "utils/rel.h"
|
|
|
|
#include "utils/builtins.h"
|
|
|
|
#include "utils/fmgroids.h"
|
|
|
|
#include "utils/pg_lzcompress.h"
|
2004-06-05 03:55:05 +02:00
|
|
|
#include "utils/typcache.h"
|
1999-12-21 01:06:44 +01:00
|
|
|
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
#undef TOAST_DEBUG
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
static void toast_delete(Relation rel, HeapTuple oldtup);
|
|
|
|
static void toast_delete_datum(Relation rel, Datum value);
|
|
|
|
static void toast_insert_or_update(Relation rel, HeapTuple newtup,
|
|
|
|
HeapTuple oldtup);
|
2001-08-10 20:57:42 +02:00
|
|
|
static Datum toast_save_datum(Relation rel, Datum value);
|
2001-03-22 05:01:46 +01:00
|
|
|
static varattrib *toast_fetch_datum(varattrib *attr);
|
2002-03-05 06:33:31 +01:00
|
|
|
static varattrib *toast_fetch_datum_slice(varattrib *attr,
|
2002-09-04 22:31:48 +02:00
|
|
|
int32 sliceoffset, int32 length);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
1999-12-21 01:06:44 +01:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
/* ----------
|
|
|
|
* heap_tuple_toast_attrs -
|
|
|
|
*
|
|
|
|
* This is the central public entry point for toasting from heapam.
|
|
|
|
*
|
|
|
|
* Calls the appropriate event specific action.
|
|
|
|
* ----------
|
|
|
|
*/
|
1999-12-21 01:06:44 +01:00
|
|
|
void
|
2000-04-12 19:17:23 +02:00
|
|
|
heap_tuple_toast_attrs(Relation rel, HeapTuple newtup, HeapTuple oldtup)
|
1999-12-21 01:06:44 +01:00
|
|
|
{
|
2000-07-04 01:10:14 +02:00
|
|
|
if (newtup == NULL)
|
|
|
|
toast_delete(rel, oldtup);
|
|
|
|
else
|
|
|
|
toast_insert_or_update(rel, newtup, oldtup);
|
|
|
|
}
|
2000-07-22 13:18:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* heap_tuple_fetch_attr -
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Public entry point to get back a toasted value
|
2000-07-22 13:18:47 +02:00
|
|
|
* external storage (possibly still in compressed format).
|
|
|
|
* ----------
|
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
varattrib *
|
2000-07-22 13:18:47 +02:00
|
|
|
heap_tuple_fetch_attr(varattrib *attr)
|
2000-07-21 12:31:31 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
varattrib *result;
|
2000-07-22 13:18:47 +02:00
|
|
|
|
|
|
|
if (VARATT_IS_EXTERNAL(attr))
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-22 13:18:47 +02:00
|
|
|
* This is an external stored plain value
|
|
|
|
*/
|
|
|
|
result = toast_fetch_datum(attr);
|
|
|
|
}
|
2000-07-21 12:31:31 +02:00
|
|
|
else
|
2000-07-22 13:18:47 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* This is a plain value inside of the main tuple - why am I
|
|
|
|
* called?
|
2000-07-22 13:18:47 +02:00
|
|
|
*/
|
|
|
|
result = attr;
|
2001-03-22 05:01:46 +01:00
|
|
|
}
|
2000-07-22 13:18:47 +02:00
|
|
|
|
|
|
|
return result;
|
2000-07-21 12:31:31 +02:00
|
|
|
}
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* heap_tuple_untoast_attr -
|
|
|
|
*
|
|
|
|
* Public entry point to get back a toasted value from compression
|
|
|
|
* or external storage.
|
|
|
|
* ----------
|
|
|
|
*/
|
2001-10-25 07:50:21 +02:00
|
|
|
varattrib *
|
2000-07-04 01:10:14 +02:00
|
|
|
heap_tuple_untoast_attr(varattrib *attr)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
varattrib *result;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
if (VARATT_IS_EXTERNAL(attr))
|
|
|
|
{
|
|
|
|
if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
{
|
|
|
|
/* ----------
|
|
|
|
* This is an external stored compressed value
|
|
|
|
* Fetch it from the toast heap and decompress.
|
|
|
|
* ----------
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
varattrib *tmp;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
tmp = toast_fetch_datum(attr);
|
2001-03-22 05:01:46 +01:00
|
|
|
result = (varattrib *) palloc(attr->va_content.va_external.va_rawsize
|
|
|
|
+ VARHDRSZ);
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = attr->va_content.va_external.va_rawsize
|
2001-03-22 05:01:46 +01:00
|
|
|
+ VARHDRSZ;
|
|
|
|
pglz_decompress((PGLZ_Header *) tmp, VARATT_DATA(result));
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
pfree(tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* This is an external stored plain value
|
|
|
|
*/
|
|
|
|
result = toast_fetch_datum(attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* This is a compressed value inside of the main tuple
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
result = (varattrib *) palloc(attr->va_content.va_compressed.va_rawsize
|
|
|
|
+ VARHDRSZ);
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = attr->va_content.va_compressed.va_rawsize
|
2001-03-22 05:01:46 +01:00
|
|
|
+ VARHDRSZ;
|
|
|
|
pglz_decompress((PGLZ_Header *) attr, VARATT_DATA(result));
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
else
|
2001-03-22 07:16:21 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a plain value inside of the main tuple - why am I
|
|
|
|
* called?
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
return attr;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
/* ----------
|
|
|
|
* heap_tuple_untoast_attr_slice -
|
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Public entry point to get back part of a toasted value
|
|
|
|
* from compression or external storage.
|
2002-03-05 06:33:31 +01:00
|
|
|
* ----------
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
varattrib *
|
2002-03-05 06:33:31 +01:00
|
|
|
heap_tuple_untoast_attr_slice(varattrib *attr, int32 sliceoffset, int32 slicelength)
|
|
|
|
{
|
|
|
|
varattrib *preslice;
|
|
|
|
varattrib *result;
|
2002-09-04 22:31:48 +02:00
|
|
|
int32 attrsize;
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
varattrib *tmp;
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
if (VARATT_IS_EXTERNAL(attr))
|
|
|
|
tmp = toast_fetch_datum(attr);
|
|
|
|
else
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
tmp = attr; /* compressed in main tuple */
|
2002-03-05 06:33:31 +01:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
preslice = (varattrib *) palloc(attr->va_content.va_external.va_rawsize
|
|
|
|
+ VARHDRSZ);
|
|
|
|
VARATT_SIZEP(preslice) = attr->va_content.va_external.va_rawsize + VARHDRSZ;
|
|
|
|
pglz_decompress((PGLZ_Header *) tmp, VARATT_DATA(preslice));
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (tmp != attr)
|
2002-03-05 06:33:31 +01:00
|
|
|
pfree(tmp);
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
else
|
2002-03-05 06:33:31 +01:00
|
|
|
{
|
|
|
|
/* Plain value */
|
|
|
|
if (VARATT_IS_EXTERNAL(attr))
|
2002-09-04 22:31:48 +02:00
|
|
|
{
|
2002-03-05 06:33:31 +01:00
|
|
|
/* fast path */
|
|
|
|
return (toast_fetch_datum_slice(attr, sliceoffset, slicelength));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
preslice = attr;
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
/* slicing of datum for compressed cases and plain value */
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
attrsize = VARSIZE(preslice) - VARHDRSZ;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (sliceoffset >= attrsize)
|
2002-03-05 06:33:31 +01:00
|
|
|
{
|
|
|
|
sliceoffset = 0;
|
|
|
|
slicelength = 0;
|
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
if (((sliceoffset + slicelength) > attrsize) || slicelength < 0)
|
|
|
|
slicelength = attrsize - sliceoffset;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
result = (varattrib *) palloc(slicelength + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(result) = slicelength + VARHDRSZ;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
memcpy(VARDATA(result), VARDATA(preslice) + sliceoffset, slicelength);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (preslice != attr)
|
|
|
|
pfree(preslice);
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-07 02:43:27 +02:00
|
|
|
/* ----------
|
|
|
|
* toast_raw_datum_size -
|
|
|
|
*
|
|
|
|
* Return the raw (detoasted) size of a varlena datum
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
Size
|
|
|
|
toast_raw_datum_size(Datum value)
|
|
|
|
{
|
|
|
|
varattrib *attr = (varattrib *) DatumGetPointer(value);
|
|
|
|
Size result;
|
|
|
|
|
|
|
|
if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
{
|
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* va_rawsize shows the original data size, whether the datum is
|
|
|
|
* external or not.
|
2001-05-07 02:43:27 +02:00
|
|
|
*/
|
|
|
|
result = attr->va_content.va_compressed.va_rawsize + VARHDRSZ;
|
|
|
|
}
|
|
|
|
else if (VARATT_IS_EXTERNAL(attr))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* an uncompressed external attribute has rawsize including the
|
|
|
|
* header (not too consistent!)
|
|
|
|
*/
|
|
|
|
result = attr->va_content.va_external.va_rawsize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* plain untoasted datum */
|
|
|
|
result = VARSIZE(attr);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
/* ----------
|
|
|
|
* toast_delete -
|
|
|
|
*
|
|
|
|
* Cascaded delete toast-entries on DELETE
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
toast_delete(Relation rel, HeapTuple oldtup)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TupleDesc tupleDesc;
|
|
|
|
Form_pg_attribute *att;
|
|
|
|
int numAttrs;
|
|
|
|
int i;
|
2004-06-04 22:35:21 +02:00
|
|
|
Datum toast_values[MaxHeapAttributeNumber];
|
|
|
|
char toast_nulls[MaxHeapAttributeNumber];
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-06-04 22:35:21 +02:00
|
|
|
* Get the tuple descriptor and break down the tuple into fields.
|
|
|
|
*
|
|
|
|
* NOTE: it's debatable whether to use heap_deformtuple() here or
|
|
|
|
* just heap_getattr() only the varlena columns. The latter could
|
|
|
|
* win if there are few varlena columns and many non-varlena ones.
|
|
|
|
* However, heap_deformtuple costs only O(N) while the heap_getattr
|
|
|
|
* way would cost O(N^2) if there are many varlena columns, so it
|
|
|
|
* seems better to err on the side of linear cost. (We won't even
|
|
|
|
* be here unless there's at least one varlena column, by the way.)
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
tupleDesc = rel->rd_att;
|
|
|
|
att = tupleDesc->attrs;
|
2004-06-04 22:35:21 +02:00
|
|
|
numAttrs = tupleDesc->natts;
|
|
|
|
|
|
|
|
Assert(numAttrs <= MaxHeapAttributeNumber);
|
|
|
|
heap_deformtuple(oldtup, tupleDesc, toast_values, toast_nulls);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Check for external stored attributes and delete them from the
|
|
|
|
* secondary relation.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
2001-01-15 06:29:19 +01:00
|
|
|
if (att[i]->attlen == -1)
|
|
|
|
{
|
2004-06-04 22:35:21 +02:00
|
|
|
Datum value = toast_values[i];
|
|
|
|
|
|
|
|
if (toast_nulls[i] != 'n' && VARATT_IS_EXTERNAL(value))
|
2000-07-04 01:10:14 +02:00
|
|
|
toast_delete_datum(rel, value);
|
2001-01-15 06:29:19 +01:00
|
|
|
}
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* toast_insert_or_update -
|
|
|
|
*
|
2000-08-04 06:16:17 +02:00
|
|
|
* Delete no-longer-used toast-entries and create new ones to
|
2000-07-04 01:10:14 +02:00
|
|
|
* make the new tuple fit on INSERT or UPDATE
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
TupleDesc tupleDesc;
|
|
|
|
Form_pg_attribute *att;
|
|
|
|
int numAttrs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
bool need_change = false;
|
|
|
|
bool need_free = false;
|
|
|
|
bool need_delold = false;
|
|
|
|
bool has_nulls = false;
|
|
|
|
|
|
|
|
Size maxDataLen;
|
|
|
|
|
|
|
|
char toast_action[MaxHeapAttributeNumber];
|
|
|
|
char toast_nulls[MaxHeapAttributeNumber];
|
2004-06-04 22:35:21 +02:00
|
|
|
char toast_oldnulls[MaxHeapAttributeNumber];
|
2001-03-22 05:01:46 +01:00
|
|
|
Datum toast_values[MaxHeapAttributeNumber];
|
2004-06-04 22:35:21 +02:00
|
|
|
Datum toast_oldvalues[MaxHeapAttributeNumber];
|
2001-03-22 05:01:46 +01:00
|
|
|
int32 toast_sizes[MaxHeapAttributeNumber];
|
|
|
|
bool toast_free[MaxHeapAttributeNumber];
|
|
|
|
bool toast_delold[MaxHeapAttributeNumber];
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-06-04 22:35:21 +02:00
|
|
|
* Get the tuple descriptor and break down the tuple(s) into fields.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
tupleDesc = rel->rd_att;
|
|
|
|
att = tupleDesc->attrs;
|
2004-06-04 22:35:21 +02:00
|
|
|
numAttrs = tupleDesc->natts;
|
|
|
|
|
|
|
|
Assert(numAttrs <= MaxHeapAttributeNumber);
|
|
|
|
heap_deformtuple(newtup, tupleDesc, toast_values, toast_nulls);
|
|
|
|
if (oldtup != NULL)
|
|
|
|
heap_deformtuple(oldtup, tupleDesc, toast_oldvalues, toast_oldnulls);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* Then collect information about the values given
|
2000-10-24 01:42:04 +02:00
|
|
|
*
|
|
|
|
* NOTE: toast_action[i] can have these values:
|
|
|
|
* ' ' default handling
|
|
|
|
* 'p' already processed --- don't touch it
|
|
|
|
* 'x' incompressible, but OK to move off
|
2004-06-04 22:35:21 +02:00
|
|
|
*
|
|
|
|
* NOTE: toast_sizes[i] is only made valid for varlena attributes with
|
|
|
|
* toast_action[i] different from 'p'.
|
2000-07-04 01:10:14 +02:00
|
|
|
* ----------
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
memset(toast_action, ' ', numAttrs * sizeof(char));
|
|
|
|
memset(toast_free, 0, numAttrs * sizeof(bool));
|
|
|
|
memset(toast_delold, 0, numAttrs * sizeof(bool));
|
2004-06-04 22:35:21 +02:00
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
varattrib *old_value;
|
|
|
|
varattrib *new_value;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
if (oldtup != NULL)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* For UPDATE get the old and new values of this attribute
|
|
|
|
*/
|
2004-06-04 22:35:21 +02:00
|
|
|
old_value = (varattrib *) DatumGetPointer(toast_oldvalues[i]);
|
2001-03-22 05:01:46 +01:00
|
|
|
new_value = (varattrib *) DatumGetPointer(toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* If the old value is an external stored one, check if it has
|
|
|
|
* changed so we have to delete it later.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2004-06-04 22:35:21 +02:00
|
|
|
if (att[i]->attlen == -1 && toast_oldnulls[i] != 'n' &&
|
2001-03-22 05:01:46 +01:00
|
|
|
VARATT_IS_EXTERNAL(old_value))
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2004-06-04 22:35:21 +02:00
|
|
|
if (toast_nulls[i] == 'n' || !VARATT_IS_EXTERNAL(new_value) ||
|
2001-08-10 20:57:42 +02:00
|
|
|
old_value->va_content.va_external.va_valueid !=
|
|
|
|
new_value->va_content.va_external.va_valueid ||
|
|
|
|
old_value->va_content.va_external.va_toastrelid !=
|
|
|
|
new_value->va_content.va_external.va_toastrelid)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-06-04 22:35:21 +02:00
|
|
|
* The old external stored value isn't needed any more
|
2001-03-22 07:16:21 +01:00
|
|
|
* after the update
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
toast_delold[i] = true;
|
|
|
|
need_delold = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* This attribute isn't changed by this update so we
|
|
|
|
* reuse the original reference to the old value in
|
|
|
|
* the new tuple.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
toast_action[i] = 'p';
|
|
|
|
toast_sizes[i] = VARATT_SIZE(toast_values[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* For INSERT simply get the new value
|
|
|
|
*/
|
2004-06-04 22:35:21 +02:00
|
|
|
new_value = (varattrib *) DatumGetPointer(toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Handle NULL attributes
|
|
|
|
*/
|
2004-06-04 22:35:21 +02:00
|
|
|
if (toast_nulls[i] == 'n')
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
|
|
|
toast_action[i] = 'p';
|
|
|
|
has_nulls = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-06-04 22:35:21 +02:00
|
|
|
* Now look at varlena attributes
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
if (att[i]->attlen == -1)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-08-01 00:39:17 +02:00
|
|
|
* If the table's attribute says PLAIN always, force it so.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
if (att[i]->attstorage == 'p')
|
|
|
|
toast_action[i] = 'p';
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-02-09 18:30:03 +01:00
|
|
|
* We took care of UPDATE above, so any external value we find
|
2000-08-01 00:39:17 +02:00
|
|
|
* still in the tuple must be someone else's we cannot reuse.
|
|
|
|
* Expand it to plain (and, probably, toast it again below).
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2004-06-05 03:55:05 +02:00
|
|
|
if (VARATT_IS_EXTERNAL(new_value))
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2004-06-05 03:55:05 +02:00
|
|
|
new_value = heap_tuple_untoast_attr(new_value);
|
|
|
|
toast_values[i] = PointerGetDatum(new_value);
|
2000-07-04 01:10:14 +02:00
|
|
|
toast_free[i] = true;
|
|
|
|
need_change = true;
|
|
|
|
need_free = true;
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Remember the size of this attribute
|
|
|
|
*/
|
2004-06-05 03:55:05 +02:00
|
|
|
toast_sizes[i] = VARATT_SIZE(new_value);
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2004-06-04 22:35:21 +02:00
|
|
|
* Not a varlena attribute, plain storage always
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
toast_action[i] = 'p';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------
|
2000-08-04 06:16:17 +02:00
|
|
|
* Compress and/or save external until data fits into target length
|
2000-07-04 01:10:14 +02:00
|
|
|
*
|
|
|
|
* 1: Inline compress attributes with attstorage 'x'
|
|
|
|
* 2: Store attributes with attstorage 'x' or 'e' external
|
2001-03-22 05:01:46 +01:00
|
|
|
* 3: Inline compress attributes with attstorage 'm'
|
2000-07-04 01:10:14 +02:00
|
|
|
* 4: Store attributes with attstorage 'm' external
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
maxDataLen = offsetof(HeapTupleHeaderData, t_bits);
|
|
|
|
if (has_nulls)
|
|
|
|
maxDataLen += BITMAPLEN(numAttrs);
|
2000-08-04 06:16:17 +02:00
|
|
|
maxDataLen = TOAST_TUPLE_TARGET - MAXALIGN(maxDataLen);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Look for attributes with attstorage 'x' to compress
|
|
|
|
*/
|
|
|
|
while (MAXALIGN(ComputeDataSize(tupleDesc, toast_values, toast_nulls)) >
|
2001-03-22 05:01:46 +01:00
|
|
|
maxDataLen)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int biggest_attno = -1;
|
|
|
|
int32 biggest_size = MAXALIGN(sizeof(varattrib));
|
|
|
|
Datum old_value;
|
|
|
|
Datum new_value;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Search for the biggest yet uncompressed internal attribute
|
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
2000-10-24 01:42:04 +02:00
|
|
|
if (toast_action[i] != ' ')
|
2000-07-04 01:10:14 +02:00
|
|
|
continue;
|
|
|
|
if (VARATT_IS_EXTENDED(toast_values[i]))
|
|
|
|
continue;
|
|
|
|
if (att[i]->attstorage != 'x')
|
|
|
|
continue;
|
|
|
|
if (toast_sizes[i] > biggest_size)
|
|
|
|
{
|
|
|
|
biggest_attno = i;
|
2001-03-22 05:01:46 +01:00
|
|
|
biggest_size = toast_sizes[i];
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (biggest_attno < 0)
|
|
|
|
break;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-10-24 01:42:04 +02:00
|
|
|
* Attempt to compress it inline
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
i = biggest_attno;
|
|
|
|
old_value = toast_values[i];
|
|
|
|
new_value = toast_compress_datum(old_value);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2000-10-24 01:42:04 +02:00
|
|
|
if (DatumGetPointer(new_value) != NULL)
|
|
|
|
{
|
|
|
|
/* successful compression */
|
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(old_value));
|
2001-03-22 05:01:46 +01:00
|
|
|
toast_values[i] = new_value;
|
|
|
|
toast_free[i] = true;
|
|
|
|
toast_sizes[i] = VARATT_SIZE(toast_values[i]);
|
|
|
|
need_change = true;
|
|
|
|
need_free = true;
|
2000-10-24 01:42:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* incompressible data, ignore on subsequent compression
|
|
|
|
* passes
|
|
|
|
*/
|
2000-10-24 01:42:04 +02:00
|
|
|
toast_action[i] = 'x';
|
|
|
|
}
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Second we look for attributes of attstorage 'x' or 'e' that are
|
|
|
|
* still inline.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
while (MAXALIGN(ComputeDataSize(tupleDesc, toast_values, toast_nulls)) >
|
2001-03-22 05:01:46 +01:00
|
|
|
maxDataLen && rel->rd_rel->reltoastrelid != InvalidOid)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int biggest_attno = -1;
|
|
|
|
int32 biggest_size = MAXALIGN(sizeof(varattrib));
|
|
|
|
Datum old_value;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-23 05:49:58 +01:00
|
|
|
/*------
|
|
|
|
* Search for the biggest yet inlined attribute with
|
|
|
|
* attstorage equals 'x' or 'e'
|
|
|
|
*------
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
|
|
|
if (toast_action[i] == 'p')
|
|
|
|
continue;
|
|
|
|
if (VARATT_IS_EXTERNAL(toast_values[i]))
|
|
|
|
continue;
|
|
|
|
if (att[i]->attstorage != 'x' && att[i]->attstorage != 'e')
|
|
|
|
continue;
|
|
|
|
if (toast_sizes[i] > biggest_size)
|
|
|
|
{
|
|
|
|
biggest_attno = i;
|
2001-03-22 05:01:46 +01:00
|
|
|
biggest_size = toast_sizes[i];
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (biggest_attno < 0)
|
|
|
|
break;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Store this external
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
i = biggest_attno;
|
|
|
|
old_value = toast_values[i];
|
|
|
|
toast_action[i] = 'p';
|
2001-08-10 20:57:42 +02:00
|
|
|
toast_values[i] = toast_save_datum(rel, toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(old_value));
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
toast_free[i] = true;
|
|
|
|
toast_sizes[i] = VARATT_SIZE(toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
need_change = true;
|
2001-03-22 05:01:46 +01:00
|
|
|
need_free = true;
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* Round 3 - this time we take attributes with storage 'm' into
|
|
|
|
* compression
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
while (MAXALIGN(ComputeDataSize(tupleDesc, toast_values, toast_nulls)) >
|
2001-03-22 05:01:46 +01:00
|
|
|
maxDataLen)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int biggest_attno = -1;
|
|
|
|
int32 biggest_size = MAXALIGN(sizeof(varattrib));
|
|
|
|
Datum old_value;
|
|
|
|
Datum new_value;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Search for the biggest yet uncompressed internal attribute
|
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
2000-10-24 01:42:04 +02:00
|
|
|
if (toast_action[i] != ' ')
|
2000-07-04 01:10:14 +02:00
|
|
|
continue;
|
|
|
|
if (VARATT_IS_EXTENDED(toast_values[i]))
|
|
|
|
continue;
|
|
|
|
if (att[i]->attstorage != 'm')
|
|
|
|
continue;
|
|
|
|
if (toast_sizes[i] > biggest_size)
|
|
|
|
{
|
|
|
|
biggest_attno = i;
|
2001-03-22 05:01:46 +01:00
|
|
|
biggest_size = toast_sizes[i];
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (biggest_attno < 0)
|
|
|
|
break;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-10-24 01:42:04 +02:00
|
|
|
* Attempt to compress it inline
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
i = biggest_attno;
|
|
|
|
old_value = toast_values[i];
|
|
|
|
new_value = toast_compress_datum(old_value);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2000-10-24 01:42:04 +02:00
|
|
|
if (DatumGetPointer(new_value) != NULL)
|
|
|
|
{
|
|
|
|
/* successful compression */
|
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(old_value));
|
2001-03-22 05:01:46 +01:00
|
|
|
toast_values[i] = new_value;
|
|
|
|
toast_free[i] = true;
|
|
|
|
toast_sizes[i] = VARATT_SIZE(toast_values[i]);
|
|
|
|
need_change = true;
|
|
|
|
need_free = true;
|
2000-10-24 01:42:04 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
|
|
|
* incompressible data, ignore on subsequent compression
|
|
|
|
* passes
|
|
|
|
*/
|
2000-10-24 01:42:04 +02:00
|
|
|
toast_action[i] = 'x';
|
|
|
|
}
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Finally we store attributes of type 'm' external
|
|
|
|
*/
|
|
|
|
while (MAXALIGN(ComputeDataSize(tupleDesc, toast_values, toast_nulls)) >
|
2001-03-22 05:01:46 +01:00
|
|
|
maxDataLen && rel->rd_rel->reltoastrelid != InvalidOid)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int biggest_attno = -1;
|
|
|
|
int32 biggest_size = MAXALIGN(sizeof(varattrib));
|
|
|
|
Datum old_value;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-23 05:49:58 +01:00
|
|
|
/*--------
|
|
|
|
* Search for the biggest yet inlined attribute with
|
|
|
|
* attstorage = 'm'
|
|
|
|
*--------
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
|
|
|
if (toast_action[i] == 'p')
|
|
|
|
continue;
|
|
|
|
if (VARATT_IS_EXTERNAL(toast_values[i]))
|
|
|
|
continue;
|
|
|
|
if (att[i]->attstorage != 'm')
|
|
|
|
continue;
|
|
|
|
if (toast_sizes[i] > biggest_size)
|
|
|
|
{
|
|
|
|
biggest_attno = i;
|
2001-03-22 05:01:46 +01:00
|
|
|
biggest_size = toast_sizes[i];
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (biggest_attno < 0)
|
|
|
|
break;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Store this external
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
i = biggest_attno;
|
|
|
|
old_value = toast_values[i];
|
|
|
|
toast_action[i] = 'p';
|
2001-08-10 20:57:42 +02:00
|
|
|
toast_values[i] = toast_save_datum(rel, toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(old_value));
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
toast_free[i] = true;
|
|
|
|
toast_sizes[i] = VARATT_SIZE(toast_values[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
need_change = true;
|
2001-03-22 05:01:46 +01:00
|
|
|
need_free = true;
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* In the case we toasted any values, we need to build a new heap
|
|
|
|
* tuple with the changed values.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
if (need_change)
|
|
|
|
{
|
2002-05-27 21:53:33 +02:00
|
|
|
HeapTupleHeader olddata = newtup->t_data;
|
2001-03-22 05:01:46 +01:00
|
|
|
char *new_data;
|
|
|
|
int32 new_len;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-05-27 21:53:33 +02:00
|
|
|
* Calculate the new size of the tuple. Header size should not
|
|
|
|
* change, but data size might.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
new_len = offsetof(HeapTupleHeaderData, t_bits);
|
|
|
|
if (has_nulls)
|
|
|
|
new_len += BITMAPLEN(numAttrs);
|
2002-09-02 03:05:06 +02:00
|
|
|
if (olddata->t_infomask & HEAP_HASOID)
|
2002-07-20 07:16:59 +02:00
|
|
|
new_len += sizeof(Oid);
|
2000-07-04 01:10:14 +02:00
|
|
|
new_len = MAXALIGN(new_len);
|
2002-05-27 21:53:33 +02:00
|
|
|
Assert(new_len == olddata->t_hoff);
|
2000-07-04 01:10:14 +02:00
|
|
|
new_len += ComputeDataSize(tupleDesc, toast_values, toast_nulls);
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-05-27 21:53:33 +02:00
|
|
|
* Allocate new tuple in same context as old one.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2002-05-27 21:53:33 +02:00
|
|
|
new_data = (char *) MemoryContextAlloc(newtup->t_datamcxt, new_len);
|
|
|
|
newtup->t_data = (HeapTupleHeader) new_data;
|
|
|
|
newtup->t_len = new_len;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Put the tuple header and the changed values into place
|
|
|
|
*/
|
2002-05-27 21:53:33 +02:00
|
|
|
memcpy(new_data, olddata, olddata->t_hoff);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2002-05-27 21:53:33 +02:00
|
|
|
DataFill((char *) new_data + olddata->t_hoff,
|
2001-03-22 05:01:46 +01:00
|
|
|
tupleDesc,
|
|
|
|
toast_values,
|
|
|
|
toast_nulls,
|
|
|
|
&(newtup->t_data->t_infomask),
|
|
|
|
has_nulls ? newtup->t_data->t_bits : NULL);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* In the case we modified a previously modified tuple again, free
|
|
|
|
* the memory from the previous run
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
if ((char *) olddata != ((char *) newtup + HEAPTUPLESIZE))
|
2000-07-04 01:10:14 +02:00
|
|
|
pfree(olddata);
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Free allocated temp values
|
|
|
|
*/
|
|
|
|
if (need_free)
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(toast_values[i]));
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Delete external values from the old tuple
|
|
|
|
*/
|
|
|
|
if (need_delold)
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
if (toast_delold[i])
|
2004-06-04 22:35:21 +02:00
|
|
|
toast_delete_datum(rel, toast_oldvalues[i]);
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-05 03:55:05 +02:00
|
|
|
/* ----------
|
|
|
|
* toast_flatten_tuple_attribute -
|
|
|
|
*
|
|
|
|
* If a Datum is of composite type, "flatten" it to contain no toasted fields.
|
|
|
|
* This must be invoked on any potentially-composite field that is to be
|
|
|
|
* inserted into a tuple. Doing this preserves the invariant that toasting
|
|
|
|
* goes only one level deep in a tuple.
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
toast_flatten_tuple_attribute(Datum value,
|
|
|
|
Oid typeId, int32 typeMod)
|
|
|
|
{
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
HeapTupleHeader olddata;
|
|
|
|
HeapTupleHeader new_data;
|
|
|
|
int32 new_len;
|
|
|
|
HeapTupleData tmptup;
|
|
|
|
Form_pg_attribute *att;
|
|
|
|
int numAttrs;
|
|
|
|
int i;
|
|
|
|
bool need_change = false;
|
|
|
|
bool has_nulls = false;
|
|
|
|
Datum toast_values[MaxTupleAttributeNumber];
|
|
|
|
char toast_nulls[MaxTupleAttributeNumber];
|
|
|
|
bool toast_free[MaxTupleAttributeNumber];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if it's a composite type, and get the tupdesc if so.
|
|
|
|
*/
|
|
|
|
tupleDesc = lookup_rowtype_tupdesc_noerror(typeId, typeMod, true);
|
|
|
|
if (tupleDesc == NULL)
|
|
|
|
return value; /* not a composite type */
|
|
|
|
|
|
|
|
att = tupleDesc->attrs;
|
|
|
|
numAttrs = tupleDesc->natts;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Break down the tuple into fields.
|
|
|
|
*/
|
|
|
|
olddata = DatumGetHeapTupleHeader(value);
|
|
|
|
Assert(typeId == HeapTupleHeaderGetTypeId(olddata));
|
|
|
|
Assert(typeMod == HeapTupleHeaderGetTypMod(olddata));
|
|
|
|
/* Build a temporary HeapTuple control structure */
|
|
|
|
tmptup.t_len = HeapTupleHeaderGetDatumLength(olddata);
|
|
|
|
ItemPointerSetInvalid(&(tmptup.t_self));
|
|
|
|
tmptup.t_tableOid = InvalidOid;
|
|
|
|
tmptup.t_data = olddata;
|
|
|
|
|
|
|
|
Assert(numAttrs <= MaxTupleAttributeNumber);
|
|
|
|
heap_deformtuple(&tmptup, tupleDesc, toast_values, toast_nulls);
|
|
|
|
|
|
|
|
memset(toast_free, 0, numAttrs * sizeof(bool));
|
|
|
|
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Look at non-null varlena attributes
|
|
|
|
*/
|
|
|
|
if (toast_nulls[i] == 'n')
|
|
|
|
has_nulls = true;
|
|
|
|
else if (att[i]->attlen == -1)
|
|
|
|
{
|
|
|
|
varattrib *new_value;
|
|
|
|
|
|
|
|
new_value = (varattrib *) DatumGetPointer(toast_values[i]);
|
|
|
|
if (VARATT_IS_EXTENDED(new_value))
|
|
|
|
{
|
|
|
|
new_value = heap_tuple_untoast_attr(new_value);
|
|
|
|
toast_values[i] = PointerGetDatum(new_value);
|
|
|
|
toast_free[i] = true;
|
|
|
|
need_change = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If nothing to untoast, just return the original tuple.
|
|
|
|
*/
|
|
|
|
if (!need_change)
|
|
|
|
return value;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the new size of the tuple. Header size should not
|
|
|
|
* change, but data size might.
|
|
|
|
*/
|
|
|
|
new_len = offsetof(HeapTupleHeaderData, t_bits);
|
|
|
|
if (has_nulls)
|
|
|
|
new_len += BITMAPLEN(numAttrs);
|
|
|
|
if (olddata->t_infomask & HEAP_HASOID)
|
|
|
|
new_len += sizeof(Oid);
|
|
|
|
new_len = MAXALIGN(new_len);
|
|
|
|
Assert(new_len == olddata->t_hoff);
|
|
|
|
new_len += ComputeDataSize(tupleDesc, toast_values, toast_nulls);
|
|
|
|
|
|
|
|
new_data = (HeapTupleHeader) palloc0(new_len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put the tuple header and the changed values into place
|
|
|
|
*/
|
|
|
|
memcpy(new_data, olddata, olddata->t_hoff);
|
|
|
|
|
|
|
|
HeapTupleHeaderSetDatumLength(new_data, new_len);
|
|
|
|
|
|
|
|
DataFill((char *) new_data + olddata->t_hoff,
|
|
|
|
tupleDesc,
|
|
|
|
toast_values,
|
|
|
|
toast_nulls,
|
|
|
|
&(new_data->t_infomask),
|
|
|
|
has_nulls ? new_data->t_bits : NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free allocated temp values
|
|
|
|
*/
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
if (toast_free[i])
|
|
|
|
pfree(DatumGetPointer(toast_values[i]));
|
|
|
|
|
|
|
|
return PointerGetDatum(new_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
/* ----------
|
|
|
|
* toast_compress_datum -
|
|
|
|
*
|
2000-08-04 06:16:17 +02:00
|
|
|
* Create a compressed version of a varlena datum
|
2000-10-24 01:42:04 +02:00
|
|
|
*
|
|
|
|
* If we fail (ie, compressed result is actually bigger than original)
|
|
|
|
* then return NULL. We must not use compressed data if it'd expand
|
|
|
|
* the tuple!
|
2000-07-04 01:10:14 +02:00
|
|
|
* ----------
|
|
|
|
*/
|
2001-02-15 21:57:01 +01:00
|
|
|
Datum
|
2000-07-04 01:10:14 +02:00
|
|
|
toast_compress_datum(Datum value)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
varattrib *tmp;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2000-10-24 01:42:04 +02:00
|
|
|
tmp = (varattrib *) palloc(sizeof(PGLZ_Header) + VARATT_SIZE(value));
|
2000-07-04 01:10:14 +02:00
|
|
|
pglz_compress(VARATT_DATA(value), VARATT_SIZE(value) - VARHDRSZ,
|
2000-10-24 01:42:04 +02:00
|
|
|
(PGLZ_Header *) tmp,
|
|
|
|
PGLZ_strategy_default);
|
|
|
|
if (VARATT_SIZE(tmp) < VARATT_SIZE(value))
|
|
|
|
{
|
|
|
|
/* successful compression */
|
|
|
|
VARATT_SIZEP(tmp) |= VARATT_FLAG_COMPRESSED;
|
|
|
|
return PointerGetDatum(tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* incompressible data */
|
|
|
|
pfree(tmp);
|
|
|
|
return PointerGetDatum(NULL);
|
|
|
|
}
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* toast_save_datum -
|
|
|
|
*
|
|
|
|
* Save one single datum into the secondary relation and return
|
|
|
|
* a varattrib reference for it.
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static Datum
|
2001-08-10 20:57:42 +02:00
|
|
|
toast_save_datum(Relation rel, Datum value)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Relation toastrel;
|
|
|
|
Relation toastidx;
|
|
|
|
HeapTuple toasttup;
|
|
|
|
InsertIndexResult idxres;
|
|
|
|
TupleDesc toasttupDesc;
|
|
|
|
Datum t_values[3];
|
|
|
|
char t_nulls[3];
|
|
|
|
varattrib *result;
|
2001-10-25 07:50:21 +02:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
struct varlena hdr;
|
|
|
|
char data[TOAST_MAX_CHUNK_SIZE];
|
2001-03-25 01:45:20 +01:00
|
|
|
} chunk_data;
|
2001-03-22 05:01:46 +01:00
|
|
|
int32 chunk_size;
|
|
|
|
int32 chunk_seq = 0;
|
|
|
|
char *data_p;
|
|
|
|
int32 data_todo;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Create the varattrib reference
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
result = (varattrib *) palloc(sizeof(varattrib));
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
result->va_header = sizeof(varattrib) | VARATT_FLAG_EXTERNAL;
|
2000-07-04 01:10:14 +02:00
|
|
|
if (VARATT_IS_COMPRESSED(value))
|
|
|
|
{
|
|
|
|
result->va_header |= VARATT_FLAG_COMPRESSED;
|
2001-03-22 05:01:46 +01:00
|
|
|
result->va_content.va_external.va_rawsize =
|
|
|
|
((varattrib *) value)->va_content.va_compressed.va_rawsize;
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
result->va_content.va_external.va_rawsize = VARATT_SIZE(value);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
result->va_content.va_external.va_extsize =
|
|
|
|
VARATT_SIZE(value) - VARHDRSZ;
|
|
|
|
result->va_content.va_external.va_valueid = newoid();
|
|
|
|
result->va_content.va_external.va_toastrelid =
|
|
|
|
rel->rd_rel->reltoastrelid;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Initialize constant parts of the tuple data
|
|
|
|
*/
|
|
|
|
t_values[0] = ObjectIdGetDatum(result->va_content.va_external.va_valueid);
|
2001-03-25 01:45:20 +01:00
|
|
|
t_values[2] = PointerGetDatum(&chunk_data);
|
2000-07-04 01:10:14 +02:00
|
|
|
t_nulls[0] = ' ';
|
|
|
|
t_nulls[1] = ' ';
|
|
|
|
t_nulls[2] = ' ';
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Get the data to process
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
data_p = VARATT_DATA(value);
|
|
|
|
data_todo = VARATT_SIZE(value) - VARHDRSZ;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Open the toast relation
|
|
|
|
*/
|
|
|
|
toastrel = heap_open(rel->rd_rel->reltoastrelid, RowExclusiveLock);
|
|
|
|
toasttupDesc = toastrel->rd_att;
|
2001-08-10 20:57:42 +02:00
|
|
|
toastidx = index_open(toastrel->rd_rel->reltoastidxid);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* Split up the item into chunks
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
while (data_todo > 0)
|
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Calculate the size of this chunk
|
|
|
|
*/
|
2000-08-04 06:16:17 +02:00
|
|
|
chunk_size = Min(TOAST_MAX_CHUNK_SIZE, data_todo);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-08-10 20:57:42 +02:00
|
|
|
* Build a tuple and store it
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2000-08-04 06:16:17 +02:00
|
|
|
t_values[1] = Int32GetDatum(chunk_seq++);
|
2001-03-25 01:45:20 +01:00
|
|
|
VARATT_SIZEP(&chunk_data) = chunk_size + VARHDRSZ;
|
|
|
|
memcpy(VARATT_DATA(&chunk_data), data_p, chunk_size);
|
2000-07-04 01:10:14 +02:00
|
|
|
toasttup = heap_formtuple(toasttupDesc, t_values, t_nulls);
|
|
|
|
if (!HeapTupleIsValid(toasttup))
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "failed to build TOAST tuple");
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2002-05-22 00:05:55 +02:00
|
|
|
simple_heap_insert(toastrel, toasttup);
|
2001-08-10 20:57:42 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Create the index entry. We cheat a little here by not using
|
2001-08-10 20:57:42 +02:00
|
|
|
* FormIndexDatum: this relies on the knowledge that the index
|
|
|
|
* columns are the same as the initial columns of the table.
|
|
|
|
*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Note also that there had better not be any user-created index on
|
|
|
|
* the TOAST table, since we don't bother to update anything else.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
|
|
|
idxres = index_insert(toastidx, t_values, t_nulls,
|
2001-03-22 05:01:46 +01:00
|
|
|
&(toasttup->t_self),
|
2002-08-06 04:36:35 +02:00
|
|
|
toastrel, toastidx->rd_index->indisunique);
|
2000-07-04 01:10:14 +02:00
|
|
|
if (idxres == NULL)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(ERROR, "failed to insert index entry for TOAST tuple");
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Free memory
|
|
|
|
*/
|
|
|
|
pfree(idxres);
|
2001-08-10 20:57:42 +02:00
|
|
|
heap_freetuple(toasttup);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Move on to next chunk
|
|
|
|
*/
|
|
|
|
data_todo -= chunk_size;
|
|
|
|
data_p += chunk_size;
|
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Done - close toast relation and return the reference
|
|
|
|
*/
|
|
|
|
index_close(toastidx);
|
|
|
|
heap_close(toastrel, RowExclusiveLock);
|
|
|
|
|
|
|
|
return PointerGetDatum(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------
|
|
|
|
* toast_delete_datum -
|
|
|
|
*
|
|
|
|
* Delete a single external stored value.
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
toast_delete_datum(Relation rel, Datum value)
|
|
|
|
{
|
2002-05-21 01:51:44 +02:00
|
|
|
varattrib *attr = (varattrib *) DatumGetPointer(value);
|
2001-03-22 05:01:46 +01:00
|
|
|
Relation toastrel;
|
|
|
|
Relation toastidx;
|
|
|
|
ScanKeyData toastkey;
|
|
|
|
IndexScanDesc toastscan;
|
2002-05-21 01:51:44 +02:00
|
|
|
HeapTuple toasttup;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
|
|
|
if (!VARATT_IS_EXTERNAL(attr))
|
|
|
|
return;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Open the toast relation and it's index
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
toastrel = heap_open(attr->va_content.va_external.va_toastrelid,
|
|
|
|
RowExclusiveLock);
|
2001-08-10 20:57:42 +02:00
|
|
|
toastidx = index_open(toastrel->rd_rel->reltoastidxid);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-10-25 07:50:21 +02:00
|
|
|
* Setup a scan key to fetch from the index by va_valueid (we don't
|
|
|
|
* particularly care whether we see them in sequence or not)
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&toastkey,
|
|
|
|
(AttrNumber) 1,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(attr->va_content.va_external.va_valueid));
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2001-08-10 20:57:42 +02:00
|
|
|
* Find the chunks by index
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
|
|
|
|
1, &toastkey);
|
|
|
|
while ((toasttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Have a chunk, delete it
|
|
|
|
*/
|
2002-05-21 01:51:44 +02:00
|
|
|
simple_heap_delete(toastrel, &toasttup->t_self);
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* End scan and close relations
|
|
|
|
*/
|
|
|
|
index_endscan(toastscan);
|
|
|
|
index_close(toastidx);
|
|
|
|
heap_close(toastrel, RowExclusiveLock);
|
1999-12-21 01:06:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
/* ----------
|
|
|
|
* toast_fetch_datum -
|
|
|
|
*
|
|
|
|
* Reconstruct an in memory varattrib from the chunks saved
|
|
|
|
* in the toast relation
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static varattrib *
|
|
|
|
toast_fetch_datum(varattrib *attr)
|
1999-12-21 01:06:44 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Relation toastrel;
|
|
|
|
Relation toastidx;
|
|
|
|
ScanKeyData toastkey;
|
|
|
|
IndexScanDesc toastscan;
|
|
|
|
HeapTuple ttup;
|
|
|
|
TupleDesc toasttupDesc;
|
|
|
|
varattrib *result;
|
|
|
|
int32 ressize;
|
2002-01-16 21:29:02 +01:00
|
|
|
int32 residx,
|
|
|
|
nextidx;
|
|
|
|
int32 numchunks;
|
2001-03-22 05:01:46 +01:00
|
|
|
Pointer chunk;
|
|
|
|
bool isnull;
|
|
|
|
int32 chunksize;
|
|
|
|
|
2000-07-04 01:10:14 +02:00
|
|
|
ressize = attr->va_content.va_external.va_extsize;
|
2001-03-22 05:01:46 +01:00
|
|
|
numchunks = ((ressize - 1) / TOAST_MAX_CHUNK_SIZE) + 1;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
result = (varattrib *) palloc(ressize + VARHDRSZ);
|
2000-07-04 01:10:14 +02:00
|
|
|
VARATT_SIZEP(result) = ressize + VARHDRSZ;
|
|
|
|
if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
VARATT_SIZEP(result) |= VARATT_FLAG_COMPRESSED;
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2002-03-05 06:33:31 +01:00
|
|
|
* Open the toast relation and its index
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
toastrel = heap_open(attr->va_content.va_external.va_toastrelid,
|
|
|
|
AccessShareLock);
|
2000-07-04 01:10:14 +02:00
|
|
|
toasttupDesc = toastrel->rd_att;
|
2001-08-10 20:57:42 +02:00
|
|
|
toastidx = index_open(toastrel->rd_rel->reltoastidxid);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Setup a scan key to fetch from the index by va_valueid
|
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&toastkey,
|
|
|
|
(AttrNumber) 1,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(attr->va_content.va_external.va_valueid));
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Read the chunks by index
|
2000-08-04 06:16:17 +02:00
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* Note that because the index is actually on (valueid, chunkidx) we will
|
|
|
|
* see the chunks in chunkidx order, even though we didn't explicitly
|
|
|
|
* ask for it.
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2002-01-16 21:29:02 +01:00
|
|
|
nextidx = 0;
|
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
|
|
|
|
1, &toastkey);
|
|
|
|
while ((ttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
|
2000-07-04 01:10:14 +02:00
|
|
|
{
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* Have a chunk, extract the sequence number and the data
|
|
|
|
*/
|
2000-08-04 06:16:17 +02:00
|
|
|
residx = DatumGetInt32(heap_getattr(ttup, 2, toasttupDesc, &isnull));
|
|
|
|
Assert(!isnull);
|
|
|
|
chunk = DatumGetPointer(heap_getattr(ttup, 3, toasttupDesc, &isnull));
|
|
|
|
Assert(!isnull);
|
|
|
|
chunksize = VARATT_SIZE(chunk) - VARHDRSZ;
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-11 14:32:03 +02:00
|
|
|
* Some checks on the data we've found
|
|
|
|
*/
|
2002-01-16 21:29:02 +01:00
|
|
|
if (residx != nextidx)
|
|
|
|
elog(ERROR, "unexpected chunk number %d (expected %d) for toast value %u",
|
|
|
|
residx, nextidx,
|
2000-08-04 06:16:17 +02:00
|
|
|
attr->va_content.va_external.va_valueid);
|
2001-03-22 05:01:46 +01:00
|
|
|
if (residx < numchunks - 1)
|
2000-08-04 06:16:17 +02:00
|
|
|
{
|
|
|
|
if (chunksize != TOAST_MAX_CHUNK_SIZE)
|
2001-08-10 20:57:42 +02:00
|
|
|
elog(ERROR, "unexpected chunk size %d in chunk %d for toast value %u",
|
2000-08-04 06:16:17 +02:00
|
|
|
chunksize, residx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
}
|
2002-01-16 21:29:02 +01:00
|
|
|
else if (residx < numchunks)
|
2000-08-04 06:16:17 +02:00
|
|
|
{
|
|
|
|
if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != ressize)
|
2001-08-10 20:57:42 +02:00
|
|
|
elog(ERROR, "unexpected chunk size %d in chunk %d for toast value %u",
|
2000-08-04 06:16:17 +02:00
|
|
|
chunksize, residx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
}
|
2002-01-16 21:29:02 +01:00
|
|
|
else
|
|
|
|
elog(ERROR, "unexpected chunk number %d for toast value %u",
|
2000-08-04 06:16:17 +02:00
|
|
|
residx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
2000-07-11 14:32:03 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-08-04 06:16:17 +02:00
|
|
|
* Copy the data into proper place in our result
|
2000-07-04 01:10:14 +02:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
memcpy(((char *) VARATT_DATA(result)) + residx * TOAST_MAX_CHUNK_SIZE,
|
2000-08-04 06:16:17 +02:00
|
|
|
VARATT_DATA(chunk),
|
|
|
|
chunksize);
|
2000-07-04 01:10:14 +02:00
|
|
|
|
2002-01-16 21:29:02 +01:00
|
|
|
nextidx++;
|
2000-07-04 01:10:14 +02:00
|
|
|
}
|
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-11 14:32:03 +02:00
|
|
|
* Final checks that we successfully fetched the datum
|
|
|
|
*/
|
2002-01-16 21:29:02 +01:00
|
|
|
if (nextidx != numchunks)
|
|
|
|
elog(ERROR, "missing chunk number %d for toast value %u",
|
|
|
|
nextidx,
|
2001-03-22 05:01:46 +01:00
|
|
|
attr->va_content.va_external.va_valueid);
|
2000-07-11 14:32:03 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
2000-07-04 01:10:14 +02:00
|
|
|
* End scan and close relations
|
|
|
|
*/
|
|
|
|
index_endscan(toastscan);
|
|
|
|
index_close(toastidx);
|
|
|
|
heap_close(toastrel, AccessShareLock);
|
|
|
|
|
|
|
|
return result;
|
1999-12-21 01:06:44 +01:00
|
|
|
}
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
/* ----------
|
|
|
|
* toast_fetch_datum_slice -
|
|
|
|
*
|
|
|
|
* Reconstruct a segment of a varattrib from the chunks saved
|
|
|
|
* in the toast relation
|
|
|
|
* ----------
|
|
|
|
*/
|
|
|
|
static varattrib *
|
|
|
|
toast_fetch_datum_slice(varattrib *attr, int32 sliceoffset, int32 length)
|
|
|
|
{
|
|
|
|
Relation toastrel;
|
|
|
|
Relation toastidx;
|
|
|
|
ScanKeyData toastkey[3];
|
2002-05-21 01:51:44 +02:00
|
|
|
int nscankeys;
|
2002-03-05 06:33:31 +01:00
|
|
|
IndexScanDesc toastscan;
|
|
|
|
HeapTuple ttup;
|
|
|
|
TupleDesc toasttupDesc;
|
|
|
|
varattrib *result;
|
|
|
|
int32 attrsize;
|
|
|
|
int32 residx;
|
2002-09-04 22:31:48 +02:00
|
|
|
int32 nextidx;
|
|
|
|
int numchunks;
|
|
|
|
int startchunk;
|
|
|
|
int endchunk;
|
2002-03-05 06:33:31 +01:00
|
|
|
int32 startoffset;
|
|
|
|
int32 endoffset;
|
2002-09-04 22:31:48 +02:00
|
|
|
int totalchunks;
|
2002-03-05 06:33:31 +01:00
|
|
|
Pointer chunk;
|
|
|
|
bool isnull;
|
|
|
|
int32 chunksize;
|
2002-09-04 22:31:48 +02:00
|
|
|
int32 chcpystrt;
|
|
|
|
int32 chcpyend;
|
2002-03-05 06:33:31 +01:00
|
|
|
|
|
|
|
attrsize = attr->va_content.va_external.va_extsize;
|
|
|
|
totalchunks = ((attrsize - 1) / TOAST_MAX_CHUNK_SIZE) + 1;
|
|
|
|
|
2002-09-04 22:31:48 +02:00
|
|
|
if (sliceoffset >= attrsize)
|
2002-05-21 01:51:44 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
sliceoffset = 0;
|
|
|
|
length = 0;
|
2002-05-21 01:51:44 +02:00
|
|
|
}
|
2002-03-05 06:33:31 +01:00
|
|
|
|
|
|
|
if (((sliceoffset + length) > attrsize) || length < 0)
|
2002-09-04 22:31:48 +02:00
|
|
|
length = attrsize - sliceoffset;
|
2002-03-05 06:33:31 +01:00
|
|
|
|
|
|
|
result = (varattrib *) palloc(length + VARHDRSZ);
|
|
|
|
VARATT_SIZEP(result) = length + VARHDRSZ;
|
|
|
|
|
|
|
|
if (VARATT_IS_COMPRESSED(attr))
|
|
|
|
VARATT_SIZEP(result) |= VARATT_FLAG_COMPRESSED;
|
2002-09-04 22:31:48 +02:00
|
|
|
|
|
|
|
if (length == 0)
|
|
|
|
return (result); /* Can save a lot of work at this point! */
|
2002-03-05 06:33:31 +01:00
|
|
|
|
|
|
|
startchunk = sliceoffset / TOAST_MAX_CHUNK_SIZE;
|
|
|
|
endchunk = (sliceoffset + length - 1) / TOAST_MAX_CHUNK_SIZE;
|
2002-09-04 22:31:48 +02:00
|
|
|
numchunks = (endchunk - startchunk) + 1;
|
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
startoffset = sliceoffset % TOAST_MAX_CHUNK_SIZE;
|
|
|
|
endoffset = (sliceoffset + length - 1) % TOAST_MAX_CHUNK_SIZE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the toast relation and it's index
|
|
|
|
*/
|
|
|
|
toastrel = heap_open(attr->va_content.va_external.va_toastrelid,
|
|
|
|
AccessShareLock);
|
|
|
|
toasttupDesc = toastrel->rd_att;
|
|
|
|
toastidx = index_open(toastrel->rd_rel->reltoastidxid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup a scan key to fetch from the index. This is either two keys
|
|
|
|
* or three depending on the number of chunks.
|
|
|
|
*/
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&toastkey[0],
|
|
|
|
(AttrNumber) 1,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(attr->va_content.va_external.va_valueid));
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
/*
|
2003-11-09 22:30:38 +01:00
|
|
|
* Use equality condition for one chunk, a range condition otherwise:
|
2002-03-05 06:33:31 +01:00
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
if (numchunks == 1)
|
2002-03-05 06:33:31 +01:00
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&toastkey[1],
|
|
|
|
(AttrNumber) 2,
|
|
|
|
BTEqualStrategyNumber, F_INT4EQ,
|
|
|
|
Int32GetDatum(startchunk));
|
2002-09-04 22:31:48 +02:00
|
|
|
nscankeys = 2;
|
2002-03-05 06:33:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&toastkey[1],
|
|
|
|
(AttrNumber) 2,
|
|
|
|
BTGreaterEqualStrategyNumber, F_INT4GE,
|
|
|
|
Int32GetDatum(startchunk));
|
|
|
|
ScanKeyInit(&toastkey[2],
|
|
|
|
(AttrNumber) 2,
|
|
|
|
BTLessEqualStrategyNumber, F_INT4LE,
|
|
|
|
Int32GetDatum(endchunk));
|
2002-09-04 22:31:48 +02:00
|
|
|
nscankeys = 3;
|
2002-03-05 06:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the chunks by index
|
|
|
|
*
|
|
|
|
* The index is on (valueid, chunkidx) so they will come in order
|
|
|
|
*/
|
|
|
|
nextidx = startchunk;
|
2002-05-21 01:51:44 +02:00
|
|
|
toastscan = index_beginscan(toastrel, toastidx, SnapshotToast,
|
|
|
|
nscankeys, toastkey);
|
|
|
|
while ((ttup = index_getnext(toastscan, ForwardScanDirection)) != NULL)
|
2002-03-05 06:33:31 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Have a chunk, extract the sequence number and the data
|
|
|
|
*/
|
|
|
|
residx = DatumGetInt32(heap_getattr(ttup, 2, toasttupDesc, &isnull));
|
|
|
|
Assert(!isnull);
|
|
|
|
chunk = DatumGetPointer(heap_getattr(ttup, 3, toasttupDesc, &isnull));
|
|
|
|
Assert(!isnull);
|
|
|
|
chunksize = VARATT_SIZE(chunk) - VARHDRSZ;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some checks on the data we've found
|
|
|
|
*/
|
|
|
|
if ((residx != nextidx) || (residx > endchunk) || (residx < startchunk))
|
|
|
|
elog(ERROR, "unexpected chunk number %d (expected %d) for toast value %u",
|
|
|
|
residx, nextidx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
if (residx < totalchunks - 1)
|
|
|
|
{
|
|
|
|
if (chunksize != TOAST_MAX_CHUNK_SIZE)
|
|
|
|
elog(ERROR, "unexpected chunk size %d in chunk %d for toast value %u",
|
|
|
|
chunksize, residx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((residx * TOAST_MAX_CHUNK_SIZE + chunksize) != attrsize)
|
|
|
|
elog(ERROR, "unexpected chunk size %d in chunk %d for toast value %u",
|
|
|
|
chunksize, residx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the data into proper place in our result
|
|
|
|
*/
|
|
|
|
chcpystrt = 0;
|
|
|
|
chcpyend = chunksize - 1;
|
2002-09-04 22:31:48 +02:00
|
|
|
if (residx == startchunk)
|
|
|
|
chcpystrt = startoffset;
|
|
|
|
if (residx == endchunk)
|
|
|
|
chcpyend = endoffset;
|
|
|
|
|
|
|
|
memcpy(((char *) VARATT_DATA(result)) +
|
|
|
|
(residx * TOAST_MAX_CHUNK_SIZE - sliceoffset) + chcpystrt,
|
2002-03-05 06:33:31 +01:00
|
|
|
VARATT_DATA(chunk) + chcpystrt,
|
|
|
|
(chcpyend - chcpystrt) + 1);
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-03-05 06:33:31 +01:00
|
|
|
nextidx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Final checks that we successfully fetched the datum
|
|
|
|
*/
|
2002-09-04 22:31:48 +02:00
|
|
|
if (nextidx != (endchunk + 1))
|
2002-03-05 06:33:31 +01:00
|
|
|
elog(ERROR, "missing chunk number %d for toast value %u",
|
|
|
|
nextidx,
|
|
|
|
attr->va_content.va_external.va_valueid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End scan and close relations
|
|
|
|
*/
|
|
|
|
index_endscan(toastscan);
|
|
|
|
index_close(toastidx);
|
|
|
|
heap_close(toastrel, AccessShareLock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|