PostgreSQL-查询所有索引

news/2024/7/9 21:41:50 标签: postgresql

pg_indexes 是一个视图,可以通过它获取某个表的索引信息。pg_indexes的定义如下:

SELECT
	n.nspname AS schemaname,
    c.relname AS tablename,
    i.relname AS indexname,
    t.spcname AS tablespace,
    pg_get_indexdef(i.oid) AS indexdef
FROM pg_index x
    JOIN pg_class c ON c.oid = x.indrelid
    JOIN pg_class i ON i.oid = x.indexrelid
    LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
    LEFT JOIN pg_tablespace t ON t.oid = i.reltablespace
WHERE (c.relkind = ANY (ARRAY['r'::"char", 'm'::"char"])) AND i.relkind = 'i'::"char";

例如从 pg_indexes中获取pg系统表pg_index表的索引信息:

select * from pg_indexes where tablename = 'pg_index';

结果如下:

 schemaname | tablename |         indexname         | tablespace |                                           indexdef
------------+-----------+---------------------------+------------+-----------------------------------------------------------------------------------------------
 pg_catalog | pg_index  | pg_index_indrelid_index   |            | CREATE INDEX pg_index_indrelid_index ON pg_catalog.pg_index USING btree (indrelid)
 pg_catalog | pg_index  | pg_index_indexrelid_index |            | CREATE UNIQUE INDEX pg_index_indexrelid_index ON pg_catalog.pg_index USING btree (indexrelid)
(2 rows)

如果要获取索引的更多属性信息,则需要通过PostgreSQL的系统表 pg_index来获取,pg_index表的定义如下:

                                      Table "pg_catalog.pg_index"
     Column     |     Type     | Collation | Nullable | Default | Storage  | Stats target | Description
----------------+--------------+-----------+----------+---------+----------+--------------+-------------
 indexrelid     | oid          |           | not null |         | plain    |              |
 indrelid       | oid          |           | not null |         | plain    |              |
 indnatts       | smallint     |           | not null |         | plain    |              |
 indisunique    | boolean      |           | not null |         | plain    |              |
 indisprimary   | boolean      |           | not null |         | plain    |              |
 indisexclusion | boolean      |           | not null |         | plain    |              |
 indimmediate   | boolean      |           | not null |         | plain    |              |
 indisclustered | boolean      |           | not null |         | plain    |              |
 indisvalid     | boolean      |           | not null |         | plain    |              |
 indcheckxmin   | boolean      |           | not null |         | plain    |              |
 indisready     | boolean      |           | not null |         | plain    |              |
 indislive      | boolean      |           | not null |         | plain    |              |
 indisreplident | boolean      |           | not null |         | plain    |              |
 indkey         | int2vector   |           | not null |         | plain    |              |
 indcollation   | oidvector    |           | not null |         | plain    |              |
 indclass       | oidvector    |           | not null |         | plain    |              |
 indoption      | int2vector   |           | not null |         | plain    |              |
 indexprs       | pg_node_tree |           |          |         | extended |              |
 indpred        | pg_node_tree |           |          |         | extended |              |
Indexes:
    "pg_index_indexrelid_index" UNIQUE, btree (indexrelid)
    "pg_index_indrelid_index" btree (indrelid)

例如:获取 pg_index 表自身的 index 信息:

select * from pg_index where indrelid in (select oid from pg_class where relname = 'pg_index')

其中 indrelidpg_class表中的oid字段。
查询结果如下:

-[ RECORD 1 ]--+-----
indexrelid     | 2678
indrelid       | 2610
indnatts       | 1
indisunique    | f
indisprimary   | f
indisexclusion | f
indimmediate   | t
indisclustered | f
indisvalid     | t
indcheckxmin   | f
indisready     | t
indislive      | t
indisreplident | f
indkey         | 2
indcollation   | 0
indclass       | 1981
indoption      | 0
indexprs       | 
indpred        | 
-[ RECORD 2 ]--+-----
indexrelid     | 2679
indrelid       | 2610
indnatts       | 1
indisunique    | t
indisprimary   | f
indisexclusion | f
indimmediate   | t
indisclustered | f
indisvalid     | t
indcheckxmin   | f
indisready     | t
indislive      | t
indisreplident | f
indkey         | 1
indcollation   | 0
indclass       | 1981
indoption      | 0
indexprs       | 
indpred        | 

其中indexrelid字段就是系统表 pg_class中的 oid字段。
所以查看系统表pg_index自身的索引名字的SQL如下:

select relname from pg_class where oid in 
    (select indexrelid from pg_index where indrelid in 
        (select oid from pg_class where relname = 'pg_index')
    )

查询结果如下:

          relname          
---------------------------
 pg_index_indexrelid_index
 pg_index_indrelid_index


http://www.niftyadmin.cn/n/788701.html

相关文章

SSM框架外卖订餐系统—— 功能介绍 、搭建

QQ 1274510382 Wechat JNZ_aming 商业互捧 QQ群538250800 技术搞事 QQ群599020441 技术合作 QQ群152889761 加入我们 QQ群649347320 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。 基于万能java 后台脚手架系统jdk1.8eclipsetomcatmysql5.7

Spark架构模型介绍

[sizemedium]集群概述:(一)组件 spark应用作为一系列独立的进程运行在集群上,通过SparkContext来协调,这个对象一般在我们的main程序里面,通常称为驱动程序,特别的,为了能够在集群上…

PostgreSQL-查询表的字段(属性)定义-pg_attribute

pg_attribute表中存储了每个表的属性信息,此表的 pg_attribute.attrelid字段是每个表在对应的 pg_class.oid的值。 例如获取pg_attribute表自己的字段定义信息的命令如下: selectattrelid,attnum,attname,attnotnull frompg_attribute whereattrelid (…

JavaWeb——SSM管理系统脚手架-介绍、原理

QQ 1274510382 Wechat JNZ_aming 商业互捧 QQ群538250800 技术搞事 QQ群599020441 技术合作 QQ群152889761 加入我们 QQ群649347320 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。

PostgreSQL-获取一个表的所有分区表

表的分区关系存储在pg_inherits中,其定义如下: Table "pg_catalog.pg_inherits"Column | Type | Collation | Nullable | Default | Storage | Stats target | Description -----------------------------------------------------------…

git 命令支持的的代码仓库地址

通过 git push --help 命令查看帮助信息中的 URLs 支持信息 . ssh://[user]host.xz[:port]/path/to/repo.git/ . git://host.xz[:port]/path/to/repo.git/ . http[s]://host.xz[:port]/path/to/repo.git/ . ftp[s]://host.xz[:port]/path/to/repo.git/ 可以使用如下命令添加远…

Spark Streaming流式计算的WordCount入门

[sizemedium] Spark Streaming是一种近实时的流式计算模型,它将作业分解成一批一批的短小的批处理任务,然后并行计算,具有可扩展,高容错,高吞吐,实时性高等一系列优点,在某些场景可达到与Storm一…

Java微服务——商城项目工程搭建

QQ 1274510382 Wechat JNZ_aming 商业互捧 QQ群538250800 技术搞事 QQ群599020441 技术合作 QQ群152889761 加入我们 QQ群649347320 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。 微服务工程结构搭建 错误:Maven Resources Compiler: Maven project …