1996-08-27 23:50:29 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* itup.h
|
1996-08-27 23:50:29 +02:00
|
|
|
* POSTGRES index tuple definitions.
|
|
|
|
*
|
|
|
|
*
|
2022-01-08 01:04:57 +01:00
|
|
|
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/include/access/itup.h
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef ITUP_H
|
|
|
|
#define ITUP_H
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "access/tupdesc.h"
|
1999-07-16 19:07:40 +02:00
|
|
|
#include "access/tupmacs.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "storage/bufpage.h"
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "storage/itemptr.h"
|
1996-08-27 23:50:29 +02:00
|
|
|
|
2005-03-27 20:38:27 +02:00
|
|
|
/*
|
|
|
|
* Index tuple header structure
|
|
|
|
*
|
|
|
|
* All index tuples start with IndexTupleData. If the HasNulls bit is set,
|
|
|
|
* this is followed by an IndexAttributeBitMapData. The index attribute
|
|
|
|
* values follow, beginning at a MAXALIGN boundary.
|
|
|
|
*
|
|
|
|
* Note that the space allocated for the bitmap does not vary with the number
|
|
|
|
* of attributes; that is because we don't have room to store the number of
|
|
|
|
* attributes in the header. Given the MAXALIGN constraint there's no space
|
|
|
|
* savings to be had anyway, for usual values of INDEX_MAX_KEYS.
|
|
|
|
*/
|
|
|
|
|
1996-08-27 23:50:29 +02:00
|
|
|
typedef struct IndexTupleData
|
|
|
|
{
|
2001-02-21 20:07:04 +01:00
|
|
|
ItemPointerData t_tid; /* reference TID to heap tuple */
|
1996-08-27 23:50:29 +02:00
|
|
|
|
2001-02-22 22:48:49 +01:00
|
|
|
/* ---------------
|
2012-04-24 04:43:09 +02:00
|
|
|
* t_info is laid out in the following fashion:
|
1996-08-27 23:50:29 +02:00
|
|
|
*
|
2001-02-22 22:48:49 +01:00
|
|
|
* 15th (high) bit: has nulls
|
2002-08-25 19:20:01 +02:00
|
|
|
* 14th bit: has var-width attributes
|
2018-04-07 22:00:39 +02:00
|
|
|
* 13th bit: AM-defined meaning
|
2001-02-22 22:48:49 +01:00
|
|
|
* 12-0 bit: size of tuple
|
|
|
|
* ---------------
|
1996-08-27 23:50:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
unsigned short t_info; /* various info about tuple */
|
|
|
|
|
|
|
|
} IndexTupleData; /* MORE DATA FOLLOWS AT END OF STRUCT */
|
|
|
|
|
|
|
|
typedef IndexTupleData *IndexTuple;
|
|
|
|
|
2005-03-27 20:38:27 +02:00
|
|
|
typedef struct IndexAttributeBitMapData
|
|
|
|
{
|
|
|
|
bits8 bits[(INDEX_MAX_KEYS + 8 - 1) / 8];
|
|
|
|
} IndexAttributeBitMapData;
|
|
|
|
|
|
|
|
typedef IndexAttributeBitMapData * IndexAttributeBitMap;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2005-03-27 20:38:27 +02:00
|
|
|
/*
|
|
|
|
* t_info manipulation macros
|
1996-08-27 23:50:29 +02:00
|
|
|
*/
|
|
|
|
#define INDEX_SIZE_MASK 0x1FFF
|
2018-04-07 22:00:39 +02:00
|
|
|
#define INDEX_AM_RESERVED_BIT 0x2000 /* reserved for index-AM specific
|
|
|
|
* usage */
|
1996-08-27 23:50:29 +02:00
|
|
|
#define INDEX_VAR_MASK 0x4000
|
2005-03-27 20:38:27 +02:00
|
|
|
#define INDEX_NULL_MASK 0x8000
|
1996-08-27 23:50:29 +02:00
|
|
|
|
2018-03-01 01:25:54 +01:00
|
|
|
#define IndexTupleSize(itup) ((Size) ((itup)->t_info & INDEX_SIZE_MASK))
|
2001-02-22 22:48:49 +01:00
|
|
|
#define IndexTupleHasNulls(itup) ((((IndexTuple) (itup))->t_info & INDEX_NULL_MASK))
|
2002-08-25 19:20:01 +02:00
|
|
|
#define IndexTupleHasVarwidths(itup) ((((IndexTuple) (itup))->t_info & INDEX_VAR_MASK))
|
1996-08-27 23:50:29 +02:00
|
|
|
|
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
/*
|
|
|
|
* Takes an infomask as argument (primarily because this needs to be usable
|
2005-03-21 02:24:04 +01:00
|
|
|
* at index_form_tuple time so enough space is allocated).
|
1998-01-31 05:39:26 +01:00
|
|
|
*/
|
|
|
|
#define IndexInfoFindDataOffset(t_info) \
|
|
|
|
( \
|
2005-03-27 20:38:27 +02:00
|
|
|
(!((t_info) & INDEX_NULL_MASK)) ? \
|
1998-01-31 05:39:26 +01:00
|
|
|
( \
|
2005-03-27 20:38:27 +02:00
|
|
|
(Size)MAXALIGN(sizeof(IndexTupleData)) \
|
1998-01-31 05:39:26 +01:00
|
|
|
) \
|
|
|
|
: \
|
|
|
|
( \
|
1999-07-19 09:07:29 +02:00
|
|
|
(Size)MAXALIGN(sizeof(IndexTupleData) + sizeof(IndexAttributeBitMapData)) \
|
1998-01-31 05:39:26 +01:00
|
|
|
) \
|
|
|
|
)
|
1996-08-27 23:50:29 +02:00
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
/* ----------------
|
|
|
|
* index_getattr
|
|
|
|
*
|
|
|
|
* This gets called many times, so we macro the cacheable and NULL
|
2005-03-27 20:38:27 +02:00
|
|
|
* lookups, and call nocache_index_getattr() for the rest.
|
1998-01-31 05:39:26 +01:00
|
|
|
*
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
#define index_getattr(tup, attnum, tupleDesc, isnull) \
|
|
|
|
( \
|
1998-06-15 20:40:05 +02:00
|
|
|
AssertMacro(PointerIsValid(isnull) && (attnum) > 0), \
|
|
|
|
*(isnull) = false, \
|
2001-02-22 22:48:49 +01:00
|
|
|
!IndexTupleHasNulls(tup) ? \
|
1998-01-31 05:39:26 +01:00
|
|
|
( \
|
2017-08-20 20:19:07 +02:00
|
|
|
TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff >= 0 ? \
|
1998-01-31 05:39:26 +01:00
|
|
|
( \
|
2017-08-20 20:19:07 +02:00
|
|
|
fetchatt(TupleDescAttr((tupleDesc), (attnum)-1), \
|
2005-03-27 20:38:27 +02:00
|
|
|
(char *) (tup) + IndexInfoFindDataOffset((tup)->t_info) \
|
2017-08-20 20:19:07 +02:00
|
|
|
+ TupleDescAttr((tupleDesc), (attnum)-1)->attcacheoff) \
|
1998-01-31 05:39:26 +01:00
|
|
|
) \
|
|
|
|
: \
|
2010-01-10 05:26:36 +01:00
|
|
|
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
|
1998-01-31 05:39:26 +01:00
|
|
|
) \
|
|
|
|
: \
|
|
|
|
( \
|
2022-07-18 07:43:39 +02:00
|
|
|
(att_isnull((attnum)-1, (bits8 *)(tup) + sizeof(IndexTupleData))) ? \
|
1998-06-15 20:40:05 +02:00
|
|
|
( \
|
|
|
|
*(isnull) = true, \
|
|
|
|
(Datum)NULL \
|
|
|
|
) \
|
|
|
|
: \
|
|
|
|
( \
|
2010-01-10 05:26:36 +01:00
|
|
|
nocache_index_getattr((tup), (attnum), (tupleDesc)) \
|
1998-06-15 20:40:05 +02:00
|
|
|
) \
|
1998-01-31 05:39:26 +01:00
|
|
|
) \
|
|
|
|
)
|
|
|
|
|
2006-05-07 03:21:30 +02:00
|
|
|
/*
|
|
|
|
* MaxIndexTuplesPerPage is an upper bound on the number of tuples that can
|
|
|
|
* fit on one index page. An index tuple must have either data or a null
|
|
|
|
* bitmap, so we can safely assume it's at least 1 byte bigger than a bare
|
|
|
|
* IndexTupleData struct. We arrive at the divisor because each tuple
|
2019-05-14 00:53:39 +02:00
|
|
|
* must be maxaligned, and it must have an associated line pointer.
|
2018-04-14 18:33:15 +02:00
|
|
|
*
|
|
|
|
* To be index-type-independent, this does not account for any special space
|
|
|
|
* on the page, and is thus conservative.
|
|
|
|
*
|
|
|
|
* Note: in btree non-leaf pages, the first tuple has no key (it's implicitly
|
|
|
|
* minus infinity), thus breaking the "at least 1 byte bigger" assumption.
|
|
|
|
* On such a page, N tuples could take one MAXALIGN quantum less space than
|
|
|
|
* estimated here, seemingly allowing one more tuple than estimated here.
|
|
|
|
* But such a page always has at least MAXALIGN special space, so we're safe.
|
2006-05-07 03:21:30 +02:00
|
|
|
*/
|
|
|
|
#define MaxIndexTuplesPerPage \
|
2008-07-13 22:45:47 +02:00
|
|
|
((int) ((BLCKSZ - SizeOfPageHeaderData) / \
|
2006-05-07 03:21:30 +02:00
|
|
|
(MAXALIGN(sizeof(IndexTupleData) + 1) + sizeof(ItemIdData))))
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2003-02-23 07:17:13 +01:00
|
|
|
/* routines in indextuple.c */
|
2005-03-21 02:24:04 +01:00
|
|
|
extern IndexTuple index_form_tuple(TupleDesc tupleDescriptor,
|
|
|
|
Datum *values, bool *isnull);
|
2022-07-06 22:14:00 +02:00
|
|
|
extern IndexTuple index_form_tuple_context(TupleDesc tupleDescriptor,
|
|
|
|
Datum *values, bool *isnull,
|
|
|
|
MemoryContext context);
|
1998-01-31 05:39:26 +01:00
|
|
|
extern Datum nocache_index_getattr(IndexTuple tup, int attnum,
|
2010-01-10 05:26:36 +01:00
|
|
|
TupleDesc tupleDesc);
|
2009-08-01 21:59:41 +02:00
|
|
|
extern void index_deform_tuple(IndexTuple tup, TupleDesc tupleDescriptor,
|
|
|
|
Datum *values, bool *isnull);
|
2021-04-06 00:41:09 +02:00
|
|
|
extern void index_deform_tuple_internal(TupleDesc tupleDescriptor,
|
|
|
|
Datum *values, bool *isnull,
|
|
|
|
char *tp, bits8 *bp, int hasnulls);
|
2003-02-23 07:17:13 +01:00
|
|
|
extern IndexTuple CopyIndexTuple(IndexTuple source);
|
Adjust INCLUDE index truncation comments and code.
Add several assertions that ensure that we're dealing with a pivot tuple
without non-key attributes where that's expected. Also, remove the
assertion within _bt_isequal(), restoring the v10 function signature. A
similar check will be performed for the page highkey within
_bt_moveright() in most cases. Also avoid dropping all objects within
regression tests, to increase pg_dump test coverage for INCLUDE indexes.
Rather than using infrastructure that's generally intended to be used
with reference counted heap tuple descriptors during truncation, use the
same function that was introduced to store flat TupleDescs in shared
memory (we use a temp palloc'd buffer). This isn't strictly necessary,
but seems more future-proof than the old approach. It also lets us
avoid including rel.h within indextuple.c, which was arguably a
modularity violation. Also, we now call index_deform_tuple() with the
truncated TupleDesc, not the source TupleDesc, since that's more robust,
and saves a few cycles.
In passing, fix a memory leak by pfree'ing truncated pivot tuple memory
during CREATE INDEX. Also pfree during a page split, just to be
consistent.
Refactor _bt_check_natts() to be more readable.
Author: Peter Geoghegan with some editorization by me
Reviewed by: Alexander Korotkov, Teodor Sigaev
Discussion: https://www.postgresql.org/message-id/CAH2-Wz%3DkCWuXeMrBCopC-tFs3FbiVxQNjjgNKdG2sHxZ5k2y3w%40mail.gmail.com
2018-04-19 07:45:58 +02:00
|
|
|
extern IndexTuple index_truncate_tuple(TupleDesc sourceDescriptor,
|
|
|
|
IndexTuple source, int leavenatts);
|
1996-08-27 23:50:29 +02:00
|
|
|
|
|
|
|
#endif /* ITUP_H */
|