Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtKOq-0007E8-Uf for pgsql-admin@arkaria.postgresql.org; Wed, 18 Dec 2013 16:55:09 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VtKOp-00033H-VW for pgsql-admin@arkaria.postgresql.org; Wed, 18 Dec 2013 16:55:08 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtDmk-0000D5-Gv for pgsql-admin@postgresql.org; Wed, 18 Dec 2013 09:51:22 +0000 Received: from mail-la0-x22f.google.com ([2a00:1450:4010:c03::22f]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VtDmd-0007v7-7N for pgsql-admin@postgresql.org; Wed, 18 Dec 2013 09:51:21 +0000 Received: by mail-la0-f47.google.com with SMTP id ep20so3762377lab.34 for ; Wed, 18 Dec 2013 01:51:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:mime-version:in-reply-to:content-type :content-transfer-encoding:message-id:cc:from:subject:date:to; bh=o+qvvpH/t+5UuUODVzvBXlBmuJWtRNgbXTCkbbKEtaQ=; b=B67jS3N3mwpjAu2e0VKxYUT+G7Ogwx+uRwxFsaQNK/qDMhyUFOU/k77MptL5R+yRQr k6t66Rh+j34372FZD/mMPdKAMnyc7Rs1Rd9Vu9reyh6sFmbVDQAa+jRqqKlVcx36P49q 90H3HbgxM/+8bhRyzjV80cm8zUJLGwgKIEm+sPJhai3l8zIJP36VVXG2BPJQc+YfkIop uheAz9cnfqPdLt8+QoPi9mz4rJ8otct5rb4hGM6Vy1zCWz6qvUKOO6GnWtk50OWHuTJh wgEFVSIflr0S1o/XK1ytegZF+pr3d/T/PFaQlIxCMz0UaGAokp0HuB3NRgxiLODGBqSJ ThHA== X-Received: by 10.112.128.194 with SMTP id nq2mr13036lbb.84.1387360273211; Wed, 18 Dec 2013 01:51:13 -0800 (PST) Received: from [10.174.62.182] (host-95-199-30-182.mobileonline.telia.com. [95.199.30.182]) by mx.google.com with ESMTPSA id e10sm27687479laa.6.2013.12.18.01.51.11 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 18 Dec 2013 01:51:11 -0800 (PST) References: Mime-Version: 1.0 (1.0) In-Reply-To: Content-Type: multipart/alternative; boundary=Apple-Mail-2F2D9557-CB8E-4528-A158-F2F4237F090E Content-Transfer-Encoding: 7bit Message-Id: <853F093C-78F0-4C3B-9020-9D97023B2BDC@drizzle.com> Cc: "pgsql-admin@postgresql.org" X-Mailer: iPhone Mail (10B144) From: reiner peterke Subject: Re: script to drop and create all indexes in a database with parameter concurrently Date: Wed, 18 Dec 2013 10:32:50 +0100 To: "Campbell, Lance" X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-admin Precedence: bulk Sender: pgsql-admin-owner@postgresql.org --Apple-Mail-2F2D9557-CB8E-4528-A158-F2F4237F090E Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable i wanted to give a few thoughts regarding your script.=20 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 wh= ere I would need to do this. Building indexes takes system resources and sho= uld never be done blindly.=20 If this is a production environment, dropping the indexes before rebuilding t= hem would be horrible for system performance. A better approach would be to i= dentify only those indexes that actually require a rebuild. When rebuilding indexes, I create the new index first so there is always an i= ndex for queries needing them, then I drop and rename the index in a transac= tion as the last step.=20 reiner On 17 dec 2013, at 21:24, "Campbell, Lance" wrote: > Below is a Linux script I wrote that will drop and create all of the index= es for your database with the =E2=80=9Cconcurrently=E2=80=9D parameter. > =20 > #!/bin/sh > =20 > dbhost=3Dlocalhost > database=3Dsomedatabasename > dbschema=3D/tempfile/dbschema.txt > filtered=3D/tempfile/dbschema_filtered.txt > sql=3D/tempfile/rebuild_indexes.sql > =20 > rm "$dbschema" > rm "$filtered" > rm "$sql" > =20 > pg_dump -s -h "$dbhost" "$database" > "$dbschema" > =20 > grep -e CREATE\ INDEX -e SET\ search_path "$dbschema" | sed 's/CREATE\ IND= EX/CREATE\ INDEX\ CONCURRENTLY/g' > "$filtered" > =20 > while read p; do > =20 > if [[ "$p" =3D=3D SET* ]]; then > =20 > echo $p >> "$sql" > =20 > else > =20 > name=3D$(cut -d\ -f4 <<<"${p}") > drop=3D"DROP INDEX $name;" > echo $drop >> "$sql" > echo $p >> "$sql" > =20 > fi > =20 > done < "$filtered" > =20 > psql -U ows -h "$dbhost" -d "$database" -f "$sql" > =20 > rm "$dbschema" > rm "$filtered" > rm "$sql" > =20 > =20 > Thanks, > =20 > Lance Campbell > Software Architect > Web Services at Public Affairs > 217-333-0382 > > =20 > =20 --Apple-Mail-2F2D9557-CB8E-4528-A158-F2F4237F090E Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
i wanted to give a few thoughts regarding you= r script. 
I don't know your intended use, but generally indexes are not s= omething 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 resou= rces and should never be done blindly. 
If this is a production environmen= t, dropping the indexes before rebuilding them would be horrible for system p= erformance. A better approach would be to identify only those indexes that a= ctually 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 renam= e 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 cr= eate all of the indexes for your database with the =E2=80=9Cconcurrently=E2=80= =9D parameter.

 

#!/bin/sh

 

dbhost=3Dlocalhost

database=3Dsomedatabasename

dbschema=3D/tempfile/dbschema.txt

filtered=3D/tempfile/dbschema_filtered.txt=

sql=3D/tempfile/rebuild_indexes.sql

 

rm "$dbschema"

rm "$filtered"

rm "$sql"

 

pg_dump -s -h "$dbhost" "$database" > "$dbschema"<= o:p>

 

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

 

while read p; do

 

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

=

 

    echo $p >> "$sql"=

 

  else

 

    name=3D$(cut -d\  -f4 <<= ;<"${p}")

    drop=3D"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 Ca= mpbell

Software Architect

Web Services at Public Affairs

217-333-0382

<image002.png><= /p>

 

 

= --Apple-Mail-2F2D9557-CB8E-4528-A158-F2F4237F090E--