Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - Determine column storage type

Tags:

postgresql

I've been reading a lot about PostgreSQL's TOAST, and there's one thing I seem to be missing. They mention in the documentation that, "there are four different strategies for storing TOAST-able columns on disk," those being: PLAIN, EXTENDED, EXTERNAL, and MAIN. They also have a very clear way to define which strategy to use for your column, which can be found here. Essentially, it would be something like this:

ALTER TABLE table_name ALTER COLUMN column_name SET STORAGE EXTERNAL

The one thing I don't see is how to easily retrieve that setting. My question is, is there a simple way (either through commands or pgAdmin) to retrieve the storage strategy being used by a column?

like image 555
Francis Bartkowiak Avatar asked Apr 20 '18 18:04

Francis Bartkowiak


1 Answers

This is stored pg_attribute.attstorage, e.g.:

select att.attname, 
       case att.attstorage
          when 'p' then 'plain'
          when 'm' then 'main'
          when 'e' then 'external'
          when 'x' then 'extended'
       end as attstorage
from pg_attribute att  
  join pg_class tbl on tbl.oid = att.attrelid   
  join pg_namespace ns on tbl.relnamespace = ns.oid   
where tbl.relname = 'table_name'
  and ns.nspname = 'public'
  and not att.attisdropped;

Note that attstorage is only valid if attlen is > -1

like image 125
a_horse_with_no_name Avatar answered Nov 07 '22 12:11

a_horse_with_no_name