Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fN8h3-0000NC-8g for pgsql-general@arkaria.postgresql.org; Mon, 28 May 2018 03:19:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fN8gy-0002m1-CR for pgsql-general@arkaria.postgresql.org; Mon, 28 May 2018 03:19:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fN8gy-0002jv-2G for pgsql-general@lists.postgresql.org; Mon, 28 May 2018 03:19:28 +0000 Received: from mail-it0-x230.google.com ([2607:f8b0:4001:c0b::230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fN8gn-0002yn-Nd for pgsql-general@postgresql.org; Mon, 28 May 2018 03:19:25 +0000 Received: by mail-it0-x230.google.com with SMTP id n64-v6so13273255itb.3 for ; Sun, 27 May 2018 20:19:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=gdgq2gBIwwAmfkSSL5H/npj+Ru6CfJxRJwi97qkyWdQ=; b=ZxzCLQoZk8B8dCTb14p/YBQtxL0bH10ytGglDodvg8vWA9d4EiYTcvPruajffT5iq2 G2pArx+yEBGv+gEWwP3deMP9Cn5439wPp7SA957FaeSoW43bMTY1rp/JxDq75KRqlBzR 9M9AWl5KMLp0V8JdlquAlvDoRdW6lWg+F2Mgjd77TyopZM7JaNHfhbDgsd5shpfxJq5N f5iU5sTzYwueRYebeE8EkDxVwvVqmmjrbipz9LhoyoJQwuYp0pWKCqoGV0TzAQmrtlXg zBhfpnysMFMnX0+0eIQ5IvSeeGhLi1e+AXrlzH/53+h2tCOhXAAFn9NHl5/PSbegFN1H ClWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=gdgq2gBIwwAmfkSSL5H/npj+Ru6CfJxRJwi97qkyWdQ=; b=c/uWxfoqHHUSawab6RAq/4eZI63HTdtC10FoR720kSFQOhxFzCqyz021GyQjToAuJ4 hsVx6IMIiflW546nvNtkPoVAYLa4WqUUXmcCGu3DY+/gVFQzkcgBCc9/+8duSKUJOj/0 X+alaJEbaZI2Y2mb+m+KQp1/QexGfJAttoXmz9IEFtKgXgtLbwaTjfvP5hksdTHW+YOX 9ONj2o+ejJzYq1KxxX9ZBihdrlmrg8wBYrXmniFQyowUnTJQ/SEgJPhXahXDfTwn5Ywi vGWrPTGK7b3CTKpicSUu39IZD6HClf/JLjh1io/3uzUHj1XDF+0oEDwIzYTdTVKVXyN2 zRrg== X-Gm-Message-State: ALKqPwfEOcb19QfjWm3XJHfC+xYxbqPNSNIIf5dcZFIgwJ/eJtC8ls1+ iLAmikjANjcF03LDnzY2hGs= X-Google-Smtp-Source: AB8JxZr/pso/5pIyQIhxNPGpSmy6iS+ahRR+xB2VF5n6F4e2Lv8Y1gfCBWDpIiWx1+g5kIy3CGKAFw== X-Received: by 2002:a24:eb0e:: with SMTP id h14-v6mr10605006itj.69.1527477555780; Sun, 27 May 2018 20:19:15 -0700 (PDT) Received: from [192.168.1.140] (c-98-202-193-197.hsd1.ut.comcast.net. [98.202.193.197]) by smtp.gmail.com with ESMTPSA id e189-v6sm15495804ioe.0.2018.05.27.20.19.14 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 27 May 2018 20:19:15 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.3 \(3445.6.18\)) Subject: Re: Help with SQL From: Rob Sargent In-Reply-To: <1527468155333-0.post@n3.nabble.com> Date: Sun, 27 May 2018 21:19:13 -0600 Cc: "pgsql-general@postgresql.org" Content-Transfer-Encoding: quoted-printable Message-Id: References: <1527468155333-0.post@n3.nabble.com> To: anand086 X-Mailer: Apple Mail (2.3445.6.18) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > On May 27, 2018, at 6:42 PM, anand086 wrote: >=20 > Hi, >=20 > 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. >=20 > I am using the below code to identify the indexname and index = definition > from parent table -- >=20 > ``` > with idx as=20 > (select indexrelid::regclass indexname, = indisunique, > indisprimary from pg_index where indrelid in=20 > (select oid from pg_class where relname in = (select > tablename from pg_indexes where tablename=3D'test_booking') > )=20 > and indpred is not null > )=20 > select idxs.tablename, idxs.indexname, idxs.indexdef, > idx.indisunique from pg_indexes as idxs join idx on > idxs.indexname=3Dsplit_part(idx.indexname::text , '.' ,2) > ``` >=20 > Suppose the indexdef is on parent table is -- >=20 > 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)) >=20 > Now, what I am trying to achieve is to create and execute the below = sql, > wherein I replace the indexname with = uniq__ > and tablename with the childtablename part of function input. >=20 > 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)) >=20 > 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. >=20 >=20 > ``` > if idxrec.indisunique=3D't' then > SELECT substr(concat(md5(random()::text), = md5(random()::text)), 0, > 7) into var; > idxname:=3D'uniq_'||idxrec.tablename||'_'||var; > raise notice 'Index name will be %', idxname; > createStmts1:=3Dsubstring(idxrec.indexdef from 0 for = 21); --> gives me > the "create unique index" > raise notice 'String1 %', createStmts1; > createStmts2:=3D > raise notice 'String2 %', createStmts2; > ``` >=20 > Is this the correct way? Any better suggestion?=20 >=20 > How can I achieve this? >=20 >=20 Is using a stored procedure a required? To do so would require = =E2=80=9Cdynamic sql=E2=80=9D but imho that=E2=80=99s not the best use = of stored procedures. Perhaps you could use your =E2=80=9Cwith ids=E2=80=9D= 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.