Re: Is that a bug?

From: "Todd R(dot) Eigenschink" <todd(at)tekinteractive(dot)com>
To: Tomaz Borstnar <tomaz(dot)borstnar(at)over(dot)net>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: Is that a bug?
Date: 2000-12-30 00:18:37
Message-ID: 14925.10717.1972.52291@rtfm.ofc.tekinteractive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Tomaz Borstnar writes:
>At 06:59 27.12.2000, R D wrote the following message:
>> >
>> > Does dropping and recreating the index help?
>>
>>10x! It worked!
>
>Anyone have some utility to recreate the index for any given database? This
>way I don't have to study the database and do lots of manual work which
>computers can do so much faster.

This is what I think you're asking for, and then some.

It's a grotesque mess of perl and shell script that we use nightly to
vacuum everything. Since it's usually (at least for our databases) a
lot faster to drop each index, vacuum the tables, and then recreate
the index, that's what this does.

This does all tables in all databases, and assumes that they're all
owned by the same user (www in our case). It uses "pg_dump -s" to get
the strings used to build each index (cheating :) and then just feeds
them back when it's done. It fits our scenario nicely, but your
mileage will probably vary wildly.

One of the reasons it's wacky is that since it's run from cron, I
don't want to see the output unless there's a problem. So this
collects the output of the vacuum, and then just eats it unless
there's a warning or error of some kind. (I could have done the whole
thing in perl, but the first version of it was mostly shell, and then
it grew. Kind of like a fungus.)

Developers, feel free to include this or something like it with the
PostreSQL distribution. Credit me or not; doesn't matter.

Todd
--
Todd R. Eigenschink TEK Interactive Group, Inc.
todd(at)tekinteractive(dot)com http://www.tekinteractive.com/
System Administrator (219) 459-2521

----------------------------------------------------------------------
#!/bin/sh

PATH=/usr/pgsql/bin:$PATH

if [ `whoami` != 'www' ]; then
echo "autovac must be run by www!"
exit 1
fi

psql -l -A -q -t| tr '|' ' ' | grep -v '^template1 ' | \
while read DATABASE PGUSERID DATAPATH
do
perl - $DATABASE <<'EOF' \
| psql -e $DATABASE > /tmp/db.psql.$$ 2>&1
my $database = shift;

open SCHEMA, "/usr/pgsql/bin/pg_dump -s $database |";
while (<SCHEMA>)
{
if (/CREATE TABLE \"(\S+)\"/)
{ $table{$1} = []; }
elsif (/CREATE\s+INDEX\s+\"\S+\" on \"(\S+)\" using/)
{ push @{$table{$1}}, $_; }
}
close SCHEMA;

foreach my $table (sort keys %table)
{
foreach (@{$table{$table}})
{
/CREATE\s+INDEX\s+\"(\S+)\"\s+on/;
print "DROP INDEX $1;\n";
}
print "VACUUM ANALYZE $table;\n";
foreach (@{$table{$table}}) { print; }
}
EOF

perl - $DATABASE /tmp/db.psql.$$ <<'PROCESS'
my $database = shift;
my ($line, $mark);
while (<>)
{
$mark = 1 if $_ !~ /^(VACUUM|CREATE|DROP)/;
$line .= $_;
}

#(print "No errors, but here's the output:\n"), $mark = 1 if not $mark;
if ($mark)
{
print "================================================================\n";
print "DATABASE: $database\n";
print $line;
print "================================================================\n";
}
PROCESS

done

rm -f /tmp/db.psql.$$

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Thomas SMETS 2000-12-30 00:50:05 Re: Running a file
Previous Message Tomaz Borstnar 2000-12-29 23:22:43 Re: Is that a bug?