COMP9315 Week 03 Monday Lecture

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [0/53]
❖ Things To Note


COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [1/53]
❖ Assignment 1

pname.c

pname.source
COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [2/53]
❖ Assignment 1 (cont)


How it fits together:

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [3/53]
❖ Assignment 1 (cont)

Creating and using PersonName objects:

[Diagram:Pics/assignments/pname1.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [4/53]
❖ Assignment 1 (cont)


Incorrect memory usage:

[Diagram:Pics/assignments/pname2.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [5/53]
❖ Assignment 1 (cont)


Correct memory usage:

[Diagram:Pics/assignments/pname3.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [6/53]
❖ Tuples

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [7/53]
❖ Tuples

Each page contains a collection of tuples

[Diagram:Pics/storage/page-tuples.png]


What do tuples contain? How are they structured internally?

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [8/53]
❖ Records vs Tuples

A table is defined by a collection of attributes (schema), e.g.

create table Employee (
   id integer primary key,  name varchar(20),
   job  varchar(10),  dept number(4)
);

Tuple = collection of attribute values for such a schema, e.g.

(33357462, 'Neil Young', 'Musician', 0277)

Record = sequence of bytes, containing data for one tuple, e.g.

[Diagram:Pics/storage/tuple-bytes.png]

Bytes need to be interpreted relative to schema to get tuple

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [9/53]
❖ Operations on Records

Common operation on records ... access record via RecordId:

Record get_record(Relation rel, RecordId rid) {
    (pid,tid) = rid;
    Page *buf = request_page(rel, pid);
    return get_bytes(buf, tid);
}

Gives a sequence of bytes, which needs to be interpreted, e.g.

Relation rel = ... // relation schema
Record rec = get_record(rel,rid)
Tuple t = makeTuple(rel,rec)

Once we have a tuple, we can access individual attributes/fields

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [10/53]
❖ Operations on Tuples

Once we have a record, we need to interpret it as a tuple ...

Tuple t = makeTuple(Relation rel, Record rec)


Once we have a tuple, we want to examine its contents ...

Typ   getTypField(Tuple t, int fieldNum)

E.g.   int x = getIntField(t,1),   char *s = getStrField(t,2)
COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [11/53]
❖ Scanning

Access methods typically involve iterators, e.g.


Scan s = start_scan(Relation r, ...)


Tuple next_tuple(Scan s)

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [12/53]
❖ Example Query

Example: simple scan of a table ...

select name from Employee

implemented as:

DB db = openDatabase("myDB");
Relation r = openRel(db,"Employee");
Scan s = start_scan(r);
Tuple t;  // current tuple
while ((t = next_tuple(s)) != NULL)
{
   char *name = getStrField(t,2);
   printf("%s\n", name);
}

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [13/53]
❖ Exercise: Implement next_tuple()

Consider the following possible Scan data structure

typedef struct {
   Relation rel;
   Page     *curPage;  // Page buffer
   int      curPID;    // current pid
   int      curTID;    // current tid
} ScanData;

Assume tuples are indexed 0..nTuples(p)-1

Assume pages are indexed 0..nPages(rel)-1

Implement the  Tuple next_tuple(Scan)  function

P.S. What's in a Relation object?

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [14/53]
❖ Fixed-length Records

Encoding scheme for fixed-length records:

[Diagram:Pics/storage/fixed-length.png]

Since record format is frequently used at query time, should be in memory.

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [15/53]
❖ Variable-length Records

Some encoding schemes for variable-length records:

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [16/53]
❖ Converting Records to Tuples

A Record is an array of bytes (byte[])

A Tuple is a collection of named,typed values  (cf. C struct)

Information on how to interpret the bytes as typed values

For variable-length records, some formatting info ...
COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [17/53]
❖ Converting Records to Tuples (cont)

DBMSs typically define a fixed set of field types, e.g.

DATE,  FLOAT,  INTEGER,  NUMBER(n),  VARCHAR(n), ...

This determines implementation-level data types:

DATE time_t
FLOAT float,double
INTEGER int,long
NUMBER(n) int[] (?)
VARCHAR(n) char[]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [18/53]
❖ Converting Records to Tuples (cont)

A Tuple could be defined as

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.

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [19/53]
❖ Converting Records to Tuples (cont)

Tuple data could be

[Diagram:Pics/storage/rec8.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [20/53]
❖ Converting Records to Tuples (cont)

Or, tuple data could be ...

[Diagram:Pics/storage/rec9.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [21/53]
❖ Exercise: How big is a FieldDesc?

FieldDesc = (offset,length,type), where

If pages are 8KB in size, how many bits are needed for each?

E.g.

[Diagram:Pics/storage/field-desc.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [22/53]
❖ PostgreSQL Tuples

Definitions: include/postgres.h,  include/access/*tup*.h

Functions: backend/access/common/*tup*.c  e.g.

PostgreSQL defines tuples via:
COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [23/53]
❖ PostgreSQL Tuples (cont)

Tuple structure:

[Diagram:Pics/storage/pg-tuple-struct.png]

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [24/53]
❖ PostgreSQL Tuples (cont)

Tuple-related data types:

// representation of a data value
typedef uintptr_t Datum;

The actual data value:

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [25/53]
❖ PostgreSQL Tuples (cont)

Tuple-related data types: (cont)

// TupleDescData: schema-related information for HeapTuples

typedef struct tupleDescData
{
  int         natts;          // number of attributes in the tuple 
  Oid         tdtypeid;       // composite type ID for tuple type 
  int32       tdtypmod;       // typmod for tuple type 
  int         tdrefcount;     // reference count (-1 if not counting)
  TupleConstr *constr;        // constraints, or NULL if none 
  FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
  // attrs[N] is description of attribute number N+1 
} *TupleDesc;

COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [26/53]
❖ 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 24T1 ♢ Week 3 Monday Lecture ♢ [27/53]
❖ PostgreSQL Tuples (cont)

PostgreSQL stores a single block of data for tuple

  • containing a tuple header, followed by data byte[]

    typedef struct HeapTupleHeaderData // simplified
    {
      HeapTupleFields t_heap;
      ItemPointerData t_ctid;      // TID of this tuple or newer version
      uint16          t_infomask2; // #attributes + flags
      uint16          t_infomask;  // flags e.g. has_null, has_varwidth
      uint8           t_hoff;      // sizeof header incl. bitmap+padding
      // above is fixed size (23 bytes) for all heap tuples
      bits8           t_bits[1];   // bitmap of NULLs, variable length
      // OID goes here if HEAP_HASOID is set in t_infomask
      // actual data follows at end of struct
    } HeapTupleHeaderData;
    

  • COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [28/53]
    ❖ 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 fields from stored tuple appear

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [29/53]
    ❖ Relational Operations and Cost Models

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [30/53]
    ❖ DBMS Architecture (revisited)

    Implementation of relational operations in DBMS:

    [Diagram:Pics/scansortproj/dbmsarch-relop.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [31/53]
    ❖ Relational Operations

    DBMS core = relational engine, with implementations of

    In this part of the course: Terminology reminder:
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [32/53]
    ❖ Cost Analyses

    When showing the cost of operations, don't include Tr and Tw:

    When comparing two methods for same query In counting reads and writes, assume minimal buffering
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [33/53]
    ❖ Cost Analyses (cont)

    Two "dimensions of variation":

    Each query method involves an operator and a file structure: As well as query costs, consider update costs (insert/delete).
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [34/53]
    ❖ Cost Analyses (cont)

    SQL vs DBMS engine

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [35/53]
    ❖ Query Types

    Type SQL RelAlg a.k.a.
    Scan select * from R R -
    Proj select x,y from R Proj[x,y]R -
    Sort select * from R
    order by x
    Sort[x]R ord
    Sel1 select * from R
    where id = k
    Sel[id=k]R one
    Seln select * from R
    where a = k
    Sel[a=k]R -
    Join1 select * from R,S
    where R.id = S.r
    R Join[id=r] S -

    Different query classes exhibit different query processing behaviours.
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [36/53]
    ❖ File Structures

    When describing file structures

    [Diagram:Pics/scansortproj/one-page.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [37/53]
    ❖ File Structures (cont)

    Consider three simple file structures:

    All files are composed of b primary blocks/pages

    [Diagram:Pics/scansortproj/file-struct0.png]

    Some records in each page may be marked as "deleted".

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [38/53]
    ❖ Exercise: Operation Costs

    For each of the following file structures

    You can assume the existence of a file header containing Assume also
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [39/53]
    ❖ Operation Costs Example

    Heap file with b = 4, c = 4:

    [Diagram:Pics/scansortproj/file-heap.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [40/53]
    ❖ Operation Costs Example (cont)

    Sorted file with b = 4, c = 4:

    [Diagram:Pics/scansortproj/file-sort1.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [41/53]
    ❖ Operation Costs Example (cont)

    Hashed file with b = 3, c = 4, h(k) = k%3

    [Diagram:Pics/scansortproj/file-hash.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [42/53]
    ❖ Scanning

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [43/53]
    ❖ Scanning

    Consider the query:

    select * from Rel;
    

    Operational view:

    for each page P in file of relation Rel {
       for each tuple t in page P {
          add tuple t to result set
       }
    }
    

    Cost: read every data page once

    Cost = b     (b page reads + ≤b page writes)

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [44/53]
    ❖ Scanning (cont)

    Scan implementation when file has overflow pages, e.g.

    [Diagram:Pics/scansortproj/file-struct1.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [45/53]
    ❖ Scanning (cont)

    In this case, the implementation changes to:

    for each page P in file of relation T {
        for each tuple t in page P {
            add tuple t to result set
        }
        for each overflow page V of page P {
            for each tuple t in page V {
                add tuple t to result set
    }   }   }
    

    Cost: read each data and overflow page once

    Cost = b + bOv

    where bOv = total number of overflow pages

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [46/53]
    ❖ Selection via Scanning

    Consider a one query like:

    select * from Employee where id = 762288;
    

    In an unordered file, search for matching tuple requires:


    [Diagram:Pics/scansortproj/file-search.png]


    Guaranteed at most one answer; but could be in any page.

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [47/53]
    ❖ Selection via Scanning (cont)

    Overview of scan process:

    for each page P in relation Employee {
        for each tuple t in page P {
            if (t.id == 762288) return t
    }   }
    

    Cost analysis for one searching in unordered file

    Page Costs:   Costavg = b/2     Costmin = 1     Costmax = b
    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [48/53]
    ❖ Exercise: Cost of Search in Hashed File

    Consider the hashed file structure b = 10, c = 4, h(k) = k%10

    [Diagram:Pics/scansortproj/hash-file-10.png]

    Describe how the following queries

    select * from R where k = 51;
    select * from R where k > 50;
    

    might be solved in a file structure like the above (h(k) = k%b).

    Estimate the minimum and maximum cost (as #pages read)

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [49/53]
    ❖ Relation Copying

    Consider an SQL statement like:

    create table T as (select * from S);
    

    Effectively, copies data from one table to another.

    Process:

    s = start scan of S
    make empty relation T
    while (t = next_tuple(s)) {
        insert tuple t into relation T
    }
    

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [50/53]
    ❖ Relation Copying (cont)

    Possible that T is smaller than S

    [Diagram:Pics/scansortproj/copy-files.png]

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [51/53]
    ❖ Relation Copying (cont)

    In terms of existing relation/page/tuple operations:

    Relation in;       // relation handle (incl. files)
    Relation out;      // relation handle (incl. files)
    int ipid,opid,tid; // page and record indexes
    Record rec;        // current record (tuple)
    Page ibuf,obuf;    // input/output file buffers
    
    in = openRelation("S", READ);
    out = openRelation("T", NEW|WRITE);
    clear(obuf);  opid = 0;
    for (ipid = 0; ipid < nPages(in); ipid++) {
        get_page(in, ipid, ibuf);
        for (tid = 0; tid < nTuples(ibuf); tid++) {
            rec = get_record(ibuf, tid);
            if (!hasSpace(obuf,rec)) {
                put_page(out, opid++, obuf);
                clear(obuf);
            }
            insert_record(obuf,rec);
    }   }
    if (nTuples(obuf) > 0) put_page(out, opid, obuf);
    

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [52/53]
    ❖ Exercise: Cost of Relation Copy

    Analyse cost for relation copying:

    1. if both input and output are heap files
    2. if input is sorted and output is heap file
    3. if input is heap file and output is sorted
    Assume ...

    Give cost in terms of #pages read + #pages written

    COMP9315 24T1 ♢ Week 3 Monday Lecture ♢ [53/53]


    Produced: 6 May 2024