heaptuple.c 24.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*-------------------------------------------------------------------------
 *
 * heaptuple.c--
 *    This file contains heap tuple accessor and mutator routines, as well
 *    as a few various tuple utilities.
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
11
 *    $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.7 1996/10/20 08:31:26 scrappy Exp $
12 13 14 15 16 17 18 19 20 21
 *
 * NOTES
 *    The old interface functions have been converted to macros
 *    and moved to heapam.h
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

22 23 24 25 26
#include "storage/block.h"
#include "storage/off.h"
#include "storage/itemptr.h"
#include <time.h>
#include "utils/nabstime.h"
27
#include "access/htup.h"
28

29
#include "storage/buf.h"
30 31 32

#include "storage/itemid.h"
#include "storage/item.h"
33
#include "storage/bufpage.h"
34 35 36 37 38 39 40 41 42 43 44 45

#include "catalog/pg_attribute.h"
#include "access/attnum.h"
#include "nodes/pg_list.h"
#include "access/tupdesc.h"
#include "storage/fd.h"
#include "catalog/pg_am.h"
#include "catalog/pg_class.h"
#include "nodes/nodes.h"
#include "rewrite/prs2lock.h"
#include "access/skey.h"
#include "access/strat.h"
46 47 48 49
#include "utils/rel.h"

/* this is so the sparcstation debugger works */

50
#if !defined(NO_ASSERT_CHECKING) && defined(sparc)
51
#define register
52
#endif /* !NO_ASSERT_CHECKING && sparc */
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

/* ----------------------------------------------------------------
 *			misc support routines
 * ----------------------------------------------------------------
 */

/* ----------------
 *	ComputeDataSize
 * ----------------
 */
Size
ComputeDataSize(TupleDesc tupleDesc,
		Datum value[],
		char nulls[])
{
68
    uint32 data_length;
69 70 71 72
    int i;
    int numberOfAttributes = tupleDesc->natts;
    AttributeTupleForm *att = tupleDesc->attrs;
    
73
    for (data_length = 0, i = 0; i < numberOfAttributes; i++) {
74 75 76 77 78 79 80 81 82
	if (nulls[i] != ' ') continue;
	    
	switch (att[i]->attlen) {
	case -1:
	    /*
	     * This is the size of the disk representation and so
	     * must include the additional sizeof long.
	     */
	    if (att[i]->attalign == 'd') {
83
		data_length = DOUBLEALIGN(data_length)
84 85
		    + VARSIZE(DatumGetPointer(value[i]));
	    } else {
86
		data_length = INTALIGN(data_length)
87 88 89 90
		    + VARSIZE(DatumGetPointer(value[i]));
	    }
	    break;
	case sizeof(char):
91
	    data_length++;
92 93
	    break;
	case sizeof(short):
94
	    data_length = SHORTALIGN(data_length + sizeof(short));
95 96
	    break;
	case sizeof(int32):
97
	    data_length = INTALIGN(data_length + sizeof(int32));
98 99 100 101 102 103
	    break;
	default:
	    if (att[i]->attlen < sizeof(int32))
		elog(WARN, "ComputeDataSize: attribute %d has len %d",
		     i, att[i]->attlen);
	    if (att[i]->attalign == 'd')
104
		data_length = DOUBLEALIGN(data_length) + att[i]->attlen;
105
	    else
106
		data_length = LONGALIGN(data_length) + att[i]->attlen;
107 108 109 110
	    break;
	}
    }
    
111
    return data_length;
112 113 114 115 116 117 118 119 120 121 122 123
}

/* ----------------
 *	DataFill
 * ----------------
 */
void
DataFill(char *data,
	 TupleDesc tupleDesc,
	 Datum value[],
	 char nulls[],
	 char *infomask,
M
Fixes:  
Marc G. Fournier 已提交
124
	 bits8 *bit)
125
{
126 127 128
    bits8				*bitP = 0;
    int					bitmask = 0;
    uint32				data_length;
129 130
    int		i;
    int         numberOfAttributes = tupleDesc->natts;
131
    AttributeTupleForm	*att = tupleDesc->attrs;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    
    if (bit != NULL) {
	bitP = &bit[-1];
	bitmask = CSIGNBIT;
    }
    
    *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) {
	case -1:
	    *infomask |= HEAP_HASVARLENA;
	    if (att[i]->attalign=='d') {
		data = (char *) DOUBLEALIGN(data);
	    } else {
		data = (char *) INTALIGN(data);
	    }
166 167 168
	    		data_length = VARSIZE(DatumGetPointer(value[i]));
	    		memmove(data, DatumGetPointer(value[i]),data_length);
	    		data += data_length;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	    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):
	    data = (char *) INTALIGN(data);
	    * (int32 *) data = (att[i]->attbyval ?
				DatumGetInt32(value[i]) :
				*((int32 *) value[i]));
	    data += sizeof(int32);
	    break;
	default:
	    if (att[i]->attlen < sizeof(int32))
		elog(WARN, "DataFill: attribute %d has len %d",
		     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;
	    }
