Remove VARLENA_FIXED_SIZE hack, which is irreversibly broken now that

both MULTIBYTE and TOAST prevent char(n) from being truly fixed-size.
Simplify and speed up fastgetattr() and index_getattr() macros by
eliminating special cases for attnum=1.  It's just as fast to handle
the first attribute by presetting its attcacheoff to zero; so do that
instead when loading the tupledesc in relcache.c.
This commit is contained in:
Tom Lane 2000-11-30 18:38:47 +00:00
parent 59a9735fc8
commit 1f5cc8c78a
7 changed files with 123 additions and 152 deletions

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.66 2000/11/14 21:04:32 tgl Exp $ * $Header: /cvsroot/pgsql/src/backend/access/common/heaptuple.c,v 1.67 2000/11/30 18:38:45 tgl Exp $
* *
* NOTES * NOTES
* The old interface functions have been converted to macros * The old interface functions have been converted to macros
@ -300,11 +300,11 @@ nocachegetattr(HeapTuple tuple,
TupleDesc tupleDesc, TupleDesc tupleDesc,
bool *isnull) bool *isnull)
{ {
char *tp; /* ptr to att in tuple */
HeapTupleHeader tup = tuple->t_data; HeapTupleHeader tup = tuple->t_data;
bits8 *bp = tup->t_bits; /* ptr to att in tuple */
Form_pg_attribute *att = tupleDesc->attrs; Form_pg_attribute *att = tupleDesc->attrs;
int slow = 0; /* do we have to walk nulls? */ char *tp; /* ptr to att in tuple */
bits8 *bp = tup->t_bits; /* ptr to null bitmask in tuple */
bool slow = false; /* do we have to walk nulls? */
(void) isnull; /* not used */ (void) isnull; /* not used */
#ifdef IN_MACRO #ifdef IN_MACRO
@ -336,14 +336,6 @@ nocachegetattr(HeapTuple tuple,
fetchatt(&(att[attnum]), fetchatt(&(att[attnum]),
(char *) tup + tup->t_hoff + att[attnum]->attcacheoff); (char *) tup + tup->t_hoff + att[attnum]->attcacheoff);
} }
else if (attnum == 0)
{
/*
* first attribute is always at position zero
*/
return (Datum) fetchatt(&(att[0]), (char *) tup + tup->t_hoff);
}
#endif #endif
} }
else else
@ -378,7 +370,7 @@ nocachegetattr(HeapTuple tuple,
/* check for nulls "before" final bit of last byte */ /* check for nulls "before" final bit of last byte */
if ((~bp[byte]) & ((1 << finalbit) - 1)) if ((~bp[byte]) & ((1 << finalbit) - 1))
slow = 1; slow = true;
else else
{ {
/* check for nulls in any "earlier" bytes */ /* check for nulls in any "earlier" bytes */
@ -388,7 +380,7 @@ nocachegetattr(HeapTuple tuple,
{ {
if (bp[i] != 0xFF) if (bp[i] != 0xFF)
{ {
slow = 1; slow = true;
break; break;
} }
} }
@ -408,21 +400,19 @@ nocachegetattr(HeapTuple tuple,
return (Datum) fetchatt(&(att[attnum]), return (Datum) fetchatt(&(att[attnum]),
tp + att[attnum]->attcacheoff); tp + att[attnum]->attcacheoff);
} }
else if (attnum == 0)
return (Datum) fetchatt(&(att[0]), tp);
else if (!HeapTupleAllFixed(tuple)) else if (!HeapTupleAllFixed(tuple))
{ {
int j; int j;
/* /*
* In for(), we make this <= and not < because we want to test * In for(), we test <= and not < because we want to see
* if we can go past it in initializing offsets. * if we can go past it in initializing offsets.
*/ */
for (j = 0; j <= attnum; j++) for (j = 0; j <= attnum; j++)
{ {
if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j])) if (att[j]->attlen <= 0)
{ {
slow = 1; slow = true;
break; break;
} }
} }
@ -430,7 +420,7 @@ nocachegetattr(HeapTuple tuple,
} }
/* /*
* If slow is zero, and we got here, we know that we have a tuple with * If slow is false, and we got here, we know that we have a tuple with
* no nulls or varlenas before the target attribute. If possible, we * no nulls or varlenas before the target attribute. If possible, we
* also want to initialize the remainder of the attribute cached * also want to initialize the remainder of the attribute cached
* offset values. * offset values.
@ -446,21 +436,17 @@ nocachegetattr(HeapTuple tuple,
att[0]->attcacheoff = 0; att[0]->attcacheoff = 0;
while (att[j]->attcacheoff > 0) while (j < attnum && att[j]->attcacheoff > 0)
j++; j++;
if (!VARLENA_FIXED_SIZE(att[j - 1])) off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
else
off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod;
for (; j <= attnum || for (; j <= attnum ||
/* Can we compute more? We will probably need them */ /* Can we compute more? We will probably need them */
(j < tup->t_natts && (j < tup->t_natts &&
att[j]->attcacheoff == -1 && att[j]->attcacheoff == -1 &&
(HeapTupleNoNulls(tuple) || !att_isnull(j, bp)) && (HeapTupleNoNulls(tuple) || !att_isnull(j, bp)) &&
(HeapTupleAllFixed(tuple) || (HeapTupleAllFixed(tuple) || att[j]->attlen > 0)); j++)
att[j]->attlen > 0 || VARLENA_FIXED_SIZE(att[j]))); j++)
{ {
/* /*
@ -516,8 +502,7 @@ nocachegetattr(HeapTuple tuple,
off = att_addlength(off, att[i]->attlen, tp + off); off = att_addlength(off, att[i]->attlen, tp + off);
if (usecache && if (usecache && att[i]->attlen == -1)
att[i]->attlen == -1 && !VARLENA_FIXED_SIZE(att[i]))
usecache = false; usecache = false;
} }

View File

@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/common/indextuple.c,v 1.46 2000/11/16 05:50:57 momjian Exp $ * $Header: /cvsroot/pgsql/src/backend/access/common/indextuple.c,v 1.47 2000/11/30 18:38:45 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -160,13 +160,13 @@ index_formtuple(TupleDesc tupleDescriptor,
* *
* This caches attribute offsets in the attribute descriptor. * This caches attribute offsets in the attribute descriptor.
* *
* an alternate way to speed things up would be to cache offsets * An alternate way to speed things up would be to cache offsets
* with the tuple, but that seems more difficult unless you take * with the tuple, but that seems more difficult unless you take
* the storage hit of actually putting those offsets into the * the storage hit of actually putting those offsets into the
* tuple you send to disk. Yuck. * tuple you send to disk. Yuck.
* *
* This scheme will be slightly slower than that, but should * This scheme will be slightly slower than that, but should
* preform well for queries which hit large #'s of tuples. After * perform well for queries which hit large #'s of tuples. After
* you cache the offsets once, examining all the other tuples using * you cache the offsets once, examining all the other tuples using
* the same attribute descriptor will go much quicker. -cim 5/4/91 * the same attribute descriptor will go much quicker. -cim 5/4/91
* ---------------- * ----------------
@ -177,13 +177,13 @@ nocache_index_getattr(IndexTuple tup,
TupleDesc tupleDesc, TupleDesc tupleDesc,
bool *isnull) bool *isnull)
{ {
char *tp; /* ptr to att in tuple */
char *bp = NULL; /* ptr to att in tuple */
int slow; /* do we have to walk nulls? */
int data_off; /* tuple data offset */
Form_pg_attribute *att = tupleDesc->attrs; Form_pg_attribute *att = tupleDesc->attrs;
char *tp; /* ptr to att in tuple */
bits8 *bp = NULL; /* ptr to null bitmask in tuple */
bool slow = false; /* do we have to walk nulls? */
int data_off; /* tuple data offset */
(void) isnull; (void) isnull; /* not used */
/* ---------------- /* ----------------
* sanity checks * sanity checks
* ---------------- * ----------------
@ -209,17 +209,12 @@ nocache_index_getattr(IndexTuple tup,
data_off = IndexTupleHasMinHeader(tup) ? sizeof *tup : data_off = IndexTupleHasMinHeader(tup) ? sizeof *tup :
IndexInfoFindDataOffset(tup->t_info); IndexInfoFindDataOffset(tup->t_info);
attnum--;
if (IndexTupleNoNulls(tup)) if (IndexTupleNoNulls(tup))
{ {
attnum--;
#ifdef IN_MACRO #ifdef IN_MACRO
/* This is handled in the macro */ /* This is handled in the macro */
/* first attribute is always at position zero */
if (attnum == 1)
return (Datum) fetchatt(&(att[0]), (char *) tup + data_off);
if (att[attnum]->attcacheoff != -1) if (att[attnum]->attcacheoff != -1)
{ {
return (Datum) fetchatt(&(att[attnum]), return (Datum) fetchatt(&(att[attnum]),
@ -227,20 +222,13 @@ nocache_index_getattr(IndexTuple tup,
att[attnum]->attcacheoff); att[attnum]->attcacheoff);
} }
#endif #endif
slow = 0;
} }
else else
{ /* there's a null somewhere in the tuple */ { /* there's a null somewhere in the tuple */
slow = 0;
/* ---------------- /* ----------------
* check to see if desired att is null * check to see if desired att is null
* ---------------- * ----------------
*/ */
attnum--;
bp = (char *) tup + sizeof(*tup); /* "knows" t_bits are bp = (char *) tup + sizeof(*tup); /* "knows" t_bits are
* here! */ * here! */
#ifdef IN_MACRO #ifdef IN_MACRO
@ -254,34 +242,28 @@ nocache_index_getattr(IndexTuple tup,
#endif #endif
/* ---------------- /* ----------------
* Now check to see if any preceeding bits are null... * Now check to see if any preceding bits are null...
* ---------------- * ----------------
*/ */
{ {
int i = 0; /* current offset in bp */ int byte = attnum >> 3;
int mask; /* bit in byte we're looking at */ int finalbit = attnum & 0x07;
char n; /* current byte in bp */
int byte,
finalbit;
byte = attnum >> 3; /* check for nulls "before" final bit of last byte */
finalbit = attnum & 0x07; if ((~bp[byte]) & ((1 << finalbit) - 1))
slow = true;
for (; i <= byte && !slow; i++) else
{ {
n = bp[i]; /* check for nulls in any "earlier" bytes */
if (i < byte) int i;
for (i = 0; i < byte; i++)
{ {
/* check for nulls in any "earlier" bytes */ if (bp[i] != 0xFF)
if ((~n) != 0) {
slow = 1; slow = true;
} break;
else }
{
/* check for nulls "before" final bit of last byte */
mask = (1 << finalbit) - 1;
if ((~n) & mask)
slow = 1;
} }
} }
} }
@ -298,24 +280,25 @@ nocache_index_getattr(IndexTuple tup,
return (Datum) fetchatt(&(att[attnum]), return (Datum) fetchatt(&(att[attnum]),
tp + att[attnum]->attcacheoff); tp + att[attnum]->attcacheoff);
} }
else if (attnum == 0)
return (Datum) fetchatt(&(att[0]), (char *) tp);
else if (!IndexTupleAllFixed(tup)) else if (!IndexTupleAllFixed(tup))
{ {
int j = 0; int j;
for (j = 0; j < attnum && !slow; j++) for (j = 0; j < attnum; j++)
if (att[j]->attlen < 1 && !VARLENA_FIXED_SIZE(att[j])) if (att[j]->attlen <= 0)
slow = 1; {
slow = true;
break;
}
} }
} }
/* /*
* if slow is zero, and we got here, we know that we have a tuple with * If slow is false, and we got here, we know that we have a tuple with
* no nulls. We also know that we have to initialize the remainder of * no nulls or varlenas before the target attribute. If possible, we
* the attribute cached offset values. * also want to initialize the remainder of the attribute cached
* offset values.
*/ */
if (!slow) if (!slow)
{ {
int j = 1; int j = 1;
@ -327,15 +310,12 @@ nocache_index_getattr(IndexTuple tup,
att[0]->attcacheoff = 0; att[0]->attcacheoff = 0;
while (att[j]->attcacheoff != -1) while (j < attnum && att[j]->attcacheoff > 0)
j++; j++;
if (!VARLENA_FIXED_SIZE(att[j - 1])) off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
off = att[j - 1]->attcacheoff + att[j - 1]->attlen;
else
off = att[j - 1]->attcacheoff + att[j - 1]->atttypmod;
for (; j < attnum + 1; j++) for (; j <= attnum; j++)
{ {
/* /*
@ -347,14 +327,7 @@ nocache_index_getattr(IndexTuple tup,
att[j]->attcacheoff = off; att[j]->attcacheoff = off;
/* The only varlena/-1 length value to get here is this */ off += att[j]->attlen;
if (!VARLENA_FIXED_SIZE(att[j]))
off += att[j]->attlen;
else
{
Assert(att[j]->atttypmod == VARSIZE(tp + off));
off += att[j]->atttypmod;
}
} }
return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff); return (Datum) fetchatt(&(att[attnum]), tp + att[attnum]->attcacheoff);
@ -391,27 +364,14 @@ nocache_index_getattr(IndexTuple tup,
att[i]->attcacheoff = off; att[i]->attcacheoff = off;
} }
switch (att[i]->attlen) if (att[i]->attlen == -1)
{ {
case sizeof(char): off += VARSIZE(tp + off);
off++; usecache = false;
break; }
case sizeof(short): else
off += sizeof(short); {
break; off += att[i]->attlen;
case sizeof(int32):
off += sizeof(int32);
break;
case -1:
Assert(!VARLENA_FIXED_SIZE(att[i]) ||
att[i]->atttypmod == VARSIZE(tp + off));
off += VARSIZE(tp + off);
if (!VARLENA_FIXED_SIZE(att[i]))
usecache = false;
break;
default:
off += att[i]->attlen;
break;
} }
} }

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.97 2000/11/30 08:46:20 vadim Exp $ * $Header: /cvsroot/pgsql/src/backend/access/heap/heapam.c,v 1.98 2000/11/30 18:38:45 tgl Exp $
* *
* *
* INTERFACE ROUTINES * INTERFACE ROUTINES
@ -535,18 +535,11 @@ fastgetattr(HeapTuple tup, int attnum, TupleDesc tupleDesc,
((isnull) ? (*(isnull) = false) : (dummyret) NULL), ((isnull) ? (*(isnull) = false) : (dummyret) NULL),
HeapTupleNoNulls(tup) ? HeapTupleNoNulls(tup) ?
( (
((tupleDesc)->attrs[(attnum) - 1]->attcacheoff != -1 || (tupleDesc)->attrs[(attnum) - 1]->attcacheoff >= 0 ?
(attnum) == 1) ?
( (
(Datum) fetchatt(&((tupleDesc)->attrs[(attnum) - 1]), (Datum) fetchatt(&((tupleDesc)->attrs[(attnum) - 1]),
(char *) (tup)->t_data + (tup)->t_data->t_hoff + (char *) (tup)->t_data + (tup)->t_data->t_hoff +
( (tupleDesc)->attrs[(attnum) - 1]->attcacheoff)
((attnum) != 1) ?
(tupleDesc)->attrs[(attnum) - 1]->attcacheoff
:
0
)
)
) )
: :
nocachegetattr((tup), (attnum), (tupleDesc), (isnull)) nocachegetattr((tup), (attnum), (tupleDesc), (isnull))

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.117 2000/11/30 08:46:24 vadim Exp $ * $Header: /cvsroot/pgsql/src/backend/utils/cache/relcache.c,v 1.118 2000/11/30 18:38:46 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -630,6 +630,32 @@ build_tupdesc_seq(RelationBuildDescInfo buildinfo,
heap_endscan(pg_attribute_scan); heap_endscan(pg_attribute_scan);
heap_close(pg_attribute_desc, AccessShareLock); heap_close(pg_attribute_desc, AccessShareLock);
/* ----------------
* The attcacheoff values we read from pg_attribute should all be -1
* ("unknown"). Verify this if assert checking is on. They will be
* computed when and if needed during tuple access.
* ----------------
*/
#ifdef USE_ASSERT_CHECKING
{
int i;
for (i = 0; i < relation->rd_rel->relnatts; i++)
{
Assert(relation->rd_att->attrs[i]->attcacheoff == -1);
}
}
#endif
/* ----------------
* However, we can easily set the attcacheoff value for the first
* attribute: it must be zero. This eliminates the need for special
* cases for attnum=1 that used to exist in fastgetattr() and
* index_getattr().
* ----------------
*/
relation->rd_att->attrs[0]->attcacheoff = 0;
SetConstrOfRelation(relation, constr, ndef, attrdef); SetConstrOfRelation(relation, constr, ndef, attrdef);
} }
@ -715,6 +741,28 @@ build_tupdesc_ind(RelationBuildDescInfo buildinfo,
heap_close(attrel, AccessShareLock); heap_close(attrel, AccessShareLock);
/* ----------------
* The attcacheoff values we read from pg_attribute should all be -1
* ("unknown"). Verify this if assert checking is on. They will be
* computed when and if needed during tuple access.
* ----------------
*/
#ifdef USE_ASSERT_CHECKING
for (i = 0; i < relation->rd_rel->relnatts; i++)
{
Assert(relation->rd_att->attrs[i]->attcacheoff == -1);
}
#endif
/* ----------------
* However, we can easily set the attcacheoff value for the first
* attribute: it must be zero. This eliminates the need for special
* cases for attnum=1 that used to exist in fastgetattr() and
* index_getattr().
* ----------------
*/
relation->rd_att->attrs[0]->attcacheoff = 0;
SetConstrOfRelation(relation, constr, ndef, attrdef); SetConstrOfRelation(relation, constr, ndef, attrdef);
} }

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: heapam.h,v 1.58 2000/11/21 21:16:05 petere Exp $ * $Id: heapam.h,v 1.59 2000/11/30 18:38:46 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -107,18 +107,11 @@ extern Datum nocachegetattr(HeapTuple tup, int attnum,
((isnull) ? (*(isnull) = false) : (dummyret)NULL), \ ((isnull) ? (*(isnull) = false) : (dummyret)NULL), \
HeapTupleNoNulls(tup) ? \ HeapTupleNoNulls(tup) ? \
( \ ( \
((tupleDesc)->attrs[(attnum)-1]->attcacheoff != -1 || \ (tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ? \
(attnum) == 1) ? \
( \ ( \
(Datum)fetchatt(&((tupleDesc)->attrs[(attnum)-1]), \ (Datum) fetchatt(&((tupleDesc)->attrs[(attnum)-1]), \
(char *) (tup)->t_data + (tup)->t_data->t_hoff + \ (char *) (tup)->t_data + (tup)->t_data->t_hoff + \
( \ (tupleDesc)->attrs[(attnum)-1]->attcacheoff) \
((attnum) != 1) ? \
(tupleDesc)->attrs[(attnum)-1]->attcacheoff \
: \
0 \
) \
) \
) \ ) \
: \ : \
nocachegetattr((tup), (attnum), (tupleDesc), (isnull)) \ nocachegetattr((tup), (attnum), (tupleDesc), (isnull)) \

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: itup.h,v 1.25 2000/07/14 22:17:53 tgl Exp $ * $Id: itup.h,v 1.26 2000/11/30 18:38:46 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -109,24 +109,17 @@ typedef RetrieveIndexResultData *RetrieveIndexResult;
*(isnull) = false, \ *(isnull) = false, \
IndexTupleNoNulls(tup) ? \ IndexTupleNoNulls(tup) ? \
( \ ( \
((tupleDesc)->attrs[(attnum)-1]->attcacheoff != -1 || \ (tupleDesc)->attrs[(attnum)-1]->attcacheoff >= 0 ? \
(attnum) == 1) ? \
( \ ( \
(Datum)fetchatt(&((tupleDesc)->attrs[(attnum)-1]), \ (Datum) fetchatt(&((tupleDesc)->attrs[(attnum)-1]), \
(char *) (tup) + \ (char *) (tup) + \
( \ ( \
IndexTupleHasMinHeader(tup) ? \ IndexTupleHasMinHeader(tup) ? \
sizeof (*(tup)) \ sizeof (*(tup)) \
: \ : \
IndexInfoFindDataOffset((tup)->t_info) \ IndexInfoFindDataOffset((tup)->t_info) \
) + \
( \
((attnum) != 1) ? \
(tupleDesc)->attrs[(attnum)-1]->attcacheoff \
: \
0 \
) \
) \ ) \
+ (tupleDesc)->attrs[(attnum)-1]->attcacheoff) \
) \ ) \
: \ : \
nocache_index_getattr((tup), (attnum), (tupleDesc), (isnull)) \ nocache_index_getattr((tup), (attnum), (tupleDesc), (isnull)) \

View File

@ -8,7 +8,7 @@
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $Id: pg_type.h,v 1.97 2000/08/21 04:48:52 tgl Exp $ * $Id: pg_type.h,v 1.98 2000/11/30 18:38:47 tgl Exp $
* *
* NOTES * NOTES
* the genbki.sh script reads this file and generates .bki * the genbki.sh script reads this file and generates .bki
@ -415,7 +415,6 @@ DATA(insert OID = 1700 ( numeric PGUID -1 -1 f b t \054 0 0 numeric_in nume
DESCR("numeric(precision, decimal), arbitrary precision number"); DESCR("numeric(precision, decimal), arbitrary precision number");
#define NUMERICOID 1700 #define NUMERICOID 1700
#define VARLENA_FIXED_SIZE(attr) ((attr)->atttypid == BPCHAROID && (attr)->atttypmod > 0)
/* /*
* prototypes for functions in pg_type.c * prototypes for functions in pg_type.c