PostgreSQL - 获取物化视图列元数据

And*_*sev 6 postgresql

这类似于物化视图的列数据类型?但我需要更多数据(不仅仅是数据类型).我想对表/视图进行相同类型的查询,但是对于物化视图.

SELECT column_name, data_type, character_maximum_length,
      character_octet_length, numeric_precision, numeric_precision_radix,
     numeric_scale, datetime_precision, interval_type, interval_precision
     FROM information_schema.columns
    WHERE table_schema = '{}'
    AND table_name   = '{}'
    order by ordinal_position
Run Code Online (Sandbox Code Playgroud)

有没有人有这样的东西?pg_attribute中的列名非常神秘.

a_h*_*ame 15

psql使用-E("echo hidden queries")选项运行时,可以轻松检索此类问题的查询.

以下查询应该执行您想要的操作:

SELECT a.attname,
       pg_catalog.format_type(a.atttypid, a.atttypmod),
       a.attnotnull
FROM pg_attribute a
  JOIN pg_class t on a.attrelid = t.oid
  JOIN pg_namespace s on t.relnamespace = s.oid
WHERE a.attnum > 0 
  AND NOT a.attisdropped
  AND t.relname = 'mv_name' --<< replace with the name of the MV 
  AND s.nspname = 'public' --<< change to the schema your MV is in 
ORDER BY a.attnum;
Run Code Online (Sandbox Code Playgroud)

  • @AndreyVykhodtsev:正如我所写:使用“-E”选项运行“psql”,然后使用“\d mv_name”显示列。 (4认同)