--- trunk/bin/pgest-index.pl 2006/08/07 21:08:41 70 +++ trunk/bin/pgest-index.pl 2006/08/08 11:40:09 78 @@ -15,7 +15,7 @@ =cut my $c = { - debug => 1, + debug => 0, }; =head1 SYNOPSIS @@ -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 @@ -111,12 +111,24 @@ sub drop_triggers { my $table = shift || die "no table?"; - warn "removing triggers from $table\n"; - - foreach my $t (qw/UPDATE INSERT DELETE/) { - my $lc_t = lc($t); - $dbh->do(qq{ DROP TRIGGER pgest_trigger_${lc_t} ON ${table} }); + my $sth = $dbh->prepare(qq{ + SELECT relname,tgname + FROM pg_trigger JOIN pg_class ON relfilenode = tgrelid + WHERE tgname LIKE 'pgest_trigger_%' AND relname = ? + }) || $dbh->errstr; + + $sth->execute( $table ) || $sth->errstr(); + + warn "there are ", $sth->rows, " triggers instead of just 3, dropping all\n" if ($sth->rows != 3); + + while (my $row = $sth->fetchrow_hashref) { + my $sql = sprintf(qq{ DROP TRIGGER %s ON %s }, $row->{tgname}, $row->{relname} ); + #warn "# $sql\n"; + $dbh->do( $sql ) || $dbh->errstr; } + + warn "removed ", $sth->rows, " triggers from $table\n" if ($sth->rows); + } if ($c->{drop}) { @@ -156,19 +168,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') ); } @@ -181,7 +197,7 @@ $c->{sql} ||= "select * from $table"; my $sth = $dbh->prepare($c->{sql}) || die $dbh->errstr(); -$sth->execute() || die $sth->errstr(); +$sth->execute() || die $sth->errstr; my @cols = @{ $sth->{NAME} }; @@ -256,10 +272,12 @@ #warn "$sql\n"; - $dbh->do( $sql ) || die $dbh->errstr(); + $dbh->do( $sql ) || die $dbh->errstr; } +warn "created consistency triggers\n"; + $dbh->commit; =head1 SEARCHING