204
				break;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	}
    }
}

/* ----------------------------------------------------------------
 *			heap tuple interface
 * ----------------------------------------------------------------
 */

/* ----------------
 *	heap_attisnull	- returns 1 iff tuple attribute is not present
 * ----------------
 */
int
heap_attisnull(HeapTuple tup, int attnum)
{
    if (attnum > (int)tup->t_natts)
	return (1);
    
    if (HeapTupleNoNulls(tup)) return(0);
    
    if (attnum > 0) {
	return(att_isnull(attnum - 1, tup->t_bits));
    } else
	switch (attnum) {
	case SelfItemPointerAttributeNumber:
	case ObjectIdAttributeNumber:
	case MinTransactionIdAttributeNumber:
	case MinCommandIdAttributeNumber:
	case MaxTransactionIdAttributeNumber:
	case MaxCommandIdAttributeNumber:
	case ChainItemPointerAttributeNumber:
	case AnchorItemPointerAttributeNumber:
	case MinAbsoluteTimeAttributeNumber:
	case MaxAbsoluteTimeAttributeNumber:
	case VersionTypeAttributeNumber:
	    break;
	    
	case 0:
	    elog(WARN, "heap_attisnull: zero attnum disallowed");
	    
	default:
	    elog(WARN, "heap_attisnull: undefined negative attnum");
	}
    
    return (0);
}

/* ----------------------------------------------------------------
 *		 system attribute heap tuple support
 * ----------------------------------------------------------------
 */

/* ----------------
 *	heap_sysattrlen
 *
 *	This routine returns the length of a system attribute.
 * ----------------
 */
int
heap_sysattrlen(AttrNumber attno)
{
    HeapTupleData	*f = NULL;

    switch (attno) {
270 271 272 273 274 275 276 277 278 279 280
		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;
		case ChainItemPointerAttributeNumber:	return sizeof f->t_chain;
		case MinAbsoluteTimeAttributeNumber:	return sizeof f->t_tmin;
		case MaxAbsoluteTimeAttributeNumber:	return sizeof f->t_tmax;
		case VersionTypeAttributeNumber:		return sizeof f->t_vtype;

281 282
    case AnchorItemPointerAttributeNumber:
	elog(WARN, "heap_sysattrlen: field t_anchor does not exist!");
283 284
			return 0;

285
    default:
286 287
			elog(WARN, "sysattrlen: System attribute number %d unknown.", attno);
			return 0;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    }
}

/* ----------------
 *	heap_sysattrbyval
 *
 *	This routine returns the "by-value" property of a system attribute.
 * ----------------
 */
bool
heap_sysattrbyval(AttrNumber attno)
{
    bool		byval;
    
    switch (attno) {
    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;
    case ChainItemPointerAttributeNumber:
	byval = false;
	break;
    case AnchorItemPointerAttributeNumber:
	byval = false;
	break;
    case MinAbsoluteTimeAttributeNumber:
	byval = true;
	break;
    case MaxAbsoluteTimeAttributeNumber:
	byval = true;
	break;
    case VersionTypeAttributeNumber:
	byval = true;
	break;
    default:
	byval = true;
	elog(WARN, "sysattrbyval: System attribute number %d unknown.",
	     attno);
	break;
    }
    
    return byval;
}

/* ----------------
 *	heap_getsysattr
 * ----------------
 */
