pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: anand086 <anand086@gmail.com>
Cc: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: Re: Help with SQL
Date: Sun, 27 May 2018 21:19:13 -0600
Message-ID: <B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.com> (raw)
In-Reply-To: <1527468155333-0.post@n3.nabble.com>
References: <1527468155333-0.post@n3.nabble.com>



> On May 27, 2018, at 6:42 PM, anand086 <anand086@gmail.com> wrote:
> 
> Hi,
> 
> I have a requirement of writing plpgsql function to create partial indexes
> on child tables if it exists on parent table. The function will have
> schemname, childtablename, tableowner as input.
> 
> I am using the below code to identify the indexname and index definition
> from parent table --
> 
> ```
> with idx as 
>   	             (select indexrelid::regclass indexname, indisunique,
> indisprimary from pg_index where indrelid in 
>   	                 (select oid from pg_class where relname in (select
> tablename from pg_indexes where tablename='test_booking')
>   				     ) 
>   				    and indpred is not null
>   				 ) 
>        select idxs.tablename, idxs.indexname, idxs.indexdef,
> idx.indisunique from pg_indexes as idxs join idx on
> idxs.indexname=split_part(idx.indexname::text , '.' ,2)
> ```
> 
> Suppose the indexdef is on parent table is --
> 
> CREATE UNIQUE INDEX uniq_test_booking_1 ON demo.test_booking USING btree
> (col1,col2 ,col3, col4,col5, col6) WHERE ((col4 IS NOT NULL) AND (col6 IS
> NOT NULL))
> 
> Now, what I am trying to achieve is to create and execute the below sql,
> wherein I replace the  indexname with uniq_<child_table_name>_<randomtext>
> and tablename with the childtablename part of function input.
> 
> CREATE UNIQUE INDEX uniq_test_booking_20180527_gdhsd ON
> demo.test_booking_20180527 USING btree (col1,col2 ,col3, col4,col5, col6)
> WHERE ((col4 IS NOT NULL) AND (col6 IS NOT NULL))
> 
> Using substring I am trying to break the SQL statement in 2 and then later
> concatenate it.
> The first part is substring(idxrec.indexdef from 0 for 21);  --> output is
> "create unique index " statement.
> and for the 2nd part, starting for USING until the end. But I am unable to
> get the 2nd part of sql.
> 
> 
> ```
>        if idxrec.indisunique='t' then
>        	SELECT substr(concat(md5(random()::text), md5(random()::text)), 0,
> 7) into var;
>        	idxname:='uniq_'||idxrec.tablename||'_'||var;
>        	raise notice 'Index name will be %', idxname;
>    		createStmts1:=substring(idxrec.indexdef from 0 for 21);  --> gives me
> the "create unique index"
>    		raise notice 'String1 %', createStmts1;
>    		createStmts2:=
>    		raise notice 'String2 %', createStmts2;
> ```
> 
> Is this the correct way? Any better suggestion? 
> 
> How can I achieve this?
> 
> 
Is using a stored procedure a required?  To do so would require “dynamic sql” but imho that’s not the best use of stored procedures.  Perhaps you could use your “with ids” sql to get the list of required indices with names, columns etc, place those values (sed, awk, perl, python, even sql) into versions of your create index code and stick those generated files in hopefully the same source code repository which has the definition of the parent and children tables.  They will likely all need maintenace at the same time.





view thread (7+ messages)

Message-ID: <B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.com>
Permalink:  ../B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.com/
Also on:    postgresql.org/message-id/B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.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-general@postgresql.org
  Cc: robjsargent@gmail.com, anand086@gmail.com
  Subject: Re: Help with SQL
  In-Reply-To: <B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.com>
  References: <1527468155333-0.post@n3.nabble.com> <B72AF0EB-198D-46F2-B479-99DCDB8D1006@gmail.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