Count file size in PostgreSQL - postgresql

I have this simple table which I would like to use to store files.
CREATE TABLE table(
ID INTEGER NOT NULL,
FILE_NAME TEXT,
FILE OID
)
;
Is there any way with SQL query to count the total size of the files into the table? Is this possible without function?

rzo is close but not quite right.
select file_name, pg_column_size(lo_get(oid)) from files;
Gives you the size in bytes.
If you want pretty printing:
select file_name, pg_size_pretty(pg_column_size(lo_get(oid))::numeric) from files;

If you are interested in the total disk size (disk usage), you could do something like:
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
Other queries to measure the size of relations can be found in the PostgreSQL Wiki

Related

Select table size and row counts for specific schema

I need to have a query that would give me the information for each table in specific schema. The information would be the size of the table (best in Mb) and also the row counts. I prepared some query as below but not sure if the result is in megabytes. Moreover i do not know how to get the row counts from information_schema.tables. Can somebody help?
This is my current query:
select table_name, pg_relation_size(quote_ident(table_name))
from information_schema.tables
where table_schema = 'myschema'
order by 2;
EDIT:
By this i can get row counts nevertheless do not know how to filter based on specific schema and how to add table size in Mb to it.
select nspname as schema, relname as tablename,
reltuples as rowcounts
from pg_class c JOIN pg_catalog.pg_namespace n
ON n.oid = c.relnamespace where relkind='r'
and relname like 't%'
order by nspname, reltuples desc;
You can get the size of the table in megabytes using the pg_relation_size function. By default the function will return the result in bytes, but you can convert the result to megabytes. Also, to filter by a specific scheme, use the table_schema entry from information_schema.tables.
SELECT
table_name,
(pg_relation_size(quote_ident(table_name)) / 1024 /1024) size,
reltuples as rowcounts FROM information_schema.tables ist
left join pg_class pc on pc.relname = ist.table_name
WHERE table_schema = 'public'
Demo in sql<>daddy.io

How to list unused indices and drop them in postgres?

Unused indexes take lot of space and slow down data modifications. How can I check if my indexes are really useful and delete unused ones?
Make sure your applications have run long enough since the last stat reset. Think especially of periodic jobs that runs once every week and may require an index.
This is the code to make a human readable view with usefull indices information. The indices on partitionned table are aggregated. If you have templates tables for your partitions, make sure the filters for template tables names are accurate.
CREATE VIEW stats_index_aggregate as
-- Index for partitionned tables
select
'partitioned index' as indextype,
nsp.nspname as schemaname,
table_class.relname as tablename,
parent_class.relname as indexname,
index_columns.idx_columns as idx_columns,
seek_childs.nb_child_index,
seek_childs.nb_scans,
seek_childs.index_size
from pg_class parent_class
join pg_index parent_index on parent_index.indexrelid = parent_class.oid
join pg_namespace nsp on nsp.oid = parent_class.relnamespace -- to get schemaname
join pg_class table_class on table_class.oid = parent_index.indrelid
, lateral (
select count(stats_child.idx_scan) as nb_child_index,
sum(stats_child.idx_scan) as nb_scans,
sum(pg_relation_size(stats_child.indexrelid)) as index_size
from pg_catalog.pg_stat_user_indexes stats_child
join pg_inherits pi on pi.inhrelid = stats_child.indexrelid
where pi.inhparent = parent_class.oid
) seek_childs
, LATERAL (
SELECT string_agg(attname, ', ' order by attnum) AS idx_columns
FROM pg_attribute
WHERE attrelid = parent_class.oid
) index_columns
where parent_class.relkind = 'I'
AND 0 <>ALL (parent_index.indkey) -- no index column is an expression
AND NOT parent_index.indisunique -- is not a UNIQUE index
AND NOT EXISTS -- does not enforce a constraint
(SELECT 1 FROM pg_catalog.pg_constraint cc WHERE cc.conindid = parent_index.indexrelid)
and table_class.relname not like '%template' -- filter for template tables
union
-- Index for regular tables
select
'regular index' as indextype,
stats_child.schemaname,
stats_child.relname AS tablename,
c.relname as indexname,
index_columns.idx_columns as idx_columns,
null as nb_child_index,
stats_child.idx_scan as id_scan_count,
pg_relation_size(stats_child.indexrelid) as index_size
from pg_class c
join pg_index idx_parent on idx_parent.indexrelid = c.oid
join pg_catalog.pg_stat_user_indexes stats_child on c.oid = stats_child.indexrelid
, LATERAL (
SELECT string_agg(attname, ', ' order by attnum) AS idx_columns
FROM pg_attribute
WHERE attrelid = c.oid
) index_columns
where c.relkind = 'i'
AND 0 <>ALL (idx_parent.indkey) -- no index column is an expression
AND NOT idx_parent.indisunique -- is not a UNIQUE index
AND NOT EXISTS -- does not enforce a constraint
(SELECT 1 FROM pg_catalog.pg_constraint cc
WHERE cc.conindid = idx_parent.indexrelid)
AND NOT EXISTS -- is not a child index
(SELECT 1 FROM pg_inherits pi
where pi.inhrelid = c.oid)
and stats_child.relname not like '%template'; -- filter for template tables
Then you can check how much space you can save with this summary :
select count(*) || ' indices are not used, you can drop them and save ' || pg_size_pretty(sum(index_size)) as pretty_size from stats_index_aggregate where nb_scans = 0;
Execute this query output to clean your database :
select
case when nb_scans = 0 then 'DROP INDEX ' || indexname || ';' else '' end as drop_command, -- make sure we don't print drop command for used index
*, pg_size_pretty(index_size) as pretty_size
from stats_index_aggregate;

Get table size of partitioned table (Postgres 10+)

