Postgres System information functions and examples

Here are collected some of the system functions and tricks how to get data from the Postgres database

How to get creation clause of the view

Sometimes you have an database view and you start to wonder what kind of sql clause there is. Following command will show you definition of particular view.

1
select definition from pg_views where schemaname = 'example_public' and viewname ='example_view';

How do I see the status of database index in postgres

Notice! Following query requires the PG_stat extension.

1
2
3
SELECT schemaname, tablename, indexname, idx_scan, idx_tup_read, idx_tup_fetch
    FROM pg_stat_all_indexes
    WHERE schemaname NOT LIKE 'pg_%' AND schemaname != 'information_schema';

Column Description
schemaname The name of the schema that contains the table.
tablename The name of the table.
indexname The name of the index.
idx_scan The number of index scans.
idx_tup_read The number of tuples read from the index.
idx_tup_fetch The number of tuples fetched from the table using the index.
By excluding system schemas (pg_%) and the information_schema, query filters out internal tables and system indexes.