Home > Software design >  SQLite3 Record Format differs from the doc
SQLite3 Record Format differs from the doc

Time:10-03

In the official documentation documentation I read the following about the structure of a record

A record contains a header and a body, in that order. The header begins with a single varint which determines the total number of bytes in the header. The varint value is the size of the header in bytes including the size varint itself. Following the size varint are one or more additional varints, one per column. These additional varints are called "serial type" numbers and determine the datatype of each column, ...

To take a closer look at this, I created a new database (SQLite Version 3.22.0) and generated the following table

sqlite> CREATE TABLE IF NOT EXISTS table_1(id INT NOT NULL, name TEXT NOT NULL);
sqlite> INSERT INTO table_1 VALUES (1, "Hello World");

I opened the database file with a hex editor. My data record is saved as follows:

0E 01 03 09  23 48 65 6C  6C 6F 20 57  6F 72 6C 64

The header begins with the header size. In this case 14 (0E). After this "Following the size varint are one or more additional varints, one per column". This is very confused. I have two columns: id and name. So I expected two varints for the corresponding Serial Type Codes (see link):

  1. 1 (1 Byte integer to store the value 1)
  2. 23 (indicating a 11 Byte String: (0x23 = 35 => (35-13) / 2 = 11))

I would have expected the following header:

03 01 23

with the body or data segment:

01 48 65 6C  6C 6F 20 57  6F 72 6C 64

Why is there a difference to the official literature? What do the additional values ​​mean and why is the length of the header specified as 14 bytes?

CodePudding user response:

The 0E 01 at the start of the bytes you're looking at are from from the b-tree leaf cell header (14 bytes of payload (the record), rowid 1), not the record itself.

The record header is actually 03 09 23. Three bytes long, first column value is 1 (which takes up no space in the body), and an 11 byte string. Then comes the string data in the record body.

  • Related