Finding the histogram size for a given table PostgreSQL

僤鯓⒐⒋嵵緔 提交于 2021-01-28 11:51:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!