Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6718549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:58:58+00:00 2026-05-26T08:58:58+00:00

I have an application that reads the structure of an existing PostgreSQL 9.1 database,

  • 0

I have an application that reads the structure of an existing PostgreSQL 9.1 database, compares it against a “should be” state and updates the database accordingly. That works fine, most of the time. However, I had several instances now when reading the current database structure deadlocked. The query responsible reads the existing foreign keys:

SELECT tc.table_schema, tc.table_name, tc.constraint_name, kcu.column_name,
       ccu.table_schema, ccu.table_name, ccu.column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
     ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
     ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'

Viewing the server status in pgAdmin shows this to be the only active query/transaction that’s running on the server. Still, the query doesn’t return.

The error is reproducible in a way: When I find a database that produces the error, it will produce the error every time. But not all databases produce the error. This is one mysterious bug, and I’m running out of options and ideas on what else to try or how to work around this. So any input or ideas are highly appreciated!

PS: A colleague of mine just reported he produced the same error using PostgreSQL 8.4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:58:58+00:00Added an answer on May 26, 2026 at 8:58 am

    I tested and found your query very slow, too. The root of this problem is that "tables" in information_schema are in fact complicated views to provide catalogs according to the SQL standard. In this particular case, matters are further complicated as foreign keys can be built on multiple columns. Your query yields duplicate rows for those cases which, I suspect, may be an undesired.

    Correlated subqueries with unnest, fed to ARRAY constructors avoid the problem in my query.

    This query yields the same information, just without duplicate rows and 100x faster. Also, I would venture to guarantee, without deadlocks.

    Only works for PostgreSQL, not portable to other RDBMSes.

    SELECT c.conrelid::regclass AS table_name
         , c.conname AS fk_name
         , ARRAY(SELECT a.attname
                 FROM   unnest(c.conkey) x
                 JOIN   pg_attribute a
                 ON     a.attrelid = c.conrelid AND a.attnum = x) AS fk_columns
         , c.confrelid::regclass AS ref_table
         , ARRAY(SELECT a.attname
                 FROM   unnest(c.confkey) x
                 JOIN   pg_attribute a
                 ON     a.attrelid = c.confrelid AND a.attnum = x) AS ref_columns
    FROM   pg_catalog.pg_constraint c
    WHERE  c.contype = 'f';
    -- ORDER  BY c.conrelid::regclass::text,2
    

    The cast to ::regclass yields table names as seen with your current search_path. May or may not be what you want. For this query to include the absolute path (schema) for every table name you can set the search_path like this:

    SET search_path = pg_catalog;
    SELECT ...
    

    To continue your session with your default search_path:

    RESET search_path;
    

    Related:

    • Get column names and data types of a query, table or view
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that reads a database and outputs alerts to any dependencies
I have an application that reads a table from a database. I issue an
I have a windows forms application that reads and updates an XML file with
I have an application that reads a CSV file with piles of data rows.
I have an application that reads lines from a file and runs its magic
I have an application that reads data from a com port using javax.comm. The
I have an iPhone application that reads RSS feeds from a YouTube channel. However,
I have a Silverlight application that reads its content from an XML file. The
I have a basic C# console application that reads a text file (CSV format)
Problem in words: For my application, I have a class that reads from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.