Received: from infnmi1.mi.infn.it (infnmi1.mi.infn.it [192.135.14.77]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5BGluE83117 for ; Mon, 11 Jun 2001 12:47:56 -0400 (EDT) (envelope-from sandro.dentella@tin.it) Received: from pcgun.mi.infn.it (maxmod2.mi.infn.it [192.84.141.195]) by infnmi1.mi.infn.it (8.9.1a/8.8.8/infnmi) with ESMTP id SAA15625 for ; Mon, 11 Jun 2001 18:47:43 +0200 (MET DST) Received: (from sandro@localhost) by pcgun.mi.infn.it (8.9.3/8.8.5) id SAA06352 for pgsql-interfaces@postgresql.org; Mon, 11 Jun 2001 18:46:17 +0200 X-Authentication-Warning: bluff.diade.it: sandro set sender to sandro.dentella@tin.it using -f Date: Mon, 11 Jun 2001 18:46:17 +0200 From: Sandro Dentella To: pgsql-interfaces@postgresql.org Subject: How to determine PRIMARY KEYS Message-ID: <20010611184617.A6328@bluff.diade.it> Mail-Followup-To: pgsql-interfaces@postgresql.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2i X-Archive-Number: 200106/46 X-Sequence-Number: 1709 Hi all, I'm the author of sdsq/tksql an interface to postgreSQL I released under GPL some weeks ago. Tksql tries as much as possible to use PRIMARY KEYS instead of oids in any operation so that logs are much more readable and so that I can use those logs to do a replica of the database to keep two databases in sync (in a very simple situation thought...) Now, I'd like suggestion and a comment on the algorithm I'm using to find the primary key. I really think that this is more than needed, becouse if you have a table w/ a primary key and unique index NOT NULL on an other attribute both these attributes would be choosen not just the minimum needed (the primary key OR an attribute NOT NULL w/ unique index). Here is what I do: 1. find all unique indexes on table ($tbl) SELECT i.indexrelid FROM pg_class c, pg_index i \ WHERE c.relname = '$tbl' and c.oid = i.indrelid \ and i.indisunique = 't' 2. select in pg_attribute the attributes of these indexes... 3. loop over these attrs ($at) and select those that are NOT NULL. SELECT attname,attnotnull from pg_attribute \ where attrelid = (SELECT oid from pg_class \ where relname = '$tbl') and attname = '$at' \ and attnotnull = 't' May I as an alternative just relay on the pattern *_pkey and look at the attribute of relation *_pkey to get the PRIMARY KEY? Is there a document that explains system tables? Thanks in advance sandro *:-) PS. For those who whant to look at tksql or sdsql (the tcl/tk package): http://pcco2.mi.infn.it/sd/soft. If you encounter problems installing or running it I'd really appreciate to be informed (and/or if you like it...;-) . Thanks Note: sorry if this message shows up 2 times. I had problems w/ config of sendmail -- Sandro Dentella *:-) e-mail: sandro@ermit.it sandro.dentella@mi.infn.it