char *
heap_getsysattr(HeapTuple tup, Buffer b, int attnum)
{
    switch (attnum) {
    case SelfItemPointerAttributeNumber:
	return ((char *)&tup->t_ctid);
    case ObjectIdAttributeNumber:
	return ((char *) (long) tup->t_oid);
    case MinTransactionIdAttributeNumber:
	return ((char *) (long) tup->t_xmin);
    case MinCommandIdAttributeNumber:
	return ((char *) (long) tup->t_cmin);
    case MaxTransactionIdAttributeNumber:
	return ((char *) (long) tup->t_xmax);
    case MaxCommandIdAttributeNumber:
	return ((char *) (long) tup->t_cmax);
    case ChainItemPointerAttributeNumber:
	return ((char *) &tup->t_chain);
    case AnchorItemPointerAttributeNumber:
	elog(WARN, "heap_getsysattr: t_anchor does not exist!");
	break;
	
	/*
	 *  For tmin and tmax, we need to do some extra work.  These don't
	 *  get filled in until the vacuum cleaner runs (or we manage to flush
	 *  a page after setting the value correctly below).  If the vacuum
	 *  cleaner hasn't run yet, then the times stored in the tuple are
	 *  wrong, and we need to look up the commit time of the transaction.
	 *  We cache this value in the tuple to avoid doing the work more than
	 *  once.
	 */
	
    case MinAbsoluteTimeAttributeNumber:
	if (!AbsoluteTimeIsBackwardCompatiblyValid(tup->t_tmin) &&
	    TransactionIdDidCommit(tup->t_xmin))
	    tup->t_tmin = TransactionIdGetCommitTime(tup->t_xmin);
	return ((char *) (long) tup->t_tmin);
    case MaxAbsoluteTimeAttributeNumber:
	if (!AbsoluteTimeIsBackwardCompatiblyReal(tup->t_tmax)) {
	    if (TransactionIdDidCommit(tup->t_xmax))
		tup->t_tmax = TransactionIdGetCommitTime(tup->t_xmax);
	    else
		tup->t_tmax = CURRENT_ABSTIME;
	}
	return ((char *) (long) tup->t_tmax);
    case VersionTypeAttributeNumber:
	return ((char *) (long) tup->t_vtype);
    default:
	elog(WARN, "heap_getsysattr: undefined attnum %d", attnum);
    }
    return(NULL);
}

/* ----------------
 *	fastgetattr
 *
 *	This is a newer version of fastgetattr which attempts to be
 *	faster by caching attribute offsets in the attribute descriptor.
 *
 *	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.
 *
 *	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
 * ----------------
 */
