script to drop and create all indexes in a database with parameter concurrently

From: "Campbell, Lance" <lance(at)illinois(dot)edu>
To: "pgsql-admin(at)postgresql(dot)org" <pgsql-admin(at)postgresql(dot)org>
Subject: script to drop and create all indexes in a database with parameter concurrently
Date: 2013-12-17 20:24:38
Message-ID: B75CD08C73BD3543B97E4EF3964B7D701F65D459@CITESMBX1.ad.uillinois.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Below is a Linux script I wrote that will drop and create all of the indexes for your database with the "concurrently" parameter.

#!/bin/sh

dbhost=localhost
database=somedatabasename
dbschema=/tempfile/dbschema.txt
filtered=/tempfile/dbschema_filtered.txt
sql=/tempfile/rebuild_indexes.sql

rm "$dbschema"
rm "$filtered"
rm "$sql"

pg_dump -s -h "$dbhost" "$database" > "$dbschema"

grep -e CREATE\ INDEX -e SET\ search_path "$dbschema" | sed 's/CREATE\ INDEX/CREATE\ INDEX\ CONCURRENTLY/g' > "$filtered"

while read p; do

if [[ "$p" == SET* ]]; then

echo $p >> "$sql"

else

name=$(cut -d\ -f4 <<<"${p}")
drop="DROP INDEX $name;"
echo $drop >> "$sql"
echo $p >> "$sql"

fi

done < "$filtered"

psql -U ows -h "$dbhost" -d "$database" -f "$sql"

rm "$dbschema"
rm "$filtered"
rm "$sql"

Thanks,

Lance Campbell<http://illinois.edu/person/lance>
Software Architect
Web Services at Public Affairs
217-333-0382
[University of Illinois at Urbana-Champaign logo]<http://illinois.edu/>

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Doom.zhou 2013-12-18 02:35:33 Re: Notice about psql !
Previous Message Matheus de Oliveira 2013-12-17 17:29:15 Re: shmmax 9.3