如果我使用以下命令创建索引:CREATE INDEX ixname ON tbname (id);
其中ixname
是索引的名称,tbname
是要为其创建索引的表名,id
是索引所在的列。
现在,如果我想查看ixname
中的内容,我该怎么做? (我要假设索引是一个带有排序列的关系/表)
答案 0 :(得分:11)
你不能。不是作为客户端,不是使用SQL。
索引中的数据是PostgreSQL的内部数据,并且外部世界无法访问它。您可以introspect your index definitions(使用pg_indexes
表格或pg_get_indexdef
功能),但无法查找实际存储在这些内容中的内容。
嗯,从技术上讲,您可以find the file(s)存储索引数据(使用pg_class.relfilenode
并检查base/
子目录中的文件),并解码其binary data b-trees(或你的索引使用的任何东西),但我不确定这是你想要做的。除非您打算学习或破解PostgreSQL内部。
答案 1 :(得分:2)
您可以使用pg_filedump实用程序
my_script.py
super_dir
$ git clone git://git.postgresql.org/git/pg_filedump.git
$ cd pg_filedump/
$ make
$ make install
# note the line /usr/bin/install -c pg_filedump '/usr/lib/postgresql/17/bin'
# add env var
$ PATH=/usr/lib/postgresql/17/bin
# check that it works
$ pg_filedump -h
使用 pg_filedump 参数进行播放以获得很好的可视化效果。受https://blog.dbi-services.com/displaying-the-contents-of-a-postgresql-data-file-with-pg_filedump/
的启发