I came across this query on Postgres weekly which shows tables, their sizes, toast sizes and index sizes in bytes:
SELECT
relname AS table_name,
pg_size_pretty(pg_total_relation_size(relid)) AS total,
pg_size_pretty(pg_relation_size(relid)) AS internal,
pg_size_pretty(pg_table_size(relid) - pg_relation_size(relid)) AS external,
pg_size_pretty(pg_indexes_size(relid)) AS indexes
FROM pg_catalog.pg_statio_user_tables
ORDER BY pg_total_relation_size(relid) DESC;
I know that Postgres is creating a table for each partition so I am getting entries for each partition separately, but is there a way to get one row per table, regardless of whether this table is partitioned or not?
Going by instructions from #Laurenz Albe I created a query that satisfies my needs. This will get total memory for all partitioned tables from specific database.
SELECT
pi.inhparent::regclass AS parent_table_name,
pg_size_pretty(sum(pg_total_relation_size(psu.relid))) AS total,
pg_size_pretty(sum(pg_relation_size(psu.relid))) AS internal,
pg_size_pretty(sum(pg_table_size(psu.relid) - pg_relation_size(psu.relid))) AS external, -- toast
pg_size_pretty(sum(pg_indexes_size(psu.relid))) AS indexes
FROM pg_catalog.pg_statio_user_tables psu
JOIN pg_class pc ON psu.relname = pc.relname
JOIN pg_database pd ON pc.relowner = pd.datdba
JOIN pg_inherits pi ON pi.inhrelid = pc.oid
WHERE pd.datname = :database_name
GROUP BY pi.inhparent
ORDER BY sum(pg_total_relation_size(psu.relid)) DESC;
Note that in the case when we have partitions of partitions, this will not have one row for the root table, but every parent table will have it's own row
This gives table size per parent table even if we have multiple partition levels:
WITH RECURSIVE tables AS (
SELECT
c.oid AS parent,
c.oid AS relid,
1 AS level
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_inherits AS i ON c.oid = i.inhrelid
-- p = partitioned table, r = normal table
WHERE c.relkind IN ('p', 'r')
-- not having a parent table -> we only get the partition heads
AND i.inhrelid IS NULL
UNION ALL
SELECT
p.parent AS parent,
c.oid AS relid,
p.level + 1 AS level
FROM tables AS p
LEFT JOIN pg_catalog.pg_inherits AS i ON p.relid = i.inhparent
LEFT JOIN pg_catalog.pg_class AS c ON c.oid = i.inhrelid AND c.relispartition
WHERE c.oid IS NOT NULL
)
SELECT
parent ::REGCLASS AS table_name,
array_agg(relid :: REGCLASS) AS all_partitions,
pg_size_pretty(sum(pg_total_relation_size(relid))) AS pretty_total_size,
sum(pg_total_relation_size(relid)) AS total_size
FROM tables
GROUP BY parent
ORDER BY sum(pg_total_relation_size(relid)) DESC
Sure, but you'd have to join with a bunch of other catalog tables and use GROUP BY.
Attribute relkind from he catalog pg_class will tell you if a relation is partitioned (p) or not (r).
The catalog pg_inherits will tell you which partition (inhrelid) belongs to which partitioned table (inhparent).
Since partitions can be partitioned again, you will have to write a recursive common table expression if you want to cover all bases.

Record table and schema sizes in PostgreSQL

Is thre a simple way to query for the size of a postgres database?
I am trying to do something like this:
select 'session_metrics',pg_size_pretty(pg_total_relation_size('schema1.session_metrics'))
union
select 'transaction_metrics',pg_size_pretty(pg_total_relation_size('schema1.transaction_metrics'))
union
select 'cookie_user_metrics',pg_size_pretty(pg_total_relation_size('schema1.cookie_user_metrics'))
union
select 'cookie_transaction_metrics',pg_size_pretty(pg_total_relation_size('schema1.cookie_transaction_metrics'));
And store those values in a table so that I can later easily track the growth rates of my tables.
The problem is I now have over 50 different tables and I don't want to add a line of query every time I create a new table.
I would appreciate if someone could orient me to something like:
select table_name, schema_name, size;
The 'table names' table you're looking for is pg_catalog.pg_namespace.
The following query is adapted from the psql \d command:
SELECT n.nspname as "Schema",
c.relname as "Name",
pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "Size",
now() as "Timestamp"
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
I have taken the liberty of adding a timestamp since you are planning to compare sizes over time.

pg_relation_size tells me column doesn't exist

http://www.postgresql.org/docs/8.4/static/functions-admin.html says:
pg_relation_size
accepts the OID or name of a table, index or toast table, and returns the size in bytes
However when I use it with a valid table name, I get the error:
column [table] does not exist...
I know my table exists, because doing
SELECT count(*) FROM [table]
returns a valid number. Any ideas?
I got the same error though the cause was different. pg_relation_size is case insensitive, so if you have anything other than lower case it will not work out of the box:
postgres=> SELECT pg_size_pretty(pg_total_relation_size('MyTable'));
ERROR: relation "mytable" does not exist
LINE 1: SELECT pg_size_pretty(pg_total_relation_size('mytable...
^
postgres=> SELECT pg_size_pretty(pg_total_relation_size('"MyTable"'));
pg_size_pretty
----------------
225 MB
(1 row)
So in order for this to work in a SELECT statement you need to enclose the table name in quotes:
postgres=> SELECT relname, nspname, pg_size_pretty(pg_relation_size('"' || relname || '"'))
FROM pg_class c LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_table_is_visible(c.oid)
ORDER BY c.relpages DESC;
Try explicitely adding the schema (e.g. 'public') where the table is located in the pg_relation_size call.
Like this (untested):
select pg_relation_size(public.mytablename) from pg_tables