postgresql11 查看表结构和系统视图

postgres=# select * from pg_tables where tablename='t';
 schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public     | t         | postgres   |            | t          | f        | f           | f
(1 row)

postgres-# \d t
                        Table "public.t"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 id     | integer               |           | not null | 
 name   | character varying(20) |           |          | 
Indexes:
    "t_pkey" PRIMARY KEY, btree (id)

sql语句查看表结构
SELECT A
	.attnum,
	A.attname AS field,
	T.typname AS TYPE,
	A.attlen AS LENGTH,
	A.atttypmod AS lengthvar,
	A.attnotnull AS NOTNULL,
	b.description AS COMMENT 
FROM
	pg_class C,
	pg_attribute
	A LEFT OUTER JOIN pg_description b ON A.attrelid = b.objoid 
	AND A.attnum = b.objsubid,
	pg_type T 
WHERE
	C.relname = 't' 
	AND A.attnum > 0 
	AND A.attrelid = C.oid 
	AND A.atttypid = T.oid 
ORDER BY
	A.attnum; 

查看系统视图

postgres-# \df pg_last*
                                                          List of functions
   Schema   |             Name              |     Result data type     |                  Argument data types                  | Type 
------------+-------------------------------+--------------------------+-------------------------------------------------------+------
 pg_catalog | pg_last_committed_xact        | record                   | OUT xid xid, OUT "timestamp" timestamp with time zone | func
 pg_catalog | pg_last_wal_receive_lsn       | pg_lsn                   |                                                       | func
 pg_catalog | pg_last_wal_replay_lsn        | pg_lsn                   |                                                       | func
 pg_catalog | pg_last_xact_replay_timestamp | timestamp with time zone |                                                       | func
(4 rows)

  

系统视图 官方链接

https://www.postgresql.org/docs/current/catalogs.html

生成建表sql语句

 1 CREATE OR REPLACE FUNCTION "public"."showcreatetable"("dbinstancename" varchar, "namespace" varchar, "tablename" varchar, "tenantid" varchar)
 2   RETURNS "pg_catalog"."varchar" AS $BODY$
 3 declare 
 4 tableScript character varying default '';
 5 tableNum int2 ;
 6  
 7 begin
 8 -- check db extist
 9 tableNum:= count(*)    from pg_class  where relname=tablename and relnamespace =(
10             select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
11             );
12 IF tableNum=0  then 
13 return '' ;
14 end if;
15 -- columns
16 tableScript:=tableScript || ' CREATE TABLE IF NOT EXISTS '|| '"sch_'||tenantid||'".'||tablename|| ' ( '|| array_to_string(
17   array(
18 select concat( c1, c2, c3, c4, c5, c6 ) as column_line
19 from (
20   select '"'||column_name||'"' || ' ' || case when data_type='ARRAY' then ltrim(udt_name,'_')||'[]' else data_type end as c1,
21     case when character_maximum_length > 0 then '(' || character_maximum_length || ')' end as c2,
22     case when numeric_precision > 0 and numeric_scale < 1 then null end as c3,
23     case when numeric_precision > 0 and numeric_scale > 0 then null end as c4,
24     case when is_nullable = 'NO' then ' NOT NULL' end as c5,
25     case when column_default is not Null then ' DEFAULT' end || ' ' || replace(column_default, '::character varying', '') as c6
26   from information_schema.columns
27   where table_name = tablename
28     and table_catalog=dbinstancename
29   and table_schema=namespace
30   order by ordinal_position
31 ) as string_columns
32 ),' , ') ||',' ;
33  
34  
35 -- 约束
36 tableScript:= tableScript || array_to_string(
37 array(
38     select concat(' CONSTRAINT ','"'||conname||'"' ,c ,u,p,f)   from (
39         select conname,
40         case when contype='c' then  ' CHECK('|| consrc ||')' end  as c  ,
41         case when contype='u' then  ' UNIQUE('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||')' end as u ,
42         case when contype='p' then ' PRIMARY KEY ('|| ( select findattname(dbinstancename,namespace,tablename,'p') ) ||')' end  as p  ,
43         case when contype='f' then ' FOREIGN KEY('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||') REFERENCES '|| 
44         (select p.relname from pg_class p where p.oid=c.confrelid )  || '('|| ( select findattname(dbinstancename,namespace,tablename,'u') ) ||')' end as  f
45         from pg_constraint c
46         where contype in('u','c','f','p') and conrelid=( 
47             select oid  from pg_class  where relname=tablename and relnamespace =(
48             select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
49             )
50          )
51     ) as t  
52 ) ,',' ) || ' ); '; 
53  
54 -- 
55 /** **/
56 --- 获取非约束索引 column
57 -- CREATE UNIQUE INDEX pg_language_oid_index ON pg_language USING btree (oid); -- table pg_language
58 tableScript:= tableScript || array_to_string(
59     array(
60         select 'CREATE '||case when is_unique_index=true then 'UNIQUE INDEX' else 'INDEX' end ||'"' || indexrelname ||'"' || ' ON ' || '"sch_'||tenantid||'".'||tablename|| ' USING '||index_type|| '(' || attname || ');' from (
61          SELECT 
62             i.relname AS indexrelname ,  x.indkey, 
63             ( select array_to_string (
64             array( 
65                 select a.attname from pg_attribute a where attrelid=c.oid and a.attnum in ( select unnest(x.indkey) )
66  
67                  ) 
68              ,',' ) )as attname, x.indisunique is_unique_index,am.amname index_type
69             
70            FROM pg_class c
71            JOIN pg_index x ON c.oid = x.indrelid
72            JOIN pg_class i ON i.oid = x.indexrelid
73              join pg_am am on am.oid = i.relam
74            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
75            WHERE  c.relname=tablename and i.relname not in
76               ( select constraint_name from information_schema.key_column_usage  where  table_name=tablename  )
77                 and c.relnamespace =(
78             select oid from pg_namespace where nspowner=(select datdba from pg_database where datname=dbinstancename) and nspname = namespace
79             )
80         )as t
81 ) ,'' );
82             
83 
84  
85 return tableScript;
86  
87 end
88 $BODY$
89   LANGUAGE plpgsql VOLATILE
90   COST 100
91  

查看建表语句

select showcreatetable('public','pub_eventlistener');

posted @ 2019-10-12 14:05  刚好遇见Mysql  阅读(2239)  评论(0)    收藏  举报