如何以编程方式找出psql命令“\ d +”的查询

时间:2014-01-15 11:21:23

标签: postgresql psql npgsql

我正在寻找一种以编程方式生成postgreSQL数据库统计信息的解决方案。 psql命令“\ d +”正是我需要的(输出方式),但到目前为止我没有找到一种方法以编程方式检索这个相同的数据(通过使用C#和npgsql.dll)。有什么建议?提前谢谢!

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

显然,找到这个的规范方法是使用-E选项启动psql。

然后它会吐出它正在使用的查询(或者它在内部运行的任何查询)。在这种情况下,它会吐出这个:

=> \d test_int;
********* QUERY **********
SELECT c.oid,
  n.nspname,
  c.relname
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ '^(test_int)$'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3;
**************************

********* QUERY **********
SELECT c.relchecks, c.relkind, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhasoids, '', c.reltablespace, CASE WHEN c.reloftype = 0 THEN '' ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text END, c.relpersistence
FROM pg_catalog.pg_class c
 LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
WHERE c.oid = '57695';
**************************

********* QUERY **********
SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
   FROM pg_catalog.pg_attrdef d
   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
  a.attnotnull, a.attnum,
  (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t
   WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) AS attcollation,
  NULL AS indexdef,
  NULL AS attfdwoptions
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = '57695' AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
**************************

********* QUERY **********
SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhparent AND i.inhrelid = '57695' ORDER BY inhseqno;
**************************

********* QUERY **********
SELECT c.oid::pg_catalog.regclass FROM pg_catalog.pg_class c, pg_catalog.pg_inherits i WHERE c.oid=i.inhrelid AND i.inhparent = '57695' ORDER BY c.oid::pg_catalog.regclass::pg_catalog.text;
**************************

Table "name_space.test_int"
 Column |  Type   | Modifiers 
--------+---------+-----------
 yo     | integer | 

这意味着它从第一个查询中获取表的oid,然后使用它来查找其余查询中的各种属性。