Given a table created as so...

CREATE TABLE measurement (
    city_id         int not null,
    logdate         date not null,
    peaktemp        int,
    unitsales       int

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)

  • @McNets - "select * from pg_class where relkind='p'" gives me those tables that are partitioned, it doesn't tell which column they've been partitioned on (not that I can see anyhow!) – ConanTheGerbil Nov 24 '18 at 16:14

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:

    par.relnamespace::regnamespace::text as schema, 
    par.relname as table_name, 
    partnatts as num_columns,
         case partstrat 
              when 'l' then 'list' 
              when 'r' then 'range' end as partition_strategy,
         unnest(partattrs) column_index
         pg_partitioned_table) pt 
    pg_class par 
    par.oid = pt.partrelid
    information_schema.columns col
    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<>fiddle here


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.                                           |
| improve this answer | |
  • quote "I'm not a PostgreSQL pro" consider yourself promoted! – ConanTheGerbil Nov 24 '18 at 17:21
  • I'm glad to help. – McNets Nov 24 '18 at 17:22

The answer by McNets already helps, but here is a query that produces slightly prettier output:

select c.relnamespace::regnamespace::text as schema,
       c.relname as table_name, 
       pg_get_partkeydef(c.oid) as partition_key
from   pg_class c
where  c.relkind = 'p';

Below is output that the above query produces:

 schema │ table_name  │      partition_key       
 public │ measurement │ RANGE (city_id, logdate)
(1 row)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.