char *
fastgetattr(HeapTuple tup,
	    int attnum,
	    TupleDesc tupleDesc,
	    bool *isnull)
{
    char *tp;		/* ptr to att in tuple */
427
    bits8  *bp = NULL;	        /* ptr to att in tuple */
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
    int slow;		/* do we have to walk nulls? */
    AttributeTupleForm *att = tupleDesc->attrs;
    
    /* ----------------
     *	sanity checks
     * ----------------
     */
    
    Assert(PointerIsValid(isnull));
    Assert(attnum > 0);
    
    /* ----------------
     *   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.
     * ----------------
     */
    
    *isnull =  false;
    
    if (HeapTupleNoNulls(tup)) {
	attnum--;
	if (att[attnum]->attcacheoff > 0) {
	    return (char *)
		fetchatt( &(att[attnum]),
			 (char *)tup + tup->t_hoff + att[attnum]->attcacheoff);
	} else if (attnum == 0) {
	    /*
	     * first attribute is always at position zero
	     */
	    return((char *) fetchatt(&(att[0]), (char *) tup + tup->t_hoff));
	}
	    
	tp = (char *) tup + tup->t_hoff;
	    
	slow = 0;
    } else {
	/*
	 * there's a null somewhere in the tuple
	 */

	bp = tup->t_bits;
	tp = (char *) tup + tup->t_hoff;
	slow = 0;
	attnum--;
	    
	/* ----------------
	 *	check to see if desired att is null
	 * ----------------
	 */
	
	if (att_isnull(attnum, bp)) {
	    *isnull = true;
	    return NULL;
	}

	/* ----------------
	 *      Now check to see if any preceeding bits are null...
	 * ----------------
	 */
	
	{
	    register int  i = 0; /* current offset in bp */
		
	    for (i = 0; i < attnum && !slow; i++) {
		if (att_isnull(i, bp)) slow = 1;
	    }
	}
    }
    
    /*
     * now check for any non-fixed length attrs before our attribute
     */
    if (!slow) {
	if (att[attnum]->attcacheoff > 0) {
	    return (char *)
		fetchatt(&(att[attnum]),
			 tp + att[attnum]->attcacheoff);
	} else if (attnum == 0) {
	    return (char *)
		fetchatt(&(att[0]), (char *) tup + tup->t_hoff);
	} else if (!HeapTupleAllFixed(tup)) {
	    register int j = 0;
		    
	    for (j = 0; j < attnum && !slow; j++)
		if (att[j]->attlen < 1) slow = 1;
	}
    }
    
    /*
     * 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.
     */
    if (!slow) {
	register int j = 1;
	register long off;
	    
	/*
	 * need to set cache for some atts
	 */
	    
	att[0]->attcacheoff = 0;
	
	while (att[j]->attcacheoff > 0) j++;
	
	off = att[j-1]->attcacheoff + att[j-1]->attlen;
	
	for (; j < attnum + 1; j++) {
	    switch(att[j]->attlen) {
	    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:
		if (att[j]->attlen < sizeof(int32)) {
		    elog(WARN,
			 "fastgetattr: attribute %d has len %d",
			 j, att[j]->attlen);
		}
		if (att[j]->attalign == 'd')
		    off = DOUBLEALIGN(off);
		else
		    off = LONGALIGN(off);
		break;
	    }
		    
	    att[j]->attcacheoff = off;
	    off += att[j]->attlen;
	}
	
	return
	    (char *)fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
    } else {
	register bool usecache = true;
	register int off = 0;
	register int i;
	
	/*
	 * 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;
		}
	    }
	    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(WARN,
			 "fastgetattr2: attribute %d has len %d",
			 i, att[i]->attlen);
		if (att[i]->attalign == 'd')
		    off = DOUBLEALIGN(off);
		else
		    off = LONGALIGN(off);
		break;
	    }
	    if (usecache && att[i]->attcacheoff > 0) {
		off = att[i]->attcacheoff;
		if (att[i]->attlen == -1) {
		    usecache = false;
		}
	    } else {
		if (usecache) att[i]->attcacheoff = off;
	    }
		    
	    switch(att[i]->attlen) {
	    case sizeof(char):
		off++;
		break;
	    case sizeof(int16):
		off += sizeof(int16);
		break;
	    case sizeof(int32):
		off += sizeof(int32);
		break;
	    case -1:
		usecache = false;
		off += VARSIZE(tp + off);
		break;
	    default:
		off += att[i]->attlen;
		break;
	    }
	}
	switch (att[attnum]->attlen) {
	case -1:
	    off = (att[attnum]->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[attnum]->attlen < sizeof(int32))
		elog(WARN, "fastgetattr3: attribute %d has len %d",
		     attnum, att[attnum]->attlen);
	    if (att[attnum]->attalign == 'd')
		off = DOUBLEALIGN(off);
	    else
		off = LONGALIGN(off);
	    break;
	}
	return((char *) fetchatt(&(att[attnum]), tp + off));
    }
}

/* ----------------
 *	heap_getattr
 *
 *	returns an attribute from a heap tuple.  uses 
 * ----------------
 */
char *
heap_getattr(HeapTuple tup,
	     Buffer b,
	     int attnum,
	     TupleDesc tupleDesc,
	     bool *isnull)
{
    bool	localIsNull;

    /* ----------------
     *	sanity checks
     * ----------------
     */
    Assert(tup != NULL);
    
    if (! PointerIsValid(isnull))
	isnull = &localIsNull;
    
    if (attnum > (int) tup->t_natts) {
	*isnull = true;
	return ((char *) NULL);
    }
    
    /* ----------------
     *	take care of user defined attributes
     * ----------------
     */
    if (attnum > 0) {
	char  *datum;
	datum = fastgetattr(tup, attnum, tupleDesc, isnull);
	
	return (datum);
    }
    
    /* ----------------
     *	take care of system attributes
     * ----------------
     */
    *isnull = false;
    return
	heap_getsysattr(tup, b, attnum);
}

/* ----------------
 *	heap_copytuple
 *
 *	returns a copy of an entire tuple
 * ----------------
 */
HeapTuple
heap_copytuple(HeapTuple tuple)
{
    HeapTuple	newTuple;

    if (! HeapTupleIsValid(tuple))
	return (NULL);
    
    /* XXX For now, just prevent an undetectable executor related error */
    if (tuple->t_len > MAXTUPLEN) {
	elog(WARN, "palloctup: cannot handle length %d tuples",
	     tuple->t_len);
    }
    
    newTuple = (HeapTuple) palloc(tuple->t_len);
    memmove((char *) newTuple, (char *) tuple, (int) tuple->t_len);
    return(newTuple);
}

/* ----------------
 *	heap_deformtuple
 *
 *	the inverse of heap_formtuple (see below)
 * ----------------
 */
void
heap_deformtuple(HeapTuple tuple,
		 TupleDesc tdesc,
		 Datum values[],
		 char nulls[])
{
    int i;
    int natts;
    
    Assert(HeapTupleIsValid(tuple));
    
    natts = tuple->t_natts;
    for (i = 0; i<natts; i++) {
	bool isnull;
	    
	values[i] = (Datum)heap_getattr(tuple,
					InvalidBuffer,
					i+1,
					tdesc,
					&isnull);
	if (isnull)
	    nulls[i] = 'n';
	else
	    nulls[i] = ' ';
    }
}

/* ----------------
 *	heap_formtuple 
 *
 *	constructs a tuple from the given value[] and null[] arrays
 *
 * old comments
 *	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).
 *
 *	Null attributes are indicated by a 'n' in the appropriate byte
 *	of the null[].  Non-null attributes are indicated by a ' ' (space).
 *
 *	Fix me.  (Figure that must keep context if debug--allow give oid.)
 *	Assumes in order.
 * ----------------
 */
HeapTuple
heap_formtuple(TupleDesc tupleDescriptor,
	       Datum value[],
	       char nulls[])
{
    char	*tp;	/* tuple pointer */
    HeapTuple	tuple;	/* return tuple */
    int		bitmaplen;
    long	len;
    int		hoff;
    bool	hasnull = false;
    int		i;
    int         numberOfAttributes = tupleDescriptor->natts;    

    len = sizeof *tuple - sizeof tuple->t_bits;
    
    for (i = 0; i < numberOfAttributes && !hasnull; i++) {
	if (nulls[i] != ' ') hasnull = true;
    }
    
    if (numberOfAttributes > MaxHeapAttributeNumber)
	elog(WARN, "heap_formtuple: numberOfAttributes of %d > %d",
	     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;

    memset(tp, 0, (int)len);
    
    tuple->t_len = 	len;
    tuple->t_natts = 	numberOfAttributes;
    tuple->t_hoff = hoff;
    tuple->t_tmin = INVALID_ABSTIME;
    tuple->t_tmax = CURRENT_ABSTIME;
    
    DataFill((char *)tuple + tuple->t_hoff,
	     tupleDescriptor,
	     value,
	     nulls,
             &tuple->t_infomask,
	     (hasnull ? tuple->t_bits : NULL));
    
    return (tuple);
}

/* ----------------
 *	heap_modifytuple
 *
 *	forms a new tuple from an old tuple and a set of replacement values.
 * ----------------
 */
HeapTuple
heap_modifytuple(HeapTuple tuple,
		 Buffer buffer,
		 Relation relation,
		 Datum replValue[],
		 char replNull[],
		 char repl[])
{
    int		attoff;
    int		numberOfAttributes;
    Datum	*value;
    char	*nulls;
    bool	isNull;
    HeapTuple	newTuple;
    int		madecopy;
    uint8	infomask;
    
    /* ----------------
     *	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] == ' ') {
	    char *attr;

	    attr =
		heap_getattr(tuple,
			     InvalidBuffer, 
			     AttrOffsetGetAttrNumber(attoff),
			     RelationGetTupleDescriptor(relation),
			     &isNull) ;
	    value[attoff] = PointerGetDatum(attr);
	    nulls[attoff] = (isNull) ? 'n' : ' ';
	    
	} else if (repl[attoff] != 'r') {
	    elog(WARN, "heap_modifytuple: repl is \\%3d", repl[attoff]);
	    
	} else { /* == 'r' */
	    value[attoff] = replValue[attoff];
	    nulls[attoff] =  replNull[attoff];
	}
    }
    
    /* ----------------
     *	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;
    memmove((char *) &newTuple->t_ctid,	/*XXX*/
	    (char *) &tuple->t_ctid,
	    ((char *) &tuple->t_hoff - (char *) &tuple->t_ctid)); /*XXX*/
    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;
}

/* ----------------------------------------------------------------
 *			other misc functions
 * ----------------------------------------------------------------
 */

HeapTuple
heap_addheader(uint32 natts,	/* max domain index */
	       int structlen,	/* its length */
	       char *structure)	/* pointer to the struct */
{
    register char	*tp;	/* tuple data pointer */
    HeapTuple		tup;
    long		len;
    int			hoff;
    
    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;
    memset((char*)tup, 0, len);
    
    tup->t_len = (short) len;			/* XXX */
    tp += tup->t_hoff = hoff;
    tup->t_natts = natts;
    tup->t_infomask = 0;
    
    memmove(tp, structure, structlen);
    
    return (tup);
}