❖ Tuples |
Each page contains a collection of tuples
What do tuples contain? How are they structured internally?
❖ Records vs Tuples |
A table is defined by a schema, e.g.
create table Employee ( id integer primary key, name varchar(20) not null, job varchar(10), dept smallint references Dept(id) );
where a schema is a collection of attributes (name,type,constraints)
Reminder: schema information (meta-data) is also stored, in the DB catalog
❖ Records vs Tuples (cont) |
Tuple = collection of attribute values based on a schema, e.g.
Record = sequence of bytes, containing data for one tuple, e.g.
Bytes need to be interpreted relative to schema to get tuple
❖ Converting Records to Tuples |
A Recordbyte[]
TupleTuplestruct
❖ Converting Records to Tuples (cont) |
Information on how to interpret bytes in a record ...
❖ Operations on Records |
Common operation on records ... access record via RecordId
Record get_record(Relation rel, RecordId rid) {
(pid,tid) = rid;
Page buf = get_page(rel, pid);
return get_bytes(rel, buf, tid);
}
Cannot use a RecordTuple
Relation rel = ... // relation schema
Record rec = get_record(rel, rid)
Tuple t = mkTuple(rel, rec)
Once we have a Tuple
❖ Operations on Tuples |
Once we have a record, we need to interpret it as a tuple ...
Tuple t = mkTuple(rel, rec)
rel
Once we have a tuple, we want to examines its contents ...
Typ getTypField(Tuple t, int i)
iTupleint x = getIntField(t,1)char *s = getStrField(t,2)❖ Fixed-length Records |
A possible encoding scheme for fixed-length records:
Since record format is frequently used at query time, cache in memory.
❖ Variable-length Records |
Possible encoding schemes for variable-length records:
❖ Data Types |
DBMSs typically define a fixed set of base types, e.g.
DATEFLOATINTEGERNUMBER()VARCHAR()This determines implementation-level data types for field values:
DATE |
time_t |
|
FLOAT |
float,double |
|
INTEGER |
int,long |
|
NUMBER() |
int[] |
|
VARCHAR() |
char[] |
PostgreSQL allows new base types to be added
❖ Field Descriptors |
A Tuple
FieldDescRecord
typedef struct {
ushort nfields; // number of fields/attrs
ushort data_off; // offset in struct for data
FieldDesc fields[]; // field descriptions
Record data; // pointer to record in buffer
} Tuple;
Fields are derived from relation descriptor + record instance data.
❖ Field Descriptors (cont) |
Tuple data
❖ Field Descriptors (cont) |
Or, tuple data
Tuple struct
Produced: 20 Feb 2023