I would like to obtain the byte size of a blob.
I am using Postgresql and would like to obtain the size using an SQL query. Something like this:
SELECT sizeof(field) FROM table;
Is this possible in Postgresql?
Update: I have read the postgresql manual and could not find an appropriate function to calculate the file size. Also, the blob is stored as a large object.
Try
length()
oroctet_length()
This is my solution:
Gives you the size in bytes.
If you want pretty printing:
Not that I've used large objects, but looking at the docs: http://www.postgresql.org/docs/current/interactive/lo-interfaces.html#LO-TELL
I think you have to use the same technique as some file system APIs require: seek to the end, then tell the position. PostgreSQL has SQL functions that appear to wrap the internal C functions. I couldn't find much documentation, but this worked:
Testing it:
It seems the LO functionality is designed to be used mostly through the client or through low-level server programming, but at least they've provided some SQL visible functions for it, which makes the above possible. I did a query for
SELECT relname FROM pg_proc where relname LIKE 'lo%'
to get myself started. Vague memories of C programming and a bit of research for the modex'40000'::int
andSEEK_END = 2
value were needed for the rest!You could change your application to store the size when you create the large object. Otherwise you can use a query such as:
You can use also the large object API functions, as suggested in a previous post, they work ok, but are an order of magnitude slower than the select method suggested above.