Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sWLqK-00EdUY-IJ for pgsql-general@arkaria.postgresql.org; Tue, 23 Jul 2024 20:10:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sWLqI-00G0GW-C8 for pgsql-general@arkaria.postgresql.org; Tue, 23 Jul 2024 20:10:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sWLqI-00G0Eo-0G for pgsql-general@lists.postgresql.org; Tue, 23 Jul 2024 20:10:38 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sWLqG-0016t2-11 for pgsql-general@lists.postgresql.org; Tue, 23 Jul 2024 20:10:37 +0000 Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-a7a975fb47eso142031866b.3 for ; Tue, 23 Jul 2024 13:10:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1721765435; x=1722370235; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=EvR5f9EUtbYZDLA9RDya/Mkf+Vt5WKKqxiRTe5vFi4U=; b=XR3+Mr9vMKfNOwvGkhAF20bAMEd3UsW1HwuWWCyJf1fXDsEjCMoYF11NPDE8pFJQgT u/ut+fvUSnH91aVZPTQO2FgU5YYmotH7rxTtKEu2SQNlTaA93rYGo5jjTGG6unobJKrv yCkMVW90aGZjslgjL6HdXL0EbYYYxV3FIFgc9tWyuYEotIha99AcC+erV296KDTvJ/Zn RyjjNkt2KoRGbfKehhIwL2ctbTEcdw+m/fBh17TeDu3MBOA0wvoHhrvx5z9evPc/xDFG uFcXZ1exopz9JiaVTmz2aowSf6fUQzY2SsmGktT5WUu4gcQVMMII078b4tVqUDv3eK0U yL5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721765435; x=1722370235; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=EvR5f9EUtbYZDLA9RDya/Mkf+Vt5WKKqxiRTe5vFi4U=; b=iP7BFLAjflw6b6DsQlsrwC/XXpd2VKGf7cJLUbHedFCEaWIfxGLNUCQgcekOUXxfKa Xgh/hcF1ggR4KMexMXuvcEjJAMwYYvk1N3MUGNqAI9rjMsqHFheou/UXNkGTu9cSH8NX CfLqvgGx7p2n+ZS9hkCUD8Ra+zBsCwAY/SsodjD/Pkf/nQpoT01Xph+F9/zmK+4rV7gT yx0ahAZZpwETAHjyFaf1r5+8naD1wGe3KqrCXDMsPNIbGiY1d5Xtho8rl77j/VGq9EBf oGZYmIomF3pyOaGdDhn/d7sp9goeW4ppfkW6sUqffkJVZ6wj/zgMLnVxi4R8Aip/85sM R9lA== X-Gm-Message-State: AOJu0YxTJMVgAxOydAkv9bh4ObYWWDjess/wVobECkQHmRLajU+aRqCV tytTrDKeXVQn7lT2PbYE2XG6fIvIfJxuIwI898chfPFdi72FUmorNLTSHxfly/YEDWNB9zmBX/X ewDwUJlHklScDbS3g7h23022uVsZ0j02D X-Google-Smtp-Source: AGHT+IEnVqZ5lE5GhgZ+ET/cWjk3FigYRQyc13Umb+Dkc99y4Nz5sil+mhBUsQazaYrQdAUyiIeG2d7SNm12Mk2C08M= X-Received: by 2002:a17:907:9727:b0:a7a:a4be:2f99 with SMTP id a640c23a62f3a-a7aab62f2cbmr52127766b.22.1721765434740; Tue, 23 Jul 2024 13:10:34 -0700 (PDT) MIME-Version: 1.0 From: veem v Date: Wed, 24 Jul 2024 01:40:23 +0530 Message-ID: Subject: Issue while creating index dynamically To: pgsql-general Content-Type: multipart/alternative; boundary="0000000000001fd91b061defc1a0" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001fd91b061defc1a0 Content-Type: text/plain; charset="UTF-8" Hi, It's postgres version 15.4. We have a requirement to create an index on a big partition table and want to do it online. And to make the script run in an automated way on any day , through our ci/cd pipeline we were trying to write it as below inside a begin/end block. I.e. create index using "ON ONLY" option and then create index on each partition using 'CONCURRENTLY" key word and then attach the index partitions to the main index, something as below. But we are getting an error while executing saying it cant be executed in transaction block with "CONCURRENTLY". So I want to understand , is there any alternate way to get away with this? EXECUTE format('CREATE INDEX %I ON ONLY %I (%I);', index_name, table_name, column_name); FOR partition_name IN SELECT inhrelid::regclass::text FROM pg_inherits WHERE inhparent = table_name::regclass LOOP partition_index_name := partition_name || '_' || index_name || '_idx'; EXECUTE format(' CREATE INDEX CONCURRENTLY %I ON %I (%I);', partition_index_name, partition_name, column_name); EXECUTE format(' ALTER INDEX %I ATTACH PARTITION %I;', index_name, partition_index_name); END LOOP; ******** ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block CONTEXT: SQL statement " CREATE INDEX CONCURRENTLY partitioned_table_0_index1_idx ON partitioned_table_0 (id);" PL/pgSQL function inline_code_block line 20 at EXECUTE Regards Veem --0000000000001fd91b061defc1a0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi,
It's postgres version 15.4. We have a requirem= ent to create an index on a big partition table and want to do it online. A= nd to make the script=C2=A0run in an automated way on any day , through=C2= =A0our ci/cd pipeline we were trying to write it as below inside a begin/en= d block. I.e. create index using "ON ONLY" option and then create= index on each partition using 'CONCURRENTLY" key word and then at= tach the index partitions to the main index, something as below.=C2=A0
<= /div>

But we are getting an error while executing saying= it cant be executed=C2=A0in transaction=C2=A0block with "CONCURRENTLY= ". So I want to understand , is there any alternate way to get away wi= th this?

=C2=A0 EXECUTE format('CREATE = INDEX %I ON ONLY %I (%I);', index_name, table_name, column_name);
=C2=A0 FOR partition_name IN
=C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT inhrel= id::regclass::text
=C2=A0 =C2=A0 =C2=A0 =C2=A0 FROM pg_inherits
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 WHERE inhparent =3D table_name::regclass
=C2=A0= =C2=A0 LOOP
=C2=A0 =C2=A0 =C2=A0 =C2=A0 partition_index_name :=3D parti= tion_name || '_' || index_name || '_idx';

=C2=A0 =C2= =A0 =C2=A0 =C2=A0 EXECUTE format('
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 CREATE INDEX CONCURRENTLY %I ON %I (%I);', partition_index_n= ame, partition_name, column_name);

=C2=A0 =C2=A0 =C2=A0 =C2=A0 EXECU= TE format('
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ALTER INDEX %I= ATTACH PARTITION %I;', index_name, partition_index_name);
=C2=A0 = =C2=A0 END LOOP;


********
ERROR= : =C2=A0CREATE INDEX CONCURRENTLY cannot run inside a transaction block
= CONTEXT: =C2=A0SQL statement "
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 CREATE INDEX CONCURRENTLY partitioned_table_0_index1_idx ON partitio= ned_table_0 (id);"
PL/pgSQL function inline_code_block line 20 at E= XECUTE


Regards
Veem
--0000000000001fd91b061defc1a0--