pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Steve Castellotti <SteveC@innocent.com>
To: pgsql-sql@postgresql.org
Subject: Looking up table names by REFERENCES
Date: Tue, 25 Jan 2005 04:09:09 +1300
Message-ID: <1106579349.746.8.camel@odyssey> (raw)


    Hello all. I'm trying to write a recursive procedure to allow me to
handle some data backup routines and deal with conflicts when restoring
from within the program.

    Essentially, what I'd like to be able to do is if a table called
"image" has a column called "file_id" which references a column called
"file_id" in another table called "file" I want to be able to determine
that pragmatically. If I wanted to backup all of the information about
images in the database, I would need to backup all of the information
about the file(s) each image corresponds to.

    For instance, I can get a list of all (user) table names with:

SELECT relname AS table_name, oid
	FROM pg_class
		WHERE NOT relname ~ 'pg_.*'
			AND NOT relname ~ 'pga_.*'
			AND NOT relname ~ '.*_pkey'
			AND NOT relname ~ '.*_id_key'
		ORDER BY relname;

and I can get a list of column names and their types (for the "image"
table) with:

SELECT a.attname AS field, t.typname AS type 
	FROM pg_class c, pg_attribute a, pg_type t 
		WHERE c.relname = 'image' and a.attnum > 0 
			and a.attrelid = c.oid and a.atttypid = t.oid 
		ORDER BY a.attnum;


    Surely there's a simple way I can trace REFERENCES in a particular
column across tables?


    Any help would be most appreciated, especially if I could be cc'd
directly.


Cheers

Steve Castellotti

view thread (2+ messages)  latest in thread

Message-ID: <1106579349.746.8.camel@odyssey>
Permalink:  ../1106579349.746.8.camel@odyssey/
Also on:    postgresql.org/message-id/1106579349.746.8.camel@odyssey

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: SteveC@innocent.com
  Subject: Re: Looking up table names by REFERENCES
  In-Reply-To: <1106579349.746.8.camel@odyssey>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox