Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to SELECT data from a postgreSQL INDEX?

If I created an index with following command: CREATE INDEX ixname ON tbname (id);

Where ixname is the name of index, tbname is the table name for which the index is being created and id is the column the index is for.

Now, if I wanted to view what's in ixname, how would I do it? (I'm asking with the assumption that an index is a relation/table with sorted column)

like image 482
Robert C. Holland Avatar asked May 06 '17 15:05

Robert C. Holland


People also ask

How do I select data in PostgreSQL?

If you want to select data from all the columns of the table, you can use an asterisk ( * ) shorthand instead of specifying all the column names. The select list may also contain expressions or literal values. Second, specify the name of the table from which you want to query data after the FROM keyword.

How does Postgres choose an index?

Index the columns with the best selectivity (i.e. being most specific), so that only a small portion of the index has to be scanned. Involve a small number of columns (possibly only one), to keep the index size small - and thus reduce the total number of pages in the index.

How do you select an index database?

It is recommended to start indexing the table by creating a clustered index, that covers the column(s) called very frequently, which will convert it from the heap table to a sorted clustered table, then create the required non-clustered indexes that cover the remaining queries in the system.

How do I view PostgreSQL index?

In PostgreSQL, we use the pr_indexes view to list the indexes of a database. PostgreSQL does not provide a command like SHOW INDEXES to list the index information of a table or database. If you use psql to access the PostgreSQL database, you can use the \d command to view the index information for a table.


1 Answers

You just can't. Not as a client, not using SQL.

Data in the index is internal to PostgreSQL, and it's not accessible to the outside world. You can introspect your index definitions (using pg_indexes table or pg_get_indexdef function), but you can't look up what's actually stored in those.

Well, you technically can find the file(s) in which the index data is stored (use pg_class.relfilenode and checking for files in base/ subdirectory), and decode the binary data of their b-trees (or whatever your indexes use), but I'm not sure this is what you want to do. Unless you intend to learn or hack PostgreSQL internals.

like image 59
drdaeman Avatar answered Oct 14 '22 13:10

drdaeman