PostgreSQL Page Internals

COMP9315 23T1 ♢ PG Page Internals ♢ [0/8]
❖ PostgreSQL Page Representation

Functions: src/backend/storage/page/*.c

Definitions: src/include/storage/bufpage.h

Each page is 8KB (default BLCKSZ) and contains:

Large data items are stored in separate (TOAST) files   (implicit)

Also supports ~SQL-standard BLOBs   (explicit large data items)

COMP9315 23T1 ♢ PG Page Internals ♢ [1/8]
❖ PostgreSQL Page Representation (cont)

PostgreSQL page layout:

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

COMP9315 23T1 ♢ PG Page Internals ♢ [2/8]
❖ PostgreSQL Page Representation (cont)

Page-related data types:


// a Page is simply a pointer to start of buffer
typedef Pointer Page;

// indexes into the tuple directory
typedef uint16  LocationIndex;

// entries in tuple directory (line pointer array)
typedef struct ItemIdData
{
   unsigned   lp_off:15,    // tuple offset from start of page
              lp_flags:2,   // unused,normal,redirect,dead
              lp_len:15;    // length of tuple (bytes)
} ItemIdData;

COMP9315 23T1 ♢ PG Page Internals ♢ [3/8]
❖ PostgreSQL Page Representation (cont)

Page-related data types: (cont)


include/storage/bufpage.h
typedef struct PageHeaderData
{
   XLogRecPtr    pd_lsn;      // xact log record for last change
   uint16        pd_tli;      // xact log reference information
   uint16        pd_flags;    // flag bits (e.g. free, full, ...
   LocationIndex pd_lower;    // offset to start of free space
   LocationIndex pd_upper;    // offset to end of free space
   LocationIndex pd_special;  // offset to start of special space
   uint16        pd_pagesize_version;
   TransactionId pd_prune_xid;// is pruning useful in data page?
   ItemIdData    pd_linp[];  // beginning of line pointer array, FLEXIBLE_ARRAY_MEMBER
} PageHeaderData;

typedef PageHeaderData *PageHeader;

COMP9315 23T1 ♢ PG Page Internals ♢ [4/8]
❖ PostgreSQL Page Representation (cont)

Operations on Pages:

void PageInit(Page page, Size pageSize, ...)

OffsetNumber PageAddItem(Page page,
                    Item item, Size size, ...)
void PageRepairFragmentation(Page page)
COMP9315 23T1 ♢ PG Page Internals ♢ [5/8]
❖ PostgreSQL Page Representation (cont)

PostgreSQL has two kinds of pages:

Both kinds of page have the same page layout.

One important difference:

COMP9315 23T1 ♢ PG Page Internals ♢ [6/8]
❖ TOAST'ing

TOAST = The Oversized-Attribute Storage Technique


[Diagram:Pics/storage/toast2.png]

COMP9315 23T1 ♢ PG Page Internals ♢ [7/8]
❖ TOAST'ing (cont)

Large attribute values are stored out-of-line (i.e. in separate file)

Strategies for storing TOAST-able columns ...

COMP9315 23T1 ♢ PG Page Internals ♢ [8/8]


Produced: 20 Feb 2023