Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgres db files - which file represents the specific table/index?

Tags:

postgresql

when i go into sql-8.2/base/ to check how much space does my table take, there are plenty of files named by a number. how can i find the specific file which stores my specific table and index for that table?

for example, i ordered the files by date (newest first) but there are several at that particular period:

-rw-------  1 postgres sql  1.0G Dec  4 13:41 15426233
-rw-------  1 postgres sql  149M Dec  4 13:41 15426233.4
-rw-------  1 postgres sql  1.0G Dec  4 13:41 15426233.3
drwx------  3 postgres sql   75K Dec  4 13:40 .
-rw-------  1 postgres sql  1.0G Dec  4 13:34 15426233.2
-rw-------  1 postgres sql  1.0G Dec  4 13:28 15426233.1
-rw-------  1 postgres sql  3.6M Dec  4 11:23 1249
-rw-------  1 postgres sql  584K Dec  4 11:23 2659
-rw-------  1 postgres sql  672K Dec  4 11:23 2663
-rw-------  1 postgres sql  136K Dec  4 11:23 2662
-rw-------  1 postgres sql  848K Dec  4 11:23 2608
-rw-------  1 postgres sql  2.6M Dec  4 11:23 2658
-rw-------  1 postgres sql  600K Dec  4 11:23 2674
-rw-------  1 postgres sql   56K Dec  4 11:23 2679
-rw-------  1 postgres sql  632K Dec  4 11:23 2673
-rw-------  1 postgres sql   72K Dec  4 11:23 2678
-rw-------  1 postgres sql  1.8M Dec  4 11:22 2619
-rw-------  1 postgres sql  112K Dec  4 11:21 2696
-rw-------  1 postgres sql 1007M Dec  4 11:21 15426228.5
-rw-------  1 postgres sql  1.0G Dec  4 11:19 15426228.4
-rw-------  1 postgres sql  1.0G Dec  4 11:19 15426228.3
-rw-------  1 postgres sql  1.0G Dec  4 11:18 15426228.2
-rw-------  1 postgres sql  1.0G Dec  4 11:17 15426228.1
-rw-------  1 postgres sql  1.0G Dec  4 11:16 15426228
like image 995
ulkas Avatar asked Dec 04 '12 13:12

ulkas


People also ask

How do I find the index of a table in PostgreSQL?

If you use psql to access the PostgreSQL database, you can use the \d command to view the index information for a table.

Where are the Postgres database files?

All the data needed for a database cluster is stored within the cluster's data directory, commonly referred to as PGDATA (after the name of the environment variable that can be used to define it). A common location for PGDATA is /var/lib/pgsql/data.

What is data file in PostgreSQL?

PostgreSQL stores its data files in the same location as its configuration and control files for a database cluster, and it is usually in the directory specified by the PGDATA environment variable. These files are organized into a number of subdirectories.

What is table index in PostgreSQL?

An Index is the structure or object by which we can retrieve specific rows or data faster. Indexes can be created using one or multiple columns or by using the partial data depending on your query requirement conditions. Index will create a pointer to the actual rows in the specified table.


2 Answers

Each directory represents a database (created via create database). The number is the oid of the database. To see the oid and its name, run the following statement:

select oid, datname
from pg_database;

Inside each directory each file corresponds to the an entry in pg_class where the oid matches the number of the file in the directory:

You can see the oids and to which relation they relate by running the statement:

select cl.relfilenode, nsp.nspname as schema_name, cl.relname, cl.relkind
from pg_class cl
  join pg_namespace nsp on cl.relnamespace = nsp.oid;

You might also want to check out the manual

  • Determining Disk Usage
  • Database File Layout
  • System catalogs

Btw: if you are really still running 8.2 you should upgrade as soon as possible.

like image 179
a_horse_with_no_name Avatar answered Oct 18 '22 17:10

a_horse_with_no_name


select pg_relation_filepath('OID or name of a table, index, sequence, or toast table');

For example:

select pg_relation_filepath('flush_history');
Returns base/83780/153211 which you will find in your data directory.

http://www.postgresql.org/docs/current/static/functions-admin.html

like image 29
Kuberchaun Avatar answered Oct 18 '22 17:10

Kuberchaun