Sobes.tech
Junior — Middle

How does PostgreSQL organize data storage in a table when using an index?

sobes.tech AI

Answer from AI

In PostgreSQL, data in a table is stored in pages of fixed size (usually 8 KB). When indexes are used, a separate data structure is created that stores references to the table's rows (tuples) and the values of indexed columns for quick search.

An index in PostgreSQL is usually implemented as a B-tree (balanced tree), where each node contains keys and pointers to child nodes or specific rows in the table. When executing a query with a condition that uses an index, the DBMS quickly finds the necessary keys in the index and then accesses the corresponding table rows.

Thus, the table data and the index are stored separately: the table as a sequence of data pages, and the index as a B-tree structure containing references to this data. This allows for faster search, sorting, and filtering operations on indexed fields.