[pgsql-jp: 27096] Re: システムカタログ

sugita @ sra.co.jp sugita @ sra.co.jp
2002年 8月 21日 (水) 12:33:33 JST


  杉田です。

From: 吉塚 陽子 <yoshizuka.wakaba @ nikon.co.jp>
Date: Wed, 21 Aug 2002 11:40:33 +0900

;;; todo表(例)の属性を見るには、以下のようにしますよね。
;;; # 〓d todo
;;;                                  Table "todo"
;;;    Attribute   |   Type    |                      Modifier
;;; ---------------+-----------+------------------------------------------------
;;; ----
;;;  todo_id       | integer   | not null default
;;; nextval('todo_todo_id_seq'::text)
;;;  uname         | text      | not null
;;;  todo_name     | text      | not null
;;;  priority_code | integer   | not null
;;;  end_time      | timestamp |
;;;  memo          | lztext    |
;;;  ended         | integer   |
;;;  changed       | timestamp | default now()
;;; Index: todo_pkey
;;; 
;;; この出力を、システムカタログを使って行いたいのですが、できるでしょうか?
;;; デフォルト値を出力するには、pg_attdef を
;;; not null を出力するには、pg_attribute を使用すればいいのはわかるのですが、
;;; これらのシステムカタログでは、Tableを特定するのは、adrelid(pg_attdef)と
;;; attrelid(pg_attribute)で、
;;; この数値とTable名を関係つけるシステムカタログがないように思えるんですが。

  psql -E と起動すると、次のようになり、表示のための SQL が分かるようになりま
す。

    ********* QUERY *********
    SELECT relhasindex, relkind, relchecks, reltriggers, relhasrules
    FROM pg_class WHERE relname='test'
    *************************

    ********* QUERY *********
    SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
    FROM pg_class c, pg_attribute a, pg_type t
    WHERE c.relname = 'test'
      AND a.attnum > 0 AND a.attrelid = c.oid AND a.atttypid = t.oid
    ORDER BY a.attnum
    *************************

    ********* QUERY *********
    SELECT substring(d.adsrc for 128) FROM pg_attrdef d, pg_class c
    WHERE c.relname = 'test' AND c.oid = d.adrelid AND d.adnum = 2
    *************************

    ********* QUERY *********
    SELECT c2.relname
    FROM pg_class c, pg_class c2, pg_index i
    WHERE c.relname = 'test' AND c.oid = i.indrelid AND i.indexrelid = c2.oid
    ORDER BY c2.relname
    *************************

	      Table "test"
     Attribute |  Type   | Modifier  
    -----------+---------+-----------
     a         | integer | not null
     b         | integer | default 0
    Index: test_pkey

    =# 

Kenji Sugita





pgsql-jp メーリングリストの案内