pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: reiner peterke <zedaardv@gmail.com>
To: Campbell, Lance <lance@illinois.edu>
Cc: pgsql-admin@postgresql.org <pgsql-admin@postgresql.org>
Subject: Re: script to drop and create all indexes in a database with parameter concurrently
Date: Wed, 18 Dec 2013 10:32:50 +0100
Message-ID: <853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com> (raw)
In-Reply-To: <B75CD08C73BD3543B97E4EF3964B7D701F65D459@CITESMBX1.ad.uillinois.edu>
References: <B75CD08C73BD3543B97E4EF3964B7D701F65D459@CITESMBX1.ad.uillinois.edu>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-admin>

i wanted to give a few thoughts regarding your script. 
I don't know your intended use, but generally indexes are not something that should be rebuilt over an entire database. I can not think of a scenario where I would need to do this. Building indexes takes system resources and should never be done blindly. 
If this is a production environment, dropping the indexes before rebuilding them would be horrible for system performance. A better approach would be to identify only those indexes that actually require a rebuild.
When rebuilding indexes, I create the new index first so there is always an index for queries needing them, then I drop and rename the index in a transaction as the last step. 

reiner

On 17 dec 2013, at 21:24, "Campbell, Lance" <lance@illinois.edu> wrote:

> 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
> Software Architect
> Web Services at Public Affairs
> 217-333-0382
> <image002.png>
>  
>  

view thread (7+ messages)  latest in thread

Message-ID: <853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com>
Permalink:  ../853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com/
Also on:    postgresql.org/message-id/853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: zedaardv@gmail.com, lance@illinois.edu
  Subject: Re: script to drop and create all indexes in a database with parameter concurrently
  In-Reply-To: <853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com>
  References: <B75CD08C73BD3543B97E4EF3964B7D701F65D459@CITESMBX1.ad.uillinois.edu> <853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox