CS计算机代考程序代写 2021/4/28 PostgreSQL Tuples

2021/4/28 PostgreSQL Tuples
PostgreSQL Tuples
PostgreSQL Tuples PostgreSQL Attribute Values
>>
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [0/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
1/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples Denitions: include/postgres.h,
include/access/*tup*.h Functions:backend/access/common/*tup*.c e.g.
HeapTuple heap_form_tuple(desc,values[],isnull[])
heap_deform_tuple(tuple,desc,values[],isnull[])
PostgreSQL implements tuples via:
a contiguous chunk of memory
starting with a header giving e.g. #elds, nulls followed by data values (as a sequence of Datum)
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [1/12]
∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
2/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont)
HeapTupleData contains information about a stored tuple typedef HeapTupleData *HeapTuple;
typedef struct HeapTupleData
{
uint32 t_len; // length of *t_data ItemPointerData t_self; // SelfItemPointer
Oid t_tableOid; // table the tuple came from HeapTupleHeader t_data; // -> tuple header and data
} HeapTupleData;
HeapTupleHeader is a pointer to a location in a buffer
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [2/12]
<< ∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
3/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont) Structure of HeapTuple:
<< ∧ >>
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [3/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
4/14

2021/4/28 PostgreSQL Tuples
<< ∧ >>
❖ PostgreSQL Tuples (cont)
PostgreSQL stores each record as tuple header, followed by
data:
typedef HeapTupleHeaderData *HeapTupleHeader;
typedef struct HeapTupleHeaderData // simplified {
HeapTupleFields t_heap;
ItemPointerData
uint16
uint16
uint8
t_ctid; // TID of newer version t_infomask2; // #attributes + flags t_infomask; // flags e.g. has_null t_hoff; // sizeof header incl. t_bits
// above is fixed size (23 bytes) for all heap tuples
bits8 t_bits[1]; // bitmap of NULLs, var.len. // OID goes here if HEAP_HASOID is set in t_infomask
// actual data follows at end of struct
} HeapTupleHeaderData; COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [4/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
5/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont) Tuple structure:
<< ∧ >>
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [5/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
6/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont) Some of the bits in t_infomask ..
#define HEAP_HASNULL 0x0001
/* has null attribute(s) */
#define HEAP_HASVARWIDTH 0x0002
/* has variable-width attribute(s) */
#define HEAP_HASEXTERNAL 0x0004
/* has external stored attribute(s) */
#define HEAP_HASOID_OLD 0x0008
/* has an object-id field */
Location of NULLs is stored in t_bits[] array
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [6/12]
<< ∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
7/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont) Tuple-related data types: (cont)
typedef struct HeapTupleFields // simplified {
TransactionId t_xmin; // inserting xact ID TransactionId t_xmax; // deleting or locking xact ID union {
CommandId t_cid; // inserting or deleting command ID
TransactionId t_xvac;// old-style VACUUM FULL xact ID } t_field3;
} HeapTupleFields;
Note that not all system elds from stored tuple appear
oid is stored after the tuple header, if used
both xmin/xmax are stored, but only one of cmin/cmax
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [7/12]
<< ∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
8/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont)
Tuple-related data types: (cont)
// TupleDesc: schema-related information for HeapTuples
typedef struct tupleDesc
{
<< ∧ >>
int natts;
Oid tdtypeid;
int32 tdtypmod;
bool tdhasoid;
int tdrefcount;
TupleConstr *constr;
FormData_pg_attribute attrs[];
// attrs[N] is a pointer to description of attribute N+1
} *TupleDesc;
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [8/12]
// # attributes in tuple
// composite type ID for tuple type
// typmod for tuple type
// does tuple have oid attribute?
// reference count (-1 if not counting)
// constraints, or NULL if none
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
9/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Tuples (cont) Tuple-related data types: (cont)
// FormData_pg_attribute:
// schema-related information for one attribute
typedef struct FormData_pg_attribute
{
<< ∧ >>
Oid attrelid;
NameData attname;
Oid atttypid;
int16 attlen;
int32 attndims;
bool attnotnull;
…..
// OID of reln containing attr
// name of attribute
// OID of attribute’s data type
// attribute length
// # dimensions if array type
// can attribute have NULL value
// and many other fields
} FormData_pg_attribute;
For details, see include/catalog/pg_attribute.h
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [9/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
10/14

2021/4/28 PostgreSQL Tuples
<< ∧ >>
❖ PostgreSQL Attribute Values
Attribute values in PostgreSQL tuples are packaged as
Datums
// representation of a data value
typedef uintptr_t Datum;
The actual data value:
may be stored in the Datum (e.g. int)
may have a header with length (for varlen attributes) may be stored in a TOAST le (if large value)
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [10/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
11/14

2021/4/28 PostgreSQL Tuples
<< ∧ >> ❖ PostgreSQL Attribute Values (cont)
Attribute values can be extracted as Datum from HeapTuples
Datum heap_getattr(
HeapTuple tup, // tuple (in memory) int attnum, // which attribute TupleDesc tupDesc, // field descriptors bool *isnull // flag to record NULL
)
isnull is set to true if value of eld is NULL
attnum can be negative … to access system attributes (e.g. OID) For details, see include/access/htup_details.h
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [11/12]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html
12/14

2021/4/28 PostgreSQL Tuples
❖ PostgreSQL Attribute Values (cont) Values of Datum objects can be manipulated via e.g.
// DatumGetBool:
// Returns boolean value of a Datum.
#define DatumGetBool(X) ((bool) ((X) != 0))
// BoolGetDatum:
// Returns Datum representation for a boolean.
#define BoolGetDatum(X) ((Datum) ((X) ? 1 : 0)) For details, see include/postgres.h
COMP9315 21T1 ♢ PostgreSQL Tuples ♢ [12/12]
<< ∧ https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html 13/14 2021/4/28 PostgreSQL Tuples Produced: 28 Feb 2021 https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-tuples/slides.html 14/14