问题
I have a PostgreSQL table named census
. I have performed the ANALYSE
command on the table, and the statistics are recorded in pg_stats
.
There are other entries in this pg_stats
from other database tables as can be expected.
However, I wanted to know the space consumed for storing the histogram_bounds
for the census
table alone. Is there a good and fast way for it?
PS: I have tried dumping the pg_stats
table onto the disk to measure the memory using
select * into copy_table from census where tablename='census';
However, it failed because of the pseudo-type anyarray
.
Any ideas there too?
回答1:
In the following I use the table pg_type
and its column typname
for demonstration purposes. Replace these with your table and column name to get the answer for your case (you didn't say which column you are interested in).
You can use the pg_column_size
function to get the size of any
column:
SELECT pg_column_size(histogram_bounds)
FROM pg_stats
WHERE schemaname = 'pg_catalog'
AND tablename = 'pg_type'
AND attname = 'typname';
pg_column_size
----------------
1269
(1 row)
To convert an anyarray
to a regular array, you can first cast it to text
and then to the desired array type:
SELECT histogram_bounds::text::name[] FROM pg_stats ...
If you measure the size of that converted array, you'll notice that it is much bigger than the result above.
The reason is that pg_column_size
measures the actual size on disk, and histogram_bounds
is big enough to be stored out of line in the TOAST table, where it will be compressed. The converted array is not compressed, because it is not stored in a table.
来源:https://stackoverflow.com/questions/52901588/finding-the-histogram-size-for-a-given-table-postgresql