88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.45 1998/10/08 18:29:10 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.46 1998/11/27 19:51:27 vadim Exp $
1212 *
1313 * NOTES
1414 * The old interface functions have been converted to macros
3636/* Used by heap_getattr() macro, for speed */
3737long heap_sysoffset []= {
3838/* Only the first one is pass-by-ref, and is handled specially in the macro */
39- offsetof(HeapTupleData ,t_ctid ),
40- offsetof(HeapTupleData ,t_oid ),
41- offsetof(HeapTupleData ,t_xmin ),
42- offsetof(HeapTupleData ,t_cmin ),
43- offsetof(HeapTupleData ,t_xmax ),
44- offsetof(HeapTupleData ,t_cmax )
39+ offsetof(HeapTupleHeaderData ,t_ctid ),
40+ offsetof(HeapTupleHeaderData ,t_oid ),
41+ offsetof(HeapTupleHeaderData ,t_xmin ),
42+ offsetof(HeapTupleHeaderData ,t_cmin ),
43+ offsetof(HeapTupleHeaderData ,t_xmax ),
44+ offsetof(HeapTupleHeaderData ,t_cmax )
4545};
4646
4747/* ----------------------------------------------------------------
@@ -167,14 +167,14 @@ DataFill(char *data,
167167int
168168heap_attisnull (HeapTuple tup ,int attnum )
169169{
170- if (attnum > (int )tup -> t_natts )
170+ if (attnum > (int )tup -> t_data -> t_natts )
171171return 1 ;
172172
173173if (HeapTupleNoNulls (tup ))
174174return 0 ;
175175
176176if (attnum > 0 )
177- return att_isnull (attnum - 1 ,tup -> t_bits );
177+ return att_isnull (attnum - 1 ,tup -> t_data -> t_bits );
178178else
179179switch (attnum )
180180{
@@ -210,7 +210,7 @@ heap_attisnull(HeapTuple tup, int attnum)
210210int
211211heap_sysattrlen (AttrNumber attno )
212212{
213- HeapTupleData * f = NULL ;
213+ HeapTupleHeader f = NULL ;
214214
215215switch (attno )
216216{
@@ -323,15 +323,16 @@ heap_getsysattr(HeapTuple tup, Buffer b, int attnum)
323323 * ----------------
324324 */
325325Datum
326- nocachegetattr (HeapTuple tup ,
326+ nocachegetattr (HeapTuple tuple ,
327327int attnum ,
328328TupleDesc tupleDesc ,
329329bool * isnull )
330330{
331- char * tp ;/* ptr to att in tuple */
332- bits8 * bp = tup -> t_bits ;/* ptr to att in tuple */
333- int slow ;/* do we have to walk nulls? */
334- Form_pg_attribute * att = tupleDesc -> attrs ;
331+ char * tp ;/* ptr to att in tuple */
332+ HeapTupleHeader tup = tuple -> t_data ;
333+ bits8 * bp = tup -> t_bits ;/* ptr to att in tuple */
334+ int slow ;/* do we have to walk nulls? */
335+ Form_pg_attribute * att = tupleDesc -> attrs ;
335336
336337
337338#if IN_MACRO
@@ -351,7 +352,7 @@ nocachegetattr(HeapTuple tup,
351352 * ----------------
352353 */
353354
354- if (HeapTupleNoNulls (tup ))
355+ if (HeapTupleNoNulls (tuple ))
355356{
356357attnum -- ;
357358
@@ -449,7 +450,7 @@ nocachegetattr(HeapTuple tup,
449450}
450451else if (attnum == 0 )
451452return (Datum )fetchatt (& (att [0 ]), (char * )tp );
452- else if (!HeapTupleAllFixed (tup ))
453+ else if (!HeapTupleAllFixed (tuple ))
453454{
454455int j = 0 ;
455456
@@ -491,8 +492,8 @@ nocachegetattr(HeapTuple tup,
491492/* Can we compute more? We will probably need them */
492493 (j < tup -> t_natts &&
493494att [j ]-> attcacheoff == -1 &&
494- (HeapTupleNoNulls (tup )|| !att_isnull (j ,bp ))&&
495- (HeapTupleAllFixed (tup )||
495+ (HeapTupleNoNulls (tuple )|| !att_isnull (j ,bp ))&&
496+ (HeapTupleAllFixed (tuple )||
496497att [j ]-> attlen > 0 || VARLENA_FIXED_SIZE (att [j ])));j ++ )
497498{
498499
@@ -527,7 +528,7 @@ nocachegetattr(HeapTuple tup,
527528
528529for (i = 0 ;i < attnum ;i ++ )
529530{
530- if (!HeapTupleNoNulls (tup ))
531+ if (!HeapTupleNoNulls (tuple ))
531532{
532533if (att_isnull (i ,bp ))
533534{
@@ -570,14 +571,41 @@ heap_copytuple(HeapTuple tuple)
570571{
571572HeapTuple newTuple ;
572573
573- if (!HeapTupleIsValid (tuple ))
574+ if (!HeapTupleIsValid (tuple )|| tuple -> t_data == NULL )
574575return NULL ;
575576
576- newTuple = (HeapTuple )palloc (tuple -> t_len );
577- memmove ((char * )newTuple , (char * )tuple , (int )tuple -> t_len );
577+ newTuple = (HeapTuple )palloc (HEAPTUPLESIZE + tuple -> t_len );
578+ newTuple -> t_len = tuple -> t_len ;
579+ newTuple -> t_self = tuple -> t_self ;
580+ newTuple -> t_data = (HeapTupleHeader ) ((char * )newTuple + HEAPTUPLESIZE );
581+ memmove ((char * )newTuple -> t_data ,
582+ (char * )tuple -> t_data , (int )tuple -> t_len );
578583return newTuple ;
579584}
580585
586+ /* ----------------
587+ *heap_copytuple_with_tuple
588+ *
589+ *returns a copy of an tuple->t_data
590+ * ----------------
591+ */
592+ void
593+ heap_copytuple_with_tuple (HeapTuple src ,HeapTuple dest )
594+ {
595+ if (!HeapTupleIsValid (src )|| src -> t_data == NULL )
596+ {
597+ dest -> t_data = NULL ;
598+ return ;
599+ }
600+
601+ dest -> t_len = src -> t_len ;
602+ dest -> t_self = src -> t_self ;
603+ dest -> t_data = (HeapTupleHeader )palloc (src -> t_len );
604+ memmove ((char * )dest -> t_data ,
605+ (char * )src -> t_data , (int )src -> t_len );
606+ return ;
607+ }
608+
581609#ifdef NOT_USED
582610/* ----------------
583611 *heap_deformtuple
@@ -637,16 +665,16 @@ heap_formtuple(TupleDesc tupleDescriptor,
637665Datum * value ,
638666char * nulls )
639667{
640- char * tp ;/*tuple pointer */
641- HeapTuple tuple ;/*return tuple */
642- int bitmaplen ;
643- long len ;
644- int hoff ;
645- bool hasnull = false;
646- int i ;
647- int numberOfAttributes = tupleDescriptor -> natts ;
668+ HeapTuple tuple ;/*return tuple */
669+ HeapTupleHeader td ;/* tuple data */
670+ int bitmaplen ;
671+ long len ;
672+ int hoff ;
673+ bool hasnull = false;
674+ int i ;
675+ int numberOfAttributes = tupleDescriptor -> natts ;
648676
649- len = offsetof(HeapTupleData ,t_bits );
677+ len = offsetof(HeapTupleHeaderData ,t_bits );
650678
651679for (i = 0 ;i < numberOfAttributes && !hasnull ;i ++ )
652680{
@@ -668,23 +696,24 @@ heap_formtuple(TupleDesc tupleDescriptor,
668696
669697len += ComputeDataSize (tupleDescriptor ,value ,nulls );
670698
671- tp = (char * )palloc (len );
672- tuple = (HeapTuple ) tp ;
699+ tuple = (HeapTuple )palloc (HEAPTUPLESIZE + len );
700+ td = tuple -> t_data = (HeapTupleHeader ) (( char * ) tuple + HEAPTUPLESIZE ); ;
673701
674- MemSet (tp ,0 , (int )len );
702+ MemSet (( char * ) td ,0 , (int )len );
675703
676704tuple -> t_len = len ;
677- tuple -> t_natts = numberOfAttributes ;
678- tuple -> t_hoff = hoff ;
705+ ItemPointerSetInvalid (& (tuple -> t_self ));
706+ td -> t_natts = numberOfAttributes ;
707+ td -> t_hoff = hoff ;
679708
680- DataFill ((char * )tuple + tuple -> t_hoff ,
709+ DataFill ((char * )td + td -> t_hoff ,
681710tupleDescriptor ,
682711value ,
683712nulls ,
684- & tuple -> t_infomask ,
685- (hasnull ?tuple -> t_bits :NULL ));
713+ & td -> t_infomask ,
714+ (hasnull ?td -> t_bits :NULL ));
686715
687- tuple -> t_infomask |=HEAP_XMAX_INVALID ;
716+ td -> t_infomask |=HEAP_XMAX_INVALID ;
688717
689718return tuple ;
690719}
@@ -767,13 +796,15 @@ heap_modifytuple(HeapTuple tuple,
767796 *copy the header except for t_len, t_natts, t_hoff, t_bits, t_infomask
768797 * ----------------
769798 */
770- infomask = newTuple -> t_infomask ;
771- memmove ((char * )& newTuple -> t_oid ,/* XXX */
772- (char * )& tuple -> t_oid ,
773- ((char * )& tuple -> t_hoff - (char * )& tuple -> t_oid ));/* XXX */
774- newTuple -> t_infomask = infomask ;
775- newTuple -> t_natts = numberOfAttributes ;/* fix t_natts just in
776- * case */
799+ infomask = newTuple -> t_data -> t_infomask ;
800+ memmove ((char * )& newTuple -> t_data -> t_oid ,/* XXX */
801+ (char * )& tuple -> t_data -> t_oid ,
802+ ((char * )& tuple -> t_data -> t_hoff -
803+ (char * )& tuple -> t_data -> t_oid ));/* XXX */
804+ newTuple -> t_data -> t_infomask = infomask ;
805+ newTuple -> t_data -> t_natts = numberOfAttributes ;
806+ newTuple -> t_self = tuple -> t_self ;
807+
777808return newTuple ;
778809}
779810
@@ -787,28 +818,30 @@ heap_addheader(uint32 natts,/* max domain index */
787818int structlen ,/* its length */
788819char * structure )/* pointer to the struct */
789820{
790- char * tp ; /* tuple data pointer */
791- HeapTuple tup ;
792- long len ;
793- int hoff ;
821+ HeapTuple tuple ;
822+ HeapTupleHeader td ; /* tuple data */
823+ long len ;
824+ int hoff ;
794825
795826AssertArg (natts > 0 );
796827
797- len = offsetof(HeapTupleData ,t_bits );
828+ len = offsetof(HeapTupleHeaderData ,t_bits );
798829
799830hoff = len = DOUBLEALIGN (len );/* be conservative */
800831len += structlen ;
801- tp = (char * )palloc (len );
802- tup = (HeapTuple )tp ;
803- MemSet ((char * )tup ,0 ,len );
832+ tuple = (HeapTuple )palloc (HEAPTUPLESIZE + len );
833+ td = tuple -> t_data = (HeapTupleHeader ) ((char * )tuple + HEAPTUPLESIZE );
834+
835+ MemSet ((char * )td ,0 , (int )len );
804836
805- tup -> t_len = len ;
806- tp += tup -> t_hoff = hoff ;
807- tup -> t_natts = natts ;
808- tup -> t_infomask = 0 ;
809- tup -> t_infomask |=HEAP_XMAX_INVALID ;
837+ tuple -> t_len = len ;
838+ ItemPointerSetInvalid (& (tuple -> t_self ));
839+ td -> t_hoff = hoff ;
840+ td -> t_natts = natts ;
841+ td -> t_infomask = 0 ;
842+ td -> t_infomask |=HEAP_XMAX_INVALID ;
810843
811- memmove (tp ,structure ,structlen );
844+ memmove (( char * ) td + hoff ,structure ,structlen );
812845
813- return tup ;
846+ return tuple ;
814847}