0

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?

2

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Thanks, but I only need to get the size of the files into the table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.