❖ Heap Files |
Heap files
Note: this is not "heap" as in the top-to-bottom ordered tree.
❖ Selection in Heaps |
For all selection queries, the only possible strategy is:
// select * from R where C
rel = openRelation("R", READ);
for (p = 0; p < nPages(rel); p++) {
get_page(rel, p, buf);
for (i = 0; i < nTuples(buf); i++) {
T = get_tuple(buf, i);
if (T satisfies C)
add tuple T to result set
}
}
i.e. linear scan through file searching for matching tuples
❖ Selection in Heaps (cont) |
The heap is scanned from the first to the last page:
Costrange = Costpmr = b
If we know that only one tuple matches the query (one query),
a simple optimisation is to stop the scan once that tuple is found.
Costone : Best = 1 Average = b/2 Worst = b
❖ Insertion in Heaps |
Insertion: new tuple is appended to file (in last page).
rel = openRelation("R", READ|WRITE); pid = nPages(rel)-1; get_page(rel, pid, buf); if (size(newTup) > size(buf)) { deal with oversize tuple } else { if (!hasSpace(buf,newTup)) { pid++; nPages(rel)++; clear(buf); } insert_record(buf,newTup); put_page(rel, pid, buf); }
Costinsert = 1r + 1w
❖ Insertion in Heaps (cont) |
Alternative strategy:
R
R
backend/access/heap/{heapam.c,hio.c}
❖ Insertion in Heaps (cont) |
Dealing with oversize tuple t
for i in 1 .. nAttr(t) { if (t[i] not oversized) continue off = appendToFile(ovf, t[i]) t[i] = (OVERSIZE, off) } insert into buf as before
❖ Insertion in Heaps (cont) |
PostgreSQL's tuple insertion:
(Relation relation, // relation desc HeapTuple newtup, // new tuple data CommandId cid, ...) // SQL statement heap_insert
newtup
xmin
❖ Deletion in Heaps |
SQL: delete from
where
Implementation of deletion:
rel = openRelation("R",READ|WRITE); for (p = 0; p < nPages(rel); p++) { get_page(rel, p, buf); ndels = 0; for (i = 0; i < nTuples(buf); i++) { tup = get_tuple(buf,i); if (tup satisfies Condition) { ndels++; delete_record(buf,i); } } if (ndels > 0) put_page(rel, p, buf); if (ndels > 0 && unique) break; }
❖ Deletion in Heaps (cont) |
PostgreSQL tuple deletion:
(Relation relation, // relation desc ItemPointer tid, ..., // tupleID CommandId cid, ...) // SQL statement heap_delete
tid
xmax
❖ Updates in Heaps |
SQL: update
set
=
where
Analysis for updates is similar to that for deletion
Complication: new tuple larger than old version (too big for page)
Solution: delete, re-organise free space, then insert
❖ Updates in Heaps (cont) |
PostgreSQL tuple update:
(Relation relation, // relation desc ItemPointer otid, // old tupleID HeapTuple newtup, ..., // new tuple data CommandId cid, ...) // SQL statement heap_update
delete(otid)
insert(newtup)
ctid
❖ Heaps in PostgreSQL |
PostgreSQL stores all table data in heap files (by default).
Typically there are also associated index files.
If a file is more useful in some other form:
create index...using hash
❖ Heaps in PostgreSQL (cont) |
PostgreSQL "heap file" may use multiple physical files
OID
OID.1
OID_fsm
OID_vm
Produced: 7 Mar 2021