2014-01-15 38 views
1

我正在尋找一個解決方案,以編程方式生成postgreSQL數據庫統計。 psql命令「\ d +」正是我所需要的(輸出方式),但到目前爲止,我沒有找到一種方法來以編程方式檢索相同的數據(通過使用C#和npgsql.dll)。有什麼建議麼?提前致謝!如何以編程方式找出哪些查詢psql命令「 d +」確實

+4

您可以使用'-E'選項運行'psql',它會顯示正在使用的語句。 –

回答

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,然後用它來查找在查詢的其他各種屬性。

相關問題