`
lovejuan1314
  • 浏览: 342046 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

PostgreSQL 系统表查看系统信息

 
阅读更多
摘自http://www.alberton.info/postgresql_meta_info.html
测试数据
-- sample data to test PostgreSQL INFORMATION_SCHEMA
 
-- TABLE TEST
CREATE TABLE TEST (
  TEST_NAME CHAR(30) NOT NULL,
  TEST_ID INTEGER DEFAULT '0' NOT NULL,
  TEST_DATE TIMESTAMP NOT NULL
);
ALTER TABLE TEST ADD CONSTRAINT PK_TEST PRIMARY KEY (TEST_ID);
 
-- TABLE TEST2 with some CONSTRAINTs and an INDEX
CREATE TABLE TEST2 (
  ID INTEGER NOT NULL,
  FIELD1 INTEGER,
  FIELD2 CHAR(15),
  FIELD3 VARCHAR(50),
  FIELD4 INTEGER,
  FIELD5 INTEGER,
  ID2 INTEGER NOT NULL
);
ALTER TABLE TEST2 ADD CONSTRAINT PK_TEST2 PRIMARY KEY (ID2);
ALTER TABLE TEST2 ADD CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1);
ALTER TABLE TEST2 ADD CONSTRAINT TEST2_FIELD4_IDX UNIQUE (FIELD4);
CREATE INDEX TEST2_FIELD5_IDX ON TEST2(FIELD5);
 
-- TABLE NUMBERS
CREATE TABLE NUMBERS (
  NUMBER INTEGER DEFAULT '0' NOT NULL,
  EN CHAR(100) NOT NULL,
  FR CHAR(100) NOT NULL
);
 
-- TABLE NEWTABLE
CREATE TABLE NEWTABLE (
  ID INT DEFAULT 0 NOT NULL,
  SOMENAME VARCHAR (12),
  SOMEDATE TIMESTAMP NOT NULL
);
ALTER TABLE NEWTABLE ADD CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID);
CREATE SEQUENCE NEWTABLE_SEQ INCREMENT 1 START 1;
 
-- VIEW on TEST
CREATE VIEW "testview"(
  TEST_NAME,
  TEST_ID,
  TEST_DATE
) AS
SELECT *
FROM TEST
WHERE TEST_NAME LIKE 't%';
 
-- VIEW on NUMBERS
CREATE VIEW "numbersview"(
  NUMBER,
  TRANS_EN,
  TRANS_FR
) AS
SELECT *
FROM NUMBERS
WHERE NUMBER > 100;
 
-- TRIGGER on NEWTABLE
CREATE FUNCTION add_stamp() RETURNS OPAQUE AS '
    BEGIN
      IF (NEW.somedate IS NULL OR NEW.somedate = 0) THEN
        NEW.somedate := CURRENT_TIMESTAMP;
        RETURN NEW;
      END IF;
    END;
' LANGUAGE 'plpgsql';
 
CREATE TRIGGER ADDCURRENTDATE
BEFORE INSERT OR UPDATE
ON newtable FOR EACH ROW
  EXECUTE PROCEDURE add_stamp();
 
-- TABLEs for testing CONSTRAINTs
CREATE TABLE testconstraints (
  someid integer NOT NULL,
  somename character varying(10) NOT NULL,
  CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid)
);
CREATE TABLE testconstraints2 (
  ext_id integer NOT NULL,
  modified date,
  uniquefield character varying(10) NOT NULL,
  usraction integer NOT NULL,
  CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id)
      REFERENCES testconstraints (someid) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT unique_2_fields_idx UNIQUE (modified, usraction),
  CONSTRAINT uniquefld_idx UNIQUE (uniquefield)
);

列出所有数据库中的表名
SELECT relname
  FROM pg_class
 WHERE relname !~ '^(pg_|sql_)'
   AND relkind = 'r';
<!--
SELECT c.relname AS "Name"
  FROM pg_class c, pg_user u
 WHERE c.relowner = u.usesysid
   AND c.relkind = 'r'
   AND NOT EXISTS (
       SELECT 1
         FROM pg_views
        WHERE viewname = c.relname
       )
   AND c.relname !~ '^(pg_|sql_)'
UNION
SELECT c.relname AS "Name"
  FROM pg_class c
 WHERE c.relkind = 'r'
   AND NOT EXISTS (
       SELECT 1
         FROM pg_views
        WHERE viewname = c.relname
       )
   AND NOT EXISTS (
       SELECT 1
         FROM pg_user
        WHERE usesysid = c.relowner
       )
   AND c.relname !~ '^pg_';
--> 
-- using INFORMATION_SCHEMA:
 
SELECT table_name
  FROM information_schema.tables
 WHERE table_type = 'BASE TABLE'
   AND table_schema NOT IN
       ('pg_catalog', 'information_schema');

列出所有视图
-- with postgresql 7.2:
 
SELECT viewname
  FROM pg_views
 WHERE viewname !~ '^pg_';
 
-- with postgresql 7.4 and later:
 
SELECT viewname
  FROM pg_views
 WHERE schemaname NOT IN
       ('pg_catalog', 'information_schema')
   AND viewname !~ '^pg_';
 
-- using INFORMATION_SCHEMA:
 
SELECT table_name
  FROM information_schema.tables
 WHERE table_type = 'VIEW'
   AND table_schema NOT IN
       ('pg_catalog', 'information_schema')
   AND table_name !~ '^pg_';
 
-- or
 
SELECT table_name
  FROM information_schema.views
 WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
   AND table_name !~ '^pg_';
<!--
# show only the VIEWs referencing a given table
 
      SELECT viewname
        FROM pg_views
NATURAL JOIN pg_tables
       WHERE tablename ='test';
-->

列出所有用户
SELECT usename
  FROM pg_user;

列出某表中得所有字段
SELECT a.attname
  FROM pg_class c, pg_attribute a, pg_type t
 WHERE c.relname = 'test2'
   AND a.attnum > 0
   AND a.attrelid = c.oid
   AND a.atttypid = t.oid
 
-- with INFORMATION_SCHEMA:
 
SELECT column_name
  FROM information_schema.columns
 WHERE table_name = 'test2';

列出某表字段的信息
SELECT a.attnum AS ordinal_position,
         a.attname AS column_name,
         t.typname AS data_type,
         a.attlen AS character_maximum_length,
         a.atttypmod AS modifier,
         a.attnotnull AS notnull,
         a.atthasdef AS hasdefault
    FROM pg_class c,
         pg_attribute a,
         pg_type t
   WHERE c.relname = 'test2'
     AND a.attnum > 0
     AND a.attrelid = c.oid
     AND a.atttypid = t.oid
ORDER BY a.attnum;
 
-- with INFORMATION_SCHEMA:
 
  SELECT ordinal_position,
         column_name,
         data_type,
         column_default,
         is_nullable,
         character_maximum_length,
         numeric_precision
    FROM information_schema.columns
   WHERE table_name = 'test2'
ORDER BY ordinal_position;

List INDICES

Here's the query that will return the names of the INDICES defined in the TEST2 table. Unfortunately I have no idea how to extract them from the INFORMATION_SCHEMA. If you do, please let me know.
NB: the CONSTRAINTs are not listed
SELECT relname
  FROM pg_class
 WHERE oid IN (
    SELECT indexrelid
      FROM pg_index, pg_class
     WHERE pg_class.relname='test2'
       AND pg_class.oid=pg_index.indrelid
       AND indisunique != 't'
       AND indisprimary != 't'
       );


列出表的索引信息
SELECT relname, indkey
  FROM pg_class, pg_index
 WHERE pg_class.oid = pg_index.indexrelid
   AND pg_class.oid IN (
    SELECT indexrelid
      FROM pg_index, pg_class
     WHERE pg_class.relname='test2'
       AND pg_class.oid=pg_index.indrelid
       AND indisunique != 't'
       AND indisprimary != 't'
);

SELECT t.relname, a.attname, a.attnum
     FROM pg_index c
LEFT JOIN pg_class t
       ON c.indrelid  = t.oid
LEFT JOIN pg_attribute a
       ON a.attrelid = t.oid
      AND a.attnum = ANY(indkey)
    WHERE t.relname = 'test2'
      AND a.attnum = 6; -- this is the index key

列出表的约束
SELECT c.conname AS constraint_name,
          CASE c.contype
            WHEN 'c' THEN 'CHECK'
            WHEN 'f' THEN 'FOREIGN KEY'
            WHEN 'p' THEN 'PRIMARY KEY'
            WHEN 'u' THEN 'UNIQUE'
          END AS "constraint_type",
          CASE WHEN c.condeferrable = 'f' THEN 0 ELSE 1 END AS is_deferrable,
          CASE WHEN c.condeferred = 'f' THEN 0 ELSE 1 END AS is_deferred,
          t.relname AS table_name,
          array_to_string(c.conkey, ' ') AS constraint_key,
          CASE confupdtype
            WHEN 'a' THEN 'NO ACTION'
            WHEN 'r' THEN 'RESTRICT'
            WHEN 'c' THEN 'CASCADE'
            WHEN 'n' THEN 'SET NULL'
            WHEN 'd' THEN 'SET DEFAULT'
          END AS on_update,
          CASE confdeltype
            WHEN 'a' THEN 'NO ACTION'
            WHEN 'r' THEN 'RESTRICT'
            WHEN 'c' THEN 'CASCADE'
            WHEN 'n' THEN 'SET NULL'
            WHEN 'd' THEN 'SET DEFAULT'
          END AS on_delete,
          CASE confmatchtype
            WHEN 'u' THEN 'UNSPECIFIED'
            WHEN 'f' THEN 'FULL'
            WHEN 'p' THEN 'PARTIAL'
          END AS match_type,
          t2.relname AS references_table,
          array_to_string(c.confkey, ' ') AS fk_constraint_key
     FROM pg_constraint c
LEFT JOIN pg_class t  ON c.conrelid  = t.oid
LEFT JOIN pg_class t2 ON c.confrelid = t2.oid
    WHERE t.relname = 'testconstraints2'
     AND c.conname = 'testconstraints_id_fk';
     
-- with INFORMATION_SCHEMA:
 
   SELECT tc.constraint_name,
          tc.constraint_type,
          tc.table_name,
          kcu.column_name,
	  tc.is_deferrable,
          tc.initially_deferred,
          rc.match_option AS match_type,
          rc.update_rule AS on_update,
          rc.delete_rule AS on_delete,
          ccu.table_name AS references_table,
          ccu.column_name AS references_field
     FROM information_schema.table_constraints tc
LEFT JOIN information_schema.key_column_usage kcu
       ON tc.constraint_catalog = kcu.constraint_catalog
      AND tc.constraint_schema = kcu.constraint_schema
      AND tc.constraint_name = kcu.constraint_name
LEFT JOIN information_schema.referential_constraints rc
       ON tc.constraint_catalog = rc.constraint_catalog
      AND tc.constraint_schema = rc.constraint_schema
      AND tc.constraint_name = rc.constraint_name
LEFT JOIN information_schema.constraint_column_usage ccu
       ON rc.unique_constraint_catalog = ccu.constraint_catalog
      AND rc.unique_constraint_schema = ccu.constraint_schema
      AND rc.unique_constraint_name = ccu.constraint_name
    WHERE tc.table_name = 'testconstraints2'
      AND tc.constraint_name = 'testconstraints_id_fk';


列出所有序列
SELECT relname
  FROM pg_class
 WHERE relkind = 'S'
   AND relnamespace IN (
        SELECT oid
          FROM pg_namespace
         WHERE nspname NOT LIKE 'pg_%'
           AND nspname != 'information_schema'
);


列出所有触发器
SELECT trg.tgname AS trigger_name
  FROM pg_trigger trg, pg_class tbl
 WHERE trg.tgrelid = tbl.oid
   AND tbl.relname !~ '^pg_';
-- or
SELECT tgname AS trigger_name
  FROM pg_trigger
 WHERE tgname !~ '^pg_';
 
-- with INFORMATION_SCHEMA:
 
SELECT DISTINCT trigger_name
  FROM information_schema.triggers
 WHERE trigger_schema NOT IN
       ('pg_catalog', 'information_schema');

SELECT trg.tgname AS trigger_name
  FROM pg_trigger trg, pg_class tbl
 WHERE trg.tgrelid = tbl.oid
   AND tbl.relname = 'newtable';
 
-- with INFORMATION_SCHEMA:
 
SELECT DISTINCT trigger_name
  FROM information_schema.triggers
 WHERE event_object_table = 'newtable'
   AND trigger_schema NOT IN
       ('pg_catalog', 'information_schema');

列出所有触发器的信息
SELECT trg.tgname AS trigger_name,
       tbl.relname AS table_name,
       p.proname AS function_name,
       CASE trg.tgtype & cast(2 as int2)
         WHEN 0 THEN 'AFTER'
         ELSE 'BEFORE'
       END AS trigger_type,
       CASE trg.tgtype & cast(28 as int2)
         WHEN 16 THEN 'UPDATE'
         WHEN  8 THEN 'DELETE'
         WHEN  4 THEN 'INSERT'
         WHEN 20 THEN 'INSERT, UPDATE'
         WHEN 28 THEN 'INSERT, UPDATE, DELETE'
         WHEN 24 THEN 'UPDATE, DELETE'
         WHEN 12 THEN 'INSERT, DELETE'
       END AS trigger_event,
       CASE trg.tgtype & cast(1 as int2)
         WHEN 0 THEN 'STATEMENT'
         ELSE 'ROW'
       END AS action_orientation
  FROM pg_trigger trg,
       pg_class tbl,
       pg_proc p
 WHERE trg.tgrelid = tbl.oid
   AND trg.tgfoid = p.oid
   AND tbl.relname !~ '^pg_';
 
-- with INFORMATION_SCHEMA:
 
SELECT *
  FROM information_schema.triggers
 WHERE trigger_schema NOT IN
       ('pg_catalog', 'information_schema');


列出所有函数
SELECT proname
  FROM pg_proc pr,
       pg_type tp
 WHERE tp.oid = pr.prorettype
   AND pr.proisagg = FALSE
   AND tp.typname <> 'trigger'
   AND pr.pronamespace IN (
       SELECT oid
         FROM pg_namespace
        WHERE nspname NOT LIKE 'pg_%'
          AND nspname != 'information_schema'
);
 
-- with INFORMATION_SCHEMA:
 
SELECT routine_name
  FROM information_schema.routines
 WHERE specific_schema NOT IN
       ('pg_catalog', 'information_schema')
   AND type_udt_name != 'trigger';


Albe Laurenz sent me the following function that is even more informative: for a function name and schema, it selects the position in the argument list, the direction, the name and the data-type of each argument. This procedure requires PostgreSQL 8.1 or later.

CREATE OR REPLACE FUNCTION public.function_args(
  IN funcname character varying,
  IN schema character varying,
  OUT pos integer,
  OUT direction character,
  OUT argname character varying,
  OUT datatype character varying)
RETURNS SETOF RECORD AS $$DECLARE
  rettype character varying;
  argtypes oidvector;
  allargtypes oid[];
  argmodes "char"[];
  argnames text[];
  mini integer;
  maxi integer;
BEGIN
  /* get object ID of function */
  SELECT INTO rettype, argtypes, allargtypes, argmodes, argnames
         CASE
         WHEN pg_proc.proretset
         THEN 'setof ' || pg_catalog.format_type(pg_proc.prorettype, NULL)
         ELSE pg_catalog.format_type(pg_proc.prorettype, NULL) END,
         pg_proc.proargtypes,
         pg_proc.proallargtypes,
         pg_proc.proargmodes,
         pg_proc.proargnames
    FROM pg_catalog.pg_proc
         JOIN pg_catalog.pg_namespace
         ON (pg_proc.pronamespace = pg_namespace.oid)
   WHERE pg_proc.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
     AND (pg_proc.proargtypes[0] IS NULL
      OR pg_proc.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
     AND NOT pg_proc.proisagg
     AND pg_proc.proname = funcname
     AND pg_namespace.nspname = schema
     AND pg_catalog.pg_function_is_visible(pg_proc.oid);
 
  /* bail out if not found */
  IF NOT FOUND THEN
    RETURN;
  END IF;
 
  /* return a row for the return value */
  pos = 0;
  direction = 'o'::char;
  argname = 'RETURN VALUE';
  datatype = rettype;
  RETURN NEXT;
 
  /* unfortunately allargtypes is NULL if there are no OUT parameters */
  IF allargtypes IS NULL THEN
    mini = array_lower(argtypes, 1); maxi = array_upper(argtypes, 1);
  ELSE
    mini = array_lower(allargtypes, 1); maxi = array_upper(allargtypes, 1);
  END IF;
  IF maxi < mini THEN RETURN; END IF;
 
  /* loop all the arguments */
  FOR i IN mini .. maxi LOOP
    pos = i - mini + 1;
    IF argnames IS NULL THEN
      argname = NULL;
    ELSE
      argname = argnames[pos];
    END IF;
    IF allargtypes IS NULL THEN
      direction = 'i'::char;
      datatype = pg_catalog.format_type(argtypes[i], NULL);
    ELSE
      direction = argmodes[i];
      datatype = pg_catalog.format_type(allargtypes[i], NULL);
    END IF;
    RETURN NEXT;
  END LOOP;
 
  RETURN;
END;$$ LANGUAGE plpgsql STABLE STRICT SECURITY INVOKER;
COMMENT ON FUNCTION public.function_args(character varying, character
varying)
IS $$For a function name and schema, this procedure selects for each
argument the following data:
- position in the argument list (0 for the return value)
- direction 'i', 'o', or 'b'
- name (NULL if not defined)
- data type$$;


列出所有存储过程
SELECT p.proname AS procedure_name,
          p.pronargs AS num_args,
          t1.typname AS return_type,
          a.rolname AS procedure_owner,
          l.lanname AS language_type,
          p.proargtypes AS argument_types_oids,
          prosrc AS body
     FROM pg_proc p
LEFT JOIN pg_type t1 ON p.prorettype=t1.oid   
LEFT JOIN pg_authid a ON p.proowner=a.oid 
LEFT JOIN pg_language l ON p.prolang=l.oid
    WHERE proname = :PROCEDURE_NAME;
分享到:
评论

相关推荐

    PostgreSQL获取表名和字段名

    要获取特定表的所有字段信息,可以使用`pg_class`和`pg_attribute`系统目录视图。以下查询展示了名为`tablename`的表的所有字段,包括其注释(如果存在)、数据类型、字段名称和是否允许为空: ```sql SELECT ...

    PostgreSQL中文手册9.2

    十二、系统信息 十二、系统信息 十二、系统信息 函数: 函数: .38 PostgreSQL PostgreSQL PostgreSQL学习手册 学习手册 学习手册 (索引 ) 42 一、索引的类型: 一、索引的类型: 一、索引的类型: 一、索引的类型:...

    postgresql 资源信息

    PostgreSQL是一种强大的开源关系数据库管理系统,与Oracle等商业数据库系统相比,它提供了丰富的功能和优秀的性能。在深入探讨PostgreSQL资源信息之前,我们先理解一下标题和描述所提及的关键点——“类型转换函数”...

    许中清-PostgreSQL表分区实践

    许中清在2014年PostgreSQL中国大会上分享了关于PostgreSQL的原生表分区方案,这是数据库管理领域中一个非常重要的知识点,尤其适用于大型数据库系统的维护和优化。表分区是一种数据库架构优化技术,它允许将一个大表...

    postgresql表分区

    PostgreSQL 作为一款功能强大的开源关系型数据库系统,支持多种类型的表分区机制。本文将详细介绍 PostgreSQL 中的表分区技术,包括其工作原理、实现方法以及示例演示。 #### 二、表分区原理与实现 ##### 2.1 表...

    Postgresql-10安装包

    PostgreSQL 是一款强大的开源关系型数据库管理系统,具有高度的稳定性和可扩展性,被广泛应用于各种规模的企业和项目。本文将详细介绍如何使用提供的 PostgreSQL-10 安装包进行安装,以及在安装过程中可能遇到的关键...

    postgresql

    例如,PostGIS是PostgreSQL的一个扩展,它为数据库添加了空间和地理数据支持,使PostgreSQL成为处理地理信息系统(GIS)数据的强大平台。 在压缩包子文件的文件名称列表中,“postgresql-8.1.3-1”可能是PostgreSQL...

    PostgreSQL监控系统OPM.zip

    **OPM:PostgreSQL的开放监控系统** OPM(Open PostgreSQL Monitoring)是一个专门设计用于监控PostgreSQL数据库性能的开源系统。这个系统旨在提供全面、实时的数据库健康状况和性能指标,帮助DBA(数据库管理员)...

    PostgreSQL教程.pdf

    在PostgreSQL中,一个重要的特性是表的继承功能,它允许创建一个表(子表)继承另一个表(父表)的特性。此外,PostgreSQL提供了多种扩展机制,通过安装扩展模块可以为系统增加额外的功能。 PostgreSQL的工具包括...

    PostgreSQL 12.2 安装手册

    上传完成后,以 root 用户登录操作系统,进入 root 目录查看。 2. 创建 PostgreSQL 安装目录 创建 PostgreSQL 安装目录:mkdir /pg_sql 3. 创建 postgres 用户 创建 postgres 用户是为了安全考虑,postgres 用户...

    Python-实现轻松创建PostgreSQL的分区表DDL

    PostgreSQL作为一个强大的开源关系型数据库管理系统,支持多种分区策略,如范围、列表、哈希等。本文将探讨如何使用Python来实现PostgreSQL的分区表DDL(数据定义语言)的自动化创建。 首先,让我们了解分区表的...

    PostgreSQL教程(十五):系统表详解

    本教程将详细讲解几个重要的系统表,帮助你理解PostgreSQL如何管理和维护这些信息。 首先,我们来看`pg_class`表。`pg_class`是PostgreSQL中最核心的系统表之一,它记录了所有关系对象(包括数据表、索引、视图、...

    如何恢复PostgreSQL数据库

    PostgreSQL 是一个功能强大且广泛使用的开源关系数据库管理系统,但是在实际应用中,数据库崩溃或无法启动的情况时有发生。这时,如何恢复 PostgreSQL 数据库变得非常重要。下面将详细介绍如何恢复 PostgreSQL ...

    PostgreSQL(postgresql-14.1.tar.bz2)

    PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现...

    centos操作系统安装postgresql+postgis部署配置

    基于pg库进行地理空间数据存储的开源数据库技术方案,只有正确安装改库才能继续后续空间数据正常管理、空间数据很好的分析,从而实现系统中正常运行,postgresql与postgis对应版本问题或升级也可参考类似方法进行...

    postgresql离线一键安装

    5. initdb_postgres.sh - 初始化数据库实例的脚本,会创建默认的数据目录结构和系统表。 6. pgsql—shell一键安装.doc - 文档可能详细解释了安装过程和使用方法。 7. env_pgsql.sh - 可能用于设置环境变量,确保...

    PostgreSQL(postgresql-14.1.tar.gz)

    PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现...

    PostgreSQL_8.2.3.rar_postgresql_windows 8

    10. **扩展与模块**:PostgreSQL 8.2.3支持许多可扩展模块,如全文搜索、GIS(地理信息系统)支持等。文档会介绍如何安装和使用这些扩展。 通过阅读“PostgreSQL_8.2.3_中文文档.chm”,无论是初学者还是有经验的...

    PGAdmin系统管理PostgreSQL 中文手册

    **PGAdmin系统管理PostgreSQL中文手册** 这本中文手册详细阐述了如何利用pgAdmin这一强大的图形用户界面工具来管理和操作PostgreSQL数据库系统。pgAdmin是一款免费的开源软件,专为PostgreSQL设计,提供了直观的...

Global site tag (gtag.js) - Google Analytics