Postgresql
如何辨識用於從 Postgres 系統目錄中分區表的列
鑑於這樣創建的表…
CREATE TABLE measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (logdate);
如何確定它在哪一列上進行了分區?- 在這種情況下,’logdate’ 僅通過查詢 postgres 目錄。
我查看了目錄中明顯的位置(pg_class,pg_index),但沒有任何結果。
(使用版本 10.5)
我不是 PostgreSQL 專業人士,但稍微探勘一下,我已經建立了一個可能對您有所幫助的解決方案。(僅適用於版本 10 或更高版本)
首先,我通過在分區定義中添加兩列來稍微修改您的表(只是為了向您展示最終結果):
CREATE TABLE measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (city_id,logdate);
這是我的解決方案:
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;
架構 | 表名 | 列數 | 列索引 | 列名 :-------------------------- | :---------- | ----------: | -----------: | :---------- fiddle_ctcqwfrzpcyngmgnqkdy | 測量 | 2 | 1 | city_id fiddle_ctcqwfrzpcyngmgnqkdy | 測量 | 2 | 2 | 日誌日期
db<>在這裡擺弄
pg_partitioned_table
目錄 pg_partitioned_table 儲存有關表如何分區的資訊。
取消嵌套
partattrs
可以獲得分區中涉及的每一行的列索引。然後你可以加入information_schema.columns
只是為了檢索每列的名稱。+-----------+------------+---------------------+-------------------------------------------------------------| | 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. | +-----------+------------+---------------------+-------------------------------------------------------------|