text
stringlengths
0
1.99k
In other words, each table has its Filenode. We can lookup the OID and
respective Filenode names of a given table through the following queries:
--------------------------------------------------------------------------
SELECT oid FROM pg_class WHERE relname='TABLE_NAME'
// OR
SELECT pg_relation_filepath('TABLE_NAME');
--------------------------------------------------------------------------
All of the filenodes are stored in the PostgreSQL data directory. The path
to which can be queried from the pg_settings table by superusers:
--------------------------------------------------------------------------
SELECT setting FROM pg_settings WHERE name = 'data_directory';
--------------------------------------------------------------------------
However, this value should generally be the same across different
installations of the DBMS and can be easily guessed by a third party.
A common path for PostgreSQL data directories on Debian systems is
"/var/lib/postgresql/MAJOR_VERSION/CLUSTER_NAME/".
We can obtain the major version by running a "SELECT version()" query in
the SQLi. The default value of CLUSTER_NAME is "main".
An example path of a filenode for our "phrases" would be:
--------------------------------------------------------------------------
=== in psql ===
postgres=# SELECT pg_relation_filepath('phrases');
pg_relation_filepath
----------------------
base/13485/65549
(1 row)
postgres=# SELECT version();
version
-------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 13.13 (Ubuntu 13.13-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, 64-bit
(1 row)
=== in bash ===
$ ll /var/lib/postgresql/13/main/base/13485/65549
-rw------- 1 postgres postgres 8192 mar 14 13:45 /var/lib/postgresql/13/main/base/13485/65549
--------------------------------------------------------------------------
So: all of the files with numeric names, found in section 1.2, are in
fact separate table filenodes that the postgres user can read and write!
---[ 2.1 - Filenode format
A Filenode is a binary file composed of separate chunks of 0x2000 bytes
called Pages. Each page holds the actual row data within nested Item
objects. The layout of each Filenode can be summarized with the below
diagram:
+----------+
| Filenode |
+----------+-------------------------------------------------------+
| |
| +--------+ |
| | Page 1 | |
| +--------+----+---------+---------+-----+---------+--------+ |
| | Page Header |Item ID 1|Item ID 2| ... |Item ID n| | |
| +-------------+----+----+---------+ +----+----+ | |
| | | | | |
| | +-------------------------+--------+ | |
| | | | | |
| | +-------------------------------------+ | | |
| | | | | |
| | | ... empty space padded with 0x00 ... | | |
| | | | | |
| | +----------------------+ | | |
| | | | | |
| | v v | |
| | +--------+ +--------+--------+ |
| | | Item n | ... | Item 2 | Item 1 | |
| +-------------------------+--------+-----+--------+--------+ |
| ... |
| +--------+ |
| | Page n | |
| +--------+ |
| ... |
| |
+------------------------------------------------------------------+
---[ 2.2 - Table metadata
It is worth noting that the Item objects are stored in the binary format
and cannot be manipulated directly. One must first deserialize them using
metadata from the internal PostgreSQL "pg_attribute" table. We can query
Item metadata using the following SQL query:
--------------------------------------------------------------------------