1998-02-04 22:32:12 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* heaptuple.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* This file contains heap tuple accessor and mutator routines, as well
|
|
|
|
* as a few various tuple utilities.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-02-11 20:14:04 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.36 1998/02/11 19:09:21 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* NOTES
|
1997-09-07 07:04:48 +02:00
|
|
|
* The old interface functions have been converted to macros
|
|
|
|
* and moved to heapam.h
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1996-11-05 08:42:46 +01:00
|
|
|
#include <postgres.h>
|
1996-10-21 13:49:41 +02:00
|
|
|
|
1996-11-10 04:06:38 +01:00
|
|
|
#include <access/heapam.h>
|
1996-11-05 08:42:46 +01:00
|
|
|
#include <access/htup.h>
|
|
|
|
#include <access/transam.h>
|
|
|
|
#include <access/tupmacs.h>
|
1998-02-04 22:32:12 +01:00
|
|
|
#include <catalog/pg_type.h>
|
1996-11-05 08:42:46 +01:00
|
|
|
#include <storage/bufpage.h>
|
|
|
|
#include <utils/memutils.h>
|
1996-10-21 00:04:49 +02:00
|
|
|
|
|
|
|
#ifndef HAVE_MEMMOVE
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <regex/utils.h>
|
1996-10-21 00:04:49 +02:00
|
|
|
#else
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <string.h>
|
1996-10-21 00:04:49 +02:00
|
|
|
#endif
|
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
/* Used by heap_getattr() macro, for speed */
|
|
|
|
long heap_sysoffset[] = {
|
|
|
|
/* Only the first one is pass-by-ref, and is handled specially in the macro */
|
|
|
|
offsetof(HeapTupleData, t_ctid),
|
|
|
|
offsetof(HeapTupleData, t_oid),
|
|
|
|
offsetof(HeapTupleData, t_xmin),
|
|
|
|
offsetof(HeapTupleData, t_cmin),
|
|
|
|
offsetof(HeapTupleData, t_xmax),
|
|
|
|
offsetof(HeapTupleData, t_cmax)
|
|
|
|
};
|
1996-10-21 00:04:49 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* misc support routines
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* ComputeDataSize
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Size
|
|
|
|
ComputeDataSize(TupleDesc tupleDesc,
|
1997-09-07 07:04:48 +02:00
|
|
|
Datum value[],
|
|
|
|
char nulls[])
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
uint32 data_length;
|
|
|
|
int i;
|
|
|
|
int numberOfAttributes = tupleDesc->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
AttributeTupleForm *att = tupleDesc->attrs;
|
|
|
|
|
|
|
|
for (data_length = 0, i = 0; i < numberOfAttributes; i++)
|
|
|
|
{
|
|
|
|
if (nulls[i] != ' ')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (att[i]->attlen)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case -1:
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
/*
|
|
|
|
* This is the size of the disk representation and so must
|
|
|
|
* include the additional sizeof long.
|
|
|
|
*/
|
|
|
|
if (att[i]->attalign == 'd')
|
|
|
|
{
|
|
|
|
data_length = DOUBLEALIGN(data_length)
|
|
|
|
+ VARSIZE(DatumGetPointer(value[i]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data_length = INTALIGN(data_length)
|
|
|
|
+ VARSIZE(DatumGetPointer(value[i]));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case sizeof(char):
|
|
|
|
data_length++;
|
|
|
|
break;
|
|
|
|
case sizeof(short):
|
|
|
|
data_length = SHORTALIGN(data_length + sizeof(short));
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
data_length = INTALIGN(data_length + sizeof(int32));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (att[i]->attlen < sizeof(int32))
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "ComputeDataSize: attribute %d has len %d",
|
1997-09-08 04:41:22 +02:00
|
|
|
i, att[i]->attlen);
|
|
|
|
if (att[i]->attalign == 'd')
|
|
|
|
data_length = DOUBLEALIGN(data_length) + att[i]->attlen;
|
|
|
|
else
|
|
|
|
data_length = LONGALIGN(data_length) + att[i]->attlen;
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
return data_length;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* DataFill
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
DataFill(char *data,
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleDesc tupleDesc,
|
|
|
|
Datum value[],
|
|
|
|
char nulls[],
|
1997-11-02 16:27:14 +01:00
|
|
|
uint16 *infomask,
|
1997-09-08 23:56:23 +02:00
|
|
|
bits8 *bit)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
bits8 *bitP = 0;
|
|
|
|
int bitmask = 0;
|
|
|
|
uint32 data_length;
|
|
|
|
int i;
|
|
|
|
int numberOfAttributes = tupleDesc->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
AttributeTupleForm *att = tupleDesc->attrs;
|
|
|
|
|
|
|
|
if (bit != NULL)
|
|
|
|
{
|
|
|
|
bitP = &bit[-1];
|
|
|
|
bitmask = CSIGNBIT;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
*infomask = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < numberOfAttributes; i++)
|
|
|
|
{
|
|
|
|
if (bit != NULL)
|
|
|
|
{
|
|
|
|
if (bitmask != CSIGNBIT)
|
|
|
|
{
|
|
|
|
bitmask <<= 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bitP += 1;
|
|
|
|
*bitP = 0x0;
|
|
|
|
bitmask = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nulls[i] == 'n')
|
|
|
|
{
|
|
|
|
*infomask |= HEAP_HASNULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bitP |= bitmask;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (att[i]->attlen)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case -1:
|
|
|
|
*infomask |= HEAP_HASVARLENA;
|
|
|
|
if (att[i]->attalign == 'd')
|
|
|
|
{
|
|
|
|
data = (char *) DOUBLEALIGN(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = (char *) INTALIGN(data);
|
|
|
|
}
|
|
|
|
data_length = VARSIZE(DatumGetPointer(value[i]));
|
|
|
|
memmove(data, DatumGetPointer(value[i]), data_length);
|
|
|
|
data += data_length;
|
|
|
|
break;
|
|
|
|
case sizeof(char):
|
|
|
|
*data = att[i]->attbyval ?
|
|
|
|
DatumGetChar(value[i]) : *((char *) value[i]);
|
|
|
|
data += sizeof(char);
|
|
|
|
break;
|
|
|
|
case sizeof(int16):
|
|
|
|
data = (char *) SHORTALIGN(data);
|
|
|
|
*(short *) data = (att[i]->attbyval ?
|
|
|
|
DatumGetInt16(value[i]) :
|
|
|
|
*((short *) value[i]));
|
|
|
|
data += sizeof(short);
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
1997-09-07 07:04:48 +02:00
|
|
|
data = (char *) INTALIGN(data);
|
1997-09-08 04:41:22 +02:00
|
|
|
*(int32 *) data = (att[i]->attbyval ?
|
|
|
|
DatumGetInt32(value[i]) :
|
|
|
|
*((int32 *) value[i]));
|
|
|
|
data += sizeof(int32);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (att[i]->attlen < sizeof(int32))
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "DataFill: attribute %d has len %d",
|
1997-09-08 04:41:22 +02:00
|
|
|
i, att[i]->attlen);
|
|
|
|
if (att[i]->attalign == 'd')
|
|
|
|
{
|
|
|
|
data = (char *) DOUBLEALIGN(data);
|
|
|
|
memmove(data, DatumGetPointer(value[i]),
|
|
|
|
att[i]->attlen);
|
|
|
|
data += att[i]->attlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = (char *) LONGALIGN(data);
|
|
|
|
memmove(data, DatumGetPointer(value[i]),
|
|
|
|
att[i]->attlen);
|
|
|
|
data += att[i]->attlen;
|
|
|
|
}
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap tuple interface
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_attisnull - returns 1 iff tuple attribute is not present
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
heap_attisnull(HeapTuple tup, int attnum)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
if (attnum > (int) tup->t_natts)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (HeapTupleNoNulls(tup))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (attnum > 0)
|
|
|
|
{
|
|
|
|
return (att_isnull(attnum - 1, tup->t_bits));
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
switch (attnum)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case SelfItemPointerAttributeNumber:
|
|
|
|
case ObjectIdAttributeNumber:
|
|
|
|
case MinTransactionIdAttributeNumber:
|
|
|
|
case MinCommandIdAttributeNumber:
|
|
|
|
case MaxTransactionIdAttributeNumber:
|
|
|
|
case MaxCommandIdAttributeNumber:
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
case 0:
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "heap_attisnull: zero attnum disallowed");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "heap_attisnull: undefined negative attnum");
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* system attribute heap tuple support
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_sysattrlen
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This routine returns the length of a system attribute.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
heap_sysattrlen(AttrNumber attno)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTupleData *f = NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (attno)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case SelfItemPointerAttributeNumber:
|
|
|
|
return sizeof f->t_ctid;
|
|
|
|
case ObjectIdAttributeNumber:
|
|
|
|
return sizeof f->t_oid;
|
|
|
|
case MinTransactionIdAttributeNumber:
|
|
|
|
return sizeof f->t_xmin;
|
|
|
|
case MinCommandIdAttributeNumber:
|
|
|
|
return sizeof f->t_cmin;
|
|
|
|
case MaxTransactionIdAttributeNumber:
|
|
|
|
return sizeof f->t_xmax;
|
|
|
|
case MaxCommandIdAttributeNumber:
|
|
|
|
return sizeof f->t_cmax;
|
|
|
|
|
|
|
|
default:
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "sysattrlen: System attribute number %d unknown.", attno);
|
1997-09-08 04:41:22 +02:00
|
|
|
return 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_sysattrbyval
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This routine returns the "by-value" property of a system attribute.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
heap_sysattrbyval(AttrNumber attno)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
bool byval;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
switch (attno)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case SelfItemPointerAttributeNumber:
|
|
|
|
byval = false;
|
|
|
|
break;
|
|
|
|
case ObjectIdAttributeNumber:
|
|
|
|
byval = true;
|
|
|
|
break;
|
|
|
|
case MinTransactionIdAttributeNumber:
|
|
|
|
byval = true;
|
|
|
|
break;
|
|
|
|
case MinCommandIdAttributeNumber:
|
|
|
|
byval = true;
|
|
|
|
break;
|
|
|
|
case MaxTransactionIdAttributeNumber:
|
|
|
|
byval = true;
|
|
|
|
break;
|
|
|
|
case MaxCommandIdAttributeNumber:
|
|
|
|
byval = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
byval = true;
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "sysattrbyval: System attribute number %d unknown.",
|
1997-09-08 04:41:22 +02:00
|
|
|
attno);
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return byval;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_getsysattr
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1997-09-12 06:09:08 +02:00
|
|
|
Datum
|
1996-07-09 08:22:35 +02:00
|
|
|
heap_getsysattr(HeapTuple tup, Buffer b, int attnum)
|
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (attnum)
|
|
|
|
{
|
1998-01-31 05:39:26 +01:00
|
|
|
case SelfItemPointerAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) &tup->t_ctid);
|
1997-09-08 04:41:22 +02:00
|
|
|
case ObjectIdAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) (long) tup->t_oid);
|
1997-09-08 04:41:22 +02:00
|
|
|
case MinTransactionIdAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) (long) tup->t_xmin);
|
1997-09-08 04:41:22 +02:00
|
|
|
case MinCommandIdAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) (long) tup->t_cmin);
|
1997-09-08 04:41:22 +02:00
|
|
|
case MaxTransactionIdAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) (long) tup->t_xmax);
|
1997-09-08 04:41:22 +02:00
|
|
|
case MaxCommandIdAttributeNumber:
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) (long) tup->t_cmax);
|
1997-09-08 04:41:22 +02:00
|
|
|
default:
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "heap_getsysattr: undefined attnum %d", attnum);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-24 19:45:21 +02:00
|
|
|
return ((Datum) NULL);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1998-01-31 05:39:26 +01:00
|
|
|
* nocachegetattr
|
|
|
|
*
|
|
|
|
* This only gets called from fastgetattr() macro, in cases where
|
|
|
|
* we can't use a cacheoffset and the value is not null.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1998-01-31 05:39:26 +01:00
|
|
|
* This caches attribute offsets in the attribute descriptor.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* an alternate way to speed things up would be to cache offsets
|
|
|
|
* with the tuple, but that seems more difficult unless you take
|
|
|
|
* the storage hit of actually putting those offsets into the
|
|
|
|
* tuple you send to disk. Yuck.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This scheme will be slightly slower than that, but should
|
|
|
|
* preform well for queries which hit large #'s of tuples. After
|
|
|
|
* you cache the offsets once, examining all the other tuples using
|
|
|
|
* the same attribute descriptor will go much quicker. -cim 5/4/91
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1997-09-12 06:09:08 +02:00
|
|
|
Datum
|
1998-01-31 05:39:26 +01:00
|
|
|
nocachegetattr(HeapTuple tup,
|
1997-09-07 07:04:48 +02:00
|
|
|
int attnum,
|
|
|
|
TupleDesc tupleDesc,
|
1997-09-08 23:56:23 +02:00
|
|
|
bool *isnull)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *tp; /* ptr to att in tuple */
|
1998-02-05 04:47:08 +01:00
|
|
|
bits8 *bp = tup->t_bits; /* ptr to att in tuple */
|
1997-09-08 04:41:22 +02:00
|
|
|
int slow; /* do we have to walk nulls? */
|
1997-09-07 07:04:48 +02:00
|
|
|
AttributeTupleForm *att = tupleDesc->attrs;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
|
|
|
|
#if IN_MACRO
|
|
|
|
/* This is handled in the macro */
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(attnum > 0);
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
if (isnull)
|
|
|
|
*isnull = false;
|
|
|
|
#endif
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* Three cases:
|
|
|
|
*
|
|
|
|
* 1: No nulls and no variable length attributes.
|
|
|
|
* 2: Has a null or a varlena AFTER att.
|
|
|
|
* 3: Has nulls or varlenas BEFORE att.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (HeapTupleNoNulls(tup))
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
attnum--;
|
1998-01-31 05:39:26 +01:00
|
|
|
|
|
|
|
#if IN_MACRO
|
|
|
|
/* This is handled in the macro */
|
1998-02-06 21:18:01 +01:00
|
|
|
if (att[attnum]->attcacheoff != -1)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-12 06:09:08 +02:00
|
|
|
return (Datum)
|
1997-09-07 07:04:48 +02:00
|
|
|
fetchatt(&(att[attnum]),
|
|
|
|
(char *) tup + tup->t_hoff + att[attnum]->attcacheoff);
|
|
|
|
}
|
|
|
|
else if (attnum == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* first attribute is always at position zero
|
|
|
|
*/
|
1997-09-12 06:09:08 +02:00
|
|
|
return ((Datum) fetchatt(&(att[0]), (char *) tup + tup->t_hoff));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-01-31 05:39:26 +01:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
slow = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there's a null somewhere in the tuple
|
|
|
|
*/
|
|
|
|
|
|
|
|
tp = (char *) tup + tup->t_hoff;
|
|
|
|
slow = 0;
|
|
|
|
attnum--;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* check to see if desired att is null
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
#if IN_MACRO
|
|
|
|
/* This is handled in the macro */
|
1997-09-07 07:04:48 +02:00
|
|
|
if (att_isnull(attnum, bp))
|
|
|
|
{
|
|
|
|
if (isnull)
|
|
|
|
*isnull = true;
|
1997-09-24 19:45:21 +02:00
|
|
|
return (Datum) NULL;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1998-01-31 05:39:26 +01:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Now check to see if any preceeding bits are null...
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
int i = 0; /* current offset in bp */
|
|
|
|
int mask; /* bit in byte we're looking at */
|
|
|
|
char n; /* current byte in bp */
|
|
|
|
int byte,
|
1998-02-04 22:32:12 +01:00
|
|
|
finalbit;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-04 22:32:12 +01:00
|
|
|
byte = attnum >> 3;
|
|
|
|
finalbit = attnum & 0x07;
|
|
|
|
|
|
|
|
for (; i <= byte && !slow; i++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-02-04 22:32:12 +01:00
|
|
|
n = bp[i];
|
|
|
|
if (i < byte)
|
|
|
|
{
|
|
|
|
/* check for nulls in any "earlier" bytes */
|
|
|
|
if ((~n) != 0)
|
|
|
|
slow=1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* check for nulls "before" final bit of last byte */
|
|
|
|
mask = (1 << finalbit) - 1;
|
|
|
|
if ((~n) & mask)
|
|
|
|
slow=1;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-04 22:32:12 +01:00
|
|
|
tp = (char *) tup + tup->t_hoff;
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* now check for any non-fixed length attrs before our attribute
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!slow)
|
|
|
|
{
|
1998-02-06 21:18:01 +01:00
|
|
|
if (att[attnum]->attcacheoff != -1)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-02-04 22:32:12 +01:00
|
|
|
return (Datum)fetchatt(&(att[attnum]),
|
|
|
|
tp + att[attnum]->attcacheoff);
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else if (attnum == 0)
|
|
|
|
{
|
1998-02-04 22:32:12 +01:00
|
|
|
return ((Datum) fetchatt(&(att[0]), (char *) tp));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else if (!HeapTupleAllFixed(tup))
|
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
int j = 0;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-05 04:47:08 +01:00
|
|
|
/*
|
|
|
|
* In for(), we make this <= and not < because we want to
|
|
|
|
* test if we can go past it in initializing offsets.
|
|
|
|
*/
|
|
|
|
for (j = 0; j <= attnum && !slow; j++)
|
1998-02-04 22:32:12 +01:00
|
|
|
if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j]))
|
1997-09-07 07:04:48 +02:00
|
|
|
slow = 1;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
1997-09-07 07:04:48 +02:00
|
|
|
* if slow is zero, and we got here, we know that we have a tuple with
|
|
|
|
* no nulls. We also have to initialize the remainder of the
|
|
|
|
* attribute cached offset values.
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!slow)
|
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
int j = 1;
|
|
|
|
long off;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* need to set cache for some atts
|
|
|
|
*/
|
|
|
|
|
|
|
|
att[0]->attcacheoff = 0;
|
|
|
|
|
|
|
|
while (att[j]->attcacheoff > 0)
|
|
|
|
j++;
|
|
|
|
|
1998-02-05 16:08:49 +01:00
|
|
|
if (!VARLENA_FIXED_SIZE(att[j - 1]))
|
1998-02-04 22:32:12 +01:00
|
|
|
off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
|
|
|
|
else
|
|
|
|
off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-05 04:47:08 +01:00
|
|
|
for (; j <= attnum ||
|
|
|
|
/* Can we compute more? We will probably need them */
|
|
|
|
(j < tup->t_natts &&
|
|
|
|
att[j]->attcacheoff == -1 &&
|
|
|
|
(HeapTupleNoNulls(tup) || !att_isnull(j, bp)) &&
|
|
|
|
(HeapTupleAllFixed(tup)||
|
|
|
|
att[j]->attlen > 0 || VARLENA_FIXED_SIZE(att[j]))); j++)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1998-02-04 22:32:12 +01:00
|
|
|
/*
|
|
|
|
* Fix me when going to a machine with more than a four-byte
|
|
|
|
* word!
|
|
|
|
*/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
switch (att[j]->attlen)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case -1:
|
|
|
|
off = (att[j]->attalign == 'd') ?
|
|
|
|
DOUBLEALIGN(off) : INTALIGN(off);
|
|
|
|
break;
|
|
|
|
case sizeof(char):
|
|
|
|
break;
|
|
|
|
case sizeof(short):
|
|
|
|
off = SHORTALIGN(off);
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
off = INTALIGN(off);
|
|
|
|
break;
|
|
|
|
default:
|
1998-02-04 22:32:12 +01:00
|
|
|
if (att[j]->attlen > sizeof(int32))
|
|
|
|
off = (att[j]->attalign == 'd') ?
|
|
|
|
DOUBLEALIGN(off) : LONGALIGN(off);
|
1997-09-08 04:41:22 +02:00
|
|
|
else
|
1998-02-04 22:32:12 +01:00
|
|
|
elog(ERROR, "nocache_index_getattr: attribute %d has len %d",
|
|
|
|
j, att[j]->attlen);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
att[j]->attcacheoff = off;
|
1998-02-04 22:32:12 +01:00
|
|
|
|
1998-02-05 04:47:08 +01:00
|
|
|
switch (att[j]->attlen)
|
1998-02-04 22:32:12 +01:00
|
|
|
{
|
1998-02-05 04:47:08 +01:00
|
|
|
case sizeof(char):
|
|
|
|
off++;
|
|
|
|
break;
|
|
|
|
case sizeof(short):
|
|
|
|
off += sizeof(short);
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
off += sizeof(int32);
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
Assert(!VARLENA_FIXED_SIZE(att[j]) ||
|
|
|
|
att[j]->atttypmod == VARSIZE(tp + off));
|
|
|
|
off += VARSIZE(tp + off);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
off += att[j]->attlen;
|
|
|
|
break;
|
1998-02-04 22:32:12 +01:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-02-04 22:32:12 +01:00
|
|
|
return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
else
|
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
bool usecache = true;
|
|
|
|
int off = 0;
|
|
|
|
int i;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now we know that we have to walk the tuple CAREFULLY.
|
|
|
|
*
|
|
|
|
* Note - This loop is a little tricky. On iteration i we first set
|
|
|
|
* the offset for attribute i and figure out how much the offset
|
|
|
|
* should be incremented. Finally, we need to align the offset
|
|
|
|
* based on the size of attribute i+1 (for which the offset has
|
|
|
|
* been computed). -mer 12 Dec 1991
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < attnum; i++)
|
|
|
|
{
|
|
|
|
if (!HeapTupleNoNulls(tup))
|
|
|
|
{
|
|
|
|
if (att_isnull(i, bp))
|
|
|
|
{
|
|
|
|
usecache = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
1998-02-04 22:32:12 +01:00
|
|
|
|
|
|
|
/* If we know the next offset, we can skip the rest */
|
1998-02-06 21:18:01 +01:00
|
|
|
if (usecache && att[i]->attcacheoff != -1)
|
1997-09-07 07:04:48 +02:00
|
|
|
off = att[i]->attcacheoff;
|
|
|
|
else
|
|
|
|
{
|
1998-02-04 22:32:12 +01:00
|
|
|
switch (att[i]->attlen)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
off = (att[i]->attalign == 'd') ?
|
|
|
|
DOUBLEALIGN(off) : INTALIGN(off);
|
|
|
|
break;
|
|
|
|
case sizeof(char):
|
|
|
|
break;
|
|
|
|
case sizeof(short):
|
|
|
|
off = SHORTALIGN(off);
|
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
off = INTALIGN(off);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (att[i]->attlen < sizeof(int32))
|
|
|
|
elog(ERROR,
|
|
|
|
"nocachegetattr2: attribute %d has len %d",
|
|
|
|
i, att[i]->attlen);
|
|
|
|
if (att[i]->attalign == 'd')
|
|
|
|
off = DOUBLEALIGN(off);
|
|
|
|
else
|
|
|
|
off = LONGALIGN(off);
|
|
|
|
break;
|
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (usecache)
|
|
|
|
att[i]->attcacheoff = off;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (att[i]->attlen)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
case sizeof(char):
|
|
|
|
off++;
|
|
|
|
break;
|
1998-02-04 22:32:12 +01:00
|
|
|
case sizeof(short):
|
|
|
|
off += sizeof(short);
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
|
|
|
off += sizeof(int32);
|
|
|
|
break;
|
|
|
|
case -1:
|
1998-02-04 22:32:12 +01:00
|
|
|
Assert(!VARLENA_FIXED_SIZE(att[i]) ||
|
|
|
|
att[i]->atttypmod == VARSIZE(tp + off));
|
1997-09-08 04:41:22 +02:00
|
|
|
off += VARSIZE(tp + off);
|
1998-02-04 22:32:12 +01:00
|
|
|
if (!VARLENA_FIXED_SIZE(att[i]))
|
|
|
|
usecache = false;
|
1997-09-08 04:41:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
off += att[i]->attlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1998-02-04 22:32:12 +01:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
switch (att[attnum]->attlen)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
off = (att[attnum]->attalign == 'd') ?
|
|
|
|
DOUBLEALIGN(off) : INTALIGN(off);
|
|
|
|
break;
|
1997-09-07 07:04:48 +02:00
|
|
|
case sizeof(char):
|
|
|
|
break;
|
1997-09-08 04:41:22 +02:00
|
|
|
case sizeof(short):
|
|
|
|
off = SHORTALIGN(off);
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
case sizeof(int32):
|
1997-09-08 04:41:22 +02:00
|
|
|
off = INTALIGN(off);
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
1997-09-08 04:41:22 +02:00
|
|
|
if (att[attnum]->attlen < sizeof(int32))
|
1998-01-31 05:39:26 +01:00
|
|
|
elog(ERROR, "nocachegetattr3: attribute %d has len %d",
|
1997-09-08 04:41:22 +02:00
|
|
|
attnum, att[attnum]->attlen);
|
|
|
|
if (att[attnum]->attalign == 'd')
|
|
|
|
off = DOUBLEALIGN(off);
|
|
|
|
else
|
|
|
|
off = LONGALIGN(off);
|
1997-09-07 07:04:48 +02:00
|
|
|
break;
|
|
|
|
}
|
1998-02-04 22:32:12 +01:00
|
|
|
|
|
|
|
return (Datum) fetchatt(&(att[attnum]), tp + off);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_copytuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* returns a copy of an entire tuple
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
HeapTuple
|
|
|
|
heap_copytuple(HeapTuple tuple)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple newTuple;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!HeapTupleIsValid(tuple))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* XXX For now, just prevent an undetectable executor related error */
|
|
|
|
if (tuple->t_len > MAXTUPLEN)
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "palloctup: cannot handle length %d tuples",
|
1997-09-07 07:04:48 +02:00
|
|
|
tuple->t_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
newTuple = (HeapTuple) palloc(tuple->t_len);
|
|
|
|
memmove((char *) newTuple, (char *) tuple, (int) tuple->t_len);
|
|
|
|
return (newTuple);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#ifdef NOT_USED
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_deformtuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* the inverse of heap_formtuple (see below)
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
heap_deformtuple(HeapTuple tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
TupleDesc tdesc,
|
|
|
|
Datum values[],
|
|
|
|
char nulls[])
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
|
|
|
int natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
Assert(HeapTupleIsValid(tuple));
|
|
|
|
|
|
|
|
natts = tuple->t_natts;
|
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-09-12 06:09:08 +02:00
|
|
|
values[i] = heap_getattr(tuple,
|
|
|
|
i + 1,
|
|
|
|
tdesc,
|
|
|
|
&isnull);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (isnull)
|
|
|
|
nulls[i] = 'n';
|
|
|
|
else
|
|
|
|
nulls[i] = ' ';
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-08-19 23:40:56 +02:00
|
|
|
#endif
|
1996-07-09 08:22:35 +02:00
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_formtuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* constructs a tuple from the given value[] and null[] arrays
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* old comments
|
1997-09-07 07:04:48 +02:00
|
|
|
* Handles alignment by aligning 2 byte attributes on short boundries
|
|
|
|
* and 3 or 4 byte attributes on long word boundries on a vax; and
|
|
|
|
* aligning non-byte attributes on short boundries on a sun. Does
|
|
|
|
* not properly align fixed length arrays of 1 or 2 byte types (yet).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Null attributes are indicated by a 'n' in the appropriate byte
|
|
|
|
* of the null[]. Non-null attributes are indicated by a ' ' (space).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* Fix me. (Figure that must keep context if debug--allow give oid.)
|
|
|
|
* Assumes in order.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
HeapTuple
|
|
|
|
heap_formtuple(TupleDesc tupleDescriptor,
|
1997-09-07 07:04:48 +02:00
|
|
|
Datum value[],
|
|
|
|
char nulls[])
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *tp; /* tuple pointer */
|
|
|
|
HeapTuple tuple; /* return tuple */
|
|
|
|
int bitmaplen;
|
|
|
|
long len;
|
|
|
|
int hoff;
|
|
|
|
bool hasnull = false;
|
|
|
|
int i;
|
|
|
|
int numberOfAttributes = tupleDescriptor->natts;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
len = sizeof *tuple - sizeof tuple->t_bits;
|
|
|
|
|
|
|
|
for (i = 0; i < numberOfAttributes && !hasnull; i++)
|
|
|
|
{
|
|
|
|
if (nulls[i] != ' ')
|
|
|
|
hasnull = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numberOfAttributes > MaxHeapAttributeNumber)
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "heap_formtuple: numberOfAttributes of %d > %d",
|
1997-09-07 07:04:48 +02:00
|
|
|
numberOfAttributes, MaxHeapAttributeNumber);
|
|
|
|
|
|
|
|
if (hasnull)
|
|
|
|
{
|
|
|
|
bitmaplen = BITMAPLEN(numberOfAttributes);
|
|
|
|
len += bitmaplen;
|
|
|
|
}
|
|
|
|
|
|
|
|
hoff = len = DOUBLEALIGN(len); /* be conservative here */
|
|
|
|
|
|
|
|
len += ComputeDataSize(tupleDescriptor, value, nulls);
|
|
|
|
|
|
|
|
tp = (char *) palloc(len);
|
|
|
|
tuple = (HeapTuple) tp;
|
|
|
|
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(tp, 0, (int) len);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tuple->t_len = len;
|
|
|
|
tuple->t_natts = numberOfAttributes;
|
|
|
|
tuple->t_hoff = hoff;
|
|
|
|
|
|
|
|
DataFill((char *) tuple + tuple->t_hoff,
|
|
|
|
tupleDescriptor,
|
|
|
|
value,
|
|
|
|
nulls,
|
|
|
|
&tuple->t_infomask,
|
|
|
|
(hasnull ? tuple->t_bits : NULL));
|
|
|
|
|
1997-11-02 16:27:14 +01:00
|
|
|
tuple->t_infomask |= HEAP_XMAX_INVALID;
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
return (tuple);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* heap_modifytuple
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* forms a new tuple from an old tuple and a set of replacement values.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
HeapTuple
|
|
|
|
heap_modifytuple(HeapTuple tuple,
|
1997-09-07 07:04:48 +02:00
|
|
|
Buffer buffer,
|
|
|
|
Relation relation,
|
|
|
|
Datum replValue[],
|
|
|
|
char replNull[],
|
|
|
|
char repl[])
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int attoff;
|
|
|
|
int numberOfAttributes;
|
|
|
|
Datum *value;
|
|
|
|
char *nulls;
|
|
|
|
bool isNull;
|
|
|
|
HeapTuple newTuple;
|
|
|
|
int madecopy;
|
|
|
|
uint8 infomask;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* sanity checks
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Assert(HeapTupleIsValid(tuple));
|
|
|
|
Assert(BufferIsValid(buffer) || RelationIsValid(relation));
|
|
|
|
Assert(HeapTupleIsValid(tuple));
|
|
|
|
Assert(PointerIsValid(replValue));
|
|
|
|
Assert(PointerIsValid(replNull));
|
|
|
|
Assert(PointerIsValid(repl));
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if we're pointing to a disk page, then first
|
|
|
|
* make a copy of our tuple so that all the attributes
|
|
|
|
* are available. XXX this is inefficient -cim
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
madecopy = 0;
|
|
|
|
if (BufferIsValid(buffer) == true)
|
|
|
|
{
|
|
|
|
relation = (Relation) BufferGetRelation(buffer);
|
|
|
|
tuple = heap_copytuple(tuple);
|
|
|
|
madecopy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
numberOfAttributes = RelationGetRelationTupleForm(relation)->relnatts;
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* allocate and fill value[] and nulls[] arrays from either
|
|
|
|
* the tuple or the repl information, as appropriate.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
value = (Datum *) palloc(numberOfAttributes * sizeof *value);
|
|
|
|
nulls = (char *) palloc(numberOfAttributes * sizeof *nulls);
|
|
|
|
|
|
|
|
for (attoff = 0;
|
|
|
|
attoff < numberOfAttributes;
|
|
|
|
attoff += 1)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (repl[attoff] == ' ')
|
|
|
|
{
|
1997-09-12 06:09:08 +02:00
|
|
|
value[attoff] =
|
1997-09-07 07:04:48 +02:00
|
|
|
heap_getattr(tuple,
|
|
|
|
AttrOffsetGetAttrNumber(attoff),
|
|
|
|
RelationGetTupleDescriptor(relation),
|
|
|
|
&isNull);
|
|
|
|
nulls[attoff] = (isNull) ? 'n' : ' ';
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (repl[attoff] != 'r')
|
|
|
|
{
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "heap_modifytuple: repl is \\%3d", repl[attoff]);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ /* == 'r' */
|
|
|
|
value[attoff] = replValue[attoff];
|
|
|
|
nulls[attoff] = replNull[attoff];
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* create a new tuple from the values[] and nulls[] arrays
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
newTuple = heap_formtuple(RelationGetTupleDescriptor(relation),
|
|
|
|
value,
|
|
|
|
nulls);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* copy the header except for t_len, t_natts, t_hoff, t_bits, t_infomask
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
infomask = newTuple->t_infomask;
|
1997-11-02 16:27:14 +01:00
|
|
|
memmove((char *) &newTuple->t_oid, /* XXX */
|
|
|
|
(char *) &tuple->t_oid,
|
|
|
|
((char *) &tuple->t_hoff - (char *) &tuple->t_oid)); /* XXX */
|
1997-09-07 07:04:48 +02:00
|
|
|
newTuple->t_infomask = infomask;
|
|
|
|
newTuple->t_natts = numberOfAttributes; /* fix t_natts just in
|
|
|
|
* case */
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* if we made a copy of the tuple, then free it.
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
if (madecopy)
|
|
|
|
pfree(tuple);
|
|
|
|
|
|
|
|
return
|
|
|
|
newTuple;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* other misc functions
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
HeapTuple
|
|
|
|
heap_addheader(uint32 natts, /* max domain index */
|
1997-09-07 07:04:48 +02:00
|
|
|
int structlen, /* its length */
|
|
|
|
char *structure) /* pointer to the struct */
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
char *tp; /* tuple data pointer */
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
long len;
|
|
|
|
int hoff;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
AssertArg(natts > 0);
|
|
|
|
|
|
|
|
len = sizeof(HeapTupleData) - sizeof(tup->t_bits);
|
|
|
|
|
|
|
|
hoff = len = DOUBLEALIGN(len); /* be conservative */
|
|
|
|
len += structlen;
|
|
|
|
tp = (char *) palloc(len);
|
|
|
|
tup = (HeapTuple) tp;
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet((char *) tup, 0, len);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1997-11-02 16:27:14 +01:00
|
|
|
tup->t_len = len;
|
1997-09-07 07:04:48 +02:00
|
|
|
tp += tup->t_hoff = hoff;
|
|
|
|
tup->t_natts = natts;
|
|
|
|
tup->t_infomask = 0;
|
1997-11-02 16:27:14 +01:00
|
|
|
tup->t_infomask |= HEAP_XMAX_INVALID;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
memmove(tp, structure, structlen);
|
|
|
|
|
|
|
|
return (tup);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1998-01-31 05:39:26 +01:00
|
|
|
|