PostgreSQL 的数据字典

本文基于 PostgreSQL 14 写就。

数据字典是来自 Oracle、SQL Server 等数据库的惯常叫法,在 PostgreSQL 中,「数据字典」的正式名称是「System Catalog」。数据字典将关于数据库系统的信息组织到表中,这些表也叫作数据字典表 System Catalog Table,PostgreSQL 将这些数据字典表组织到 Schema 下,由此形成了 PostgreSQL 的数据字典。

数据字典表的 Schema

数据字典表的 Schema 有:

  • pg_catalog
  • information_schema
  • 查询 pg_catalog 中的表时,可以省略 schema,即:

    select distinct schemaname from pg_catalog.pg_tables;
    
    select distinct schemaname from pg_tables;
    

    是等价的。

    查询 information_schema 中的表时,schema 不能省略,如:

    select count(*) from information_schema.sql_parts;
    

    不能写为:

    select count(*) from sql_parts;
    

    pg_catalog 下的数据字典表

    一共 62 张表:

  • pg_statistic
  • pg_type
  • pg_foreign_table
  • pg_authid
  • pg_statistic_ext_data
  • pg_user_mapping
  • pg_subscription
  • pg_attribute
  • pg_proc
  • pg_class
  • pg_attrdef
  • pg_constraint
  • pg_inherits
  • pg_index
  • pg_operator
  • pg_opfamily
  • pg_opclass
  • pg_am
  • pg_amop
  • pg_amproc
  • pg_language
  • pg_largeobject_metadata
  • pg_aggregate
  • pg_statistic_ext
  • pg_rewrite
  • pg_trigger
  • pg_event_trigger
  • pg_description
  • pg_cast
  • pg_enum
  • pg_namespace
  • pg_conversion
  • pg_depend
  • pg_database
  • pg_db_role_setting
  • pg_tablespace
  • pg_auth_members
  • pg_shdepend
  • pg_shdescription
  • pg_ts_config
  • pg_ts_config_map
  • pg_ts_dict
  • pg_ts_parser
  • pg_ts_template
  • pg_extension
  • pg_foreign_data_wrapper
  • pg_foreign_server
  • pg_policy
  • pg_replication_origin
  • pg_default_acl
  • pg_init_privs
  • pg_seclabel
  • pg_shseclabel
  • pg_collation
  • pg_partitioned_table
  • pg_range
  • pg_transform
  • pg_sequence
  • pg_publication
  • pg_publication_rel
  • pg_subscription_rel
  • pg_largeobject
  • information_schema 下的数据字典表

    一共 4 张表:

  • sql_parts
  • sql_implementation_info
  • sql_features
  • sql_sizing
  • 查看表结构

    MySQL 中可以通过 desc <table_name> 查看表结构,在 PostgreSQL 中通过如下命令查看表结构:

    \d <table_name>