473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to get a list of indexes created on a column of a table in postgresql?

Hi All,

I want to know how to get a list of all indexes created on a column of a table in PostgreSQL. There is a program block in our application which do this in oracle, now i want to write the same piece of code which works for PostgreSQL as well. So, i want the list of indexes on a column of a table if i know the column name at that time or list of all the indexes on all the columns of the given table.

Below is the java Code written for Oracle DB, which now i need to rewrite for PostgreSQL. Can any body help me in how retrieve list of indexes as below?
Expand|Select|Wrap|Line Numbers
  1.  /**
  2.      *  Gets a list of all indexes on a given table and column in the mart
  3.      */
  4.     public Vector getIndexList(String column_name, String table_name)
  5.     throws EpiException, EpiSQLException
  6.     {
  7.         Vector indexList = new Vector();
  8.         String sql = "SELECT i.name FROM dbo.sysindexes i WHERE i.name NOT LIKE '[_]%' " +
  9.                     " AND i.name IS NOT NULL AND i.id = " +
  10.                         " (SELECT o.id FROM dbo.sysobjects o " +
  11.                             " WHERE UPPER(o.name) = '" + table_name.toUpperCase() + "') ";
  12.  
  13.         if (column_name != null)
  14.         {
  15.             sql += " AND EXISTS (SELECT 1 FROM dbo.sysindexkeys ik, dbo.syscolumns c " +
  16.                         " WHERE i.indid = ik.indid AND ik.colid = c.colid " +
  17.                         " AND i.id = ik.id AND c.id = ik.id " +
  18.                         " AND UPPER(c.name) = '" + column_name.toUpperCase() + "') ";
  19.         }
  20.         DBConnection con = null;
  21.         DBStatement    stmt = null;
  22.         DBResultSet rs = null;
  23.  
  24.         try
  25.         {
  26.             con = getConnection(this);
  27.             stmt = con.createStatement(this);
  28.             rs = stmt.executeQuery(sql);
  29.  
  30.             while (rs.next())
  31.             {
  32.                 indexList.addElement(rs.getString(1));
  33.             }
  34.         }
  35.         finally
  36.         {
  37.             if (rs != null)
  38.                 rs.close();
  39.             if (stmt != null)
  40.                 stmt.close();
  41.             if (con != null)
  42.                 releaseConnection(con);
  43.         }
  44.         return indexList;
  45.     }
  46.  
Oct 28 '10 #1
1 4403
I myself found the answer somewhere, posting it here for reference. I am surprise for not answering this question.

String sql = "select i.relname from pg_class t, pg_class i, pg_index ix, pg_attribute a" +
"where t.oid = ix.indrelid and i.oid= ix.indexrelid and a.attrelid = t.oid and a.attnum = ANY(ix.indkey)"+
"and t.relkind = 'r' and t.relname = '" + table_name.toLowerCase() + "'";

if (column_name != null)
{
sql += " AND a.attname = '" + column_name.toLowerCase() + "'";
}
Oct 30 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Peter Foti | last post by:
Hi all, I have a form that contains a 2 column table. In the left column is the description for a particular input (ie - "First Name", "Last Name", "Phone Number", etc.). In the right column...
2
by: deepakp | last post by:
Hi, I'm trying to create a site index page. The page layout will consist of a header, left navigation and content display. The left navigation and content display should look like the image file...
2
by: Hymer | last post by:
Hello, I have a small two-column table with three rows. The first column has a logo and the second column has the name of the organization. The logo's in the first column are too high. That...
5
by: Hymer | last post by:
Hello, I have a small two-column table with three rows. The first column has a logo and the second column has the name of the organization. The logo's in the first column are too high. That...
10
by: ste | last post by:
Hi there, I'm trying to query a MySQL database (containing image data) and to output the results in a HTML table of 3 columns wide (and however many rows it takes) in order to create a basic...
10
by: lesperancer | last post by:
you start with a small application in access97, then you have more modules and more... and you reach the point where tables like 'item' and 'employee' reach the limit and you know there's more...
2
by: rcamarda | last post by:
Hi, I found this SQL in the news group to drop indexs in a table. I need a script that will drop all indexes in all user tables of a given database: DECLARE @indexName NVARCHAR(128) DECLARE...
6
by: DavidOwens | last post by:
Hey everybody im creating a report for regional managers, i have created a table called STORES, what i want to be able to do is that when the page loads, it currents has the users name n id,...
1
by: akress | last post by:
Hiya, I'm trying to convert a table with 6 colums that looks like this: <table class="myTable" width="640px"> <tr> <td><a href="#"> <img src="../_img/announce.gif" align="middle"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.