DEV Community

Lakshya Khera
Lakshya Khera

Posted on

Pages and Blocks in DBMS

Hey there! I'm not so active here cause I don't write things that often, but here we are.

Been spending a lot of time around databases and everything around them, so I wanted to understand how data gets read and it's crazy how everything is orchestrated.
Usually pages and blocks are called interchangeably which pissed me off.

Disclaimer: I'm not an expert on this subject and might make mistakes.

We'll consider a scenario where we're dealing with a Relational Database (eg: Postgres)

A Table row is stored as tuples in disk
Remember this, what I mean by tuple is just an array of your entries in a row.

But of course, you can't store all these rows as it is on disk. So these rows are bundled together in Pages (database)

A page can have a bunch of rows next to each other with increasing order of IDs.

How tuples stored in a page
Let's not waste time around the technicals of the page.

Now whenever you read a row, you're fetching a whole page! (It's mainly due to efficiency. Disk I/O operations are expensive (time-consuming)) and posts this page in its cache (RAM) so in case if you re-read it you don't have to search the disk.

-- Easy till here --

Now storing this page on disk requires more work, we can't just store all pages as it is cause OS handles a lot of other things (READ about fragmentation)

So these pages ( end up breaking up into smaller chunks into a block (around 2kb).

Block is a logical unit of data in OS, in other words, OS tells with data as blocks. So it's part of file system jobs whenever to combine all these chunks of blocks into a page/file.

-- Still easy -- Cause we're not deep diving it.

But this physical disk? In that realm there are no blocks, but pages or sectors.

So There is a module called Disk Controller which puts these blocks into a disk and it's their job to take care of everything.
Here in simple terms, this controller maintains an LBA (Logical Block address) to Physical address mapping.

And on fetching this gets cached in the File system.

--- That's it --

I haven't deep-dived on things work but I suggest you should.
Feel free to reach out, and follow my socials!

Top comments (0)