--- trunk/bin/pgest-index.pl 2006/08/07 22:53:46 74 +++ trunk/bin/pgest-index.pl 2006/08/08 11:01:00 77 @@ -54,8 +54,8 @@ =item --pk id Specify name of primary key column in SQL query. If you allready have primary -key on table, and it consists of simgle column (compaund keys are not supported) -it will be picked up automatically. +key on table or unique index and it consists of simgle column +(compaund keys are not supported) it will be picked up automatically. If you specify value which is not unique, you will get just last occurence of that item in index (which might be what you want). That's because specified @@ -156,19 +156,23 @@ warn "# finding primary key for $table\n" if ($c->{debug}); - $c->{pk} = $dbh->selectrow_array(qq{ + my $index_fmt = qq{ SELECT a.attname, t.typname FROM pg_type t, pg_attribute a WHERE t.oid = a.atttypid AND attrelid = ( SELECT indexrelid FROM pg_class c, pg_index i - WHERE c.relname = '$table' + WHERE c.relname = '%s' AND c.oid = i.indrelid - AND indisprimary + AND %s AND indnatts = 1 ) - }); + }; + + $c->{pk} = $dbh->selectrow_array( sprintf($index_fmt, $table, 'indisprimary') ); + + $c->{pk} ||= $dbh->selectrow_array( sprintf($index_fmt, $table, 'indisunique') ); }