How to identify the column used to partition a table from the Postgres system catalogs Given a table created as so...

CREATE TABLE measurement (
    city_id         int not null,
    logdate         date not null,
    peaktemp        int,
    unitsales       int
) PARTITION BY RANGE (logdate);


How do I identify which column it's been partitioned on? - in this case 'logdate' solely by querying the postgres catalog.

I've looked in the obvious places in the catalog (pg_class, pg_index) but nothing springs out.

(Using version 10.5)
I'm not a PostgreSQL pro, but digging a bit I've founded a solution that perhaps can help you. (Works only for version 10 or above)

First I've slightly modified you table by adding two columns to the partition definition (just to show you the final result):

CREATE TABLE measurement (
    city_id         int not null,
    logdate         date not null,
    peaktemp        int,
    unitsales       int
) PARTITION BY RANGE (city_id,logdate);


This is my solution:




select 
    par.relnamespace::regnamespace::text as schema, 
    par.relname as table_name, 
    partnatts as num_columns,
    column_index,
    col.column_name
from   
    (select
         partrelid,
         partnatts,
         case partstrat 
              when 'l' then 'list' 
              when 'r' then 'range' end as partition_strategy,
         unnest(partattrs) column_index
     from
         pg_partitioned_table) pt 
join   
    pg_class par 
on     
    par.oid = pt.partrelid
join
    information_schema.columns col
on  
    col.table_schema = par.relnamespace::regnamespace::text
    and col.table_name = par.relname
    and ordinal_position = pt.column_index;



schema                      | table_name  | num_columns | column_index | column_name
:-------------------------- | :---------- | ----------: | -----------: | :----------
fiddle_ctcqwfrzpcyngmgnqkdy | measurement |           2 |            1 | city_id    
fiddle_ctcqwfrzpcyngmgnqkdy | measurement |           2 |            2 | logdate    



db&lt;>fiddle here

pg_partitioned_table


  The catalog pg_partitioned_table stores information about how tables
  are partitioned.


Unnesting partattrs you can get column index of each row involved in the partition. Then you can join information_schema.columns just to retrieve the name of every column.

+-----------+------------+---------------------+-------------------------------------------------------------|
| partattrs | int2vector | pg_attribute.attnum | This is an array of partnatts values that indicate          |
|           |            |                     | which table columns are part of the partition key.          |
|           |            |                     | For example, a value of 1 3 would mean that the first       |
|           |            |                     | and the third table columns make up the partition key.      |
|           |            |                     | A zero in this array indicates that the corresponding       |
|           |            |                     | partition key column is an expression, rather than a simple |
|           |            |                     | column reference.                                           |
+-----------+------------+---------------------+-------------------------------------------------------------|