cancel
Showing results for 
Search instead for 
Did you mean: 

Getting the data size of columns in bytes

You can use thepg_column_size(<column_name>) function to find out the size of a column in bytes.


For example, if we create the following table:

edb=# CREATE TABLE test (
id numeric(10,0),
text varchar(40),
roll varchar(10)
);
CREATE TABLE

And then we can insert some test data:

edb=# INSERT INTO test (id, text, roll)
VALUES (1, 'Red', 10), (2, 'Yellow', 55), (3, 'Turquoise', 101), (4, 'Blue', 30);
INSERT 0 4

 

We can then use the pg_column_size() function to get the column width in bytes:

edb=# SELECT pg_column_size(id) length_of_id_col, pg_column_size(text) length_of_text_col FROM test; 
length_of_id_col | length_of_text_col
------------------+--------------------
5 | 4
5 | 7
5 | 10
5 | 5

 

As you can see, the amount of bytes used to store each value is shown here in the results.

Version history
Revision #:
6 of 6
Last update:
‎07-10-2019 07:33 PM
Updated by: