pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Alexander Mills <alexander.d.mills@gmail.com>
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: how to prepare a create table statement
Date: Mon, 4 Jan 2021 19:55:11 +0900
Message-ID: <X/L0D8B8euaRuHBK@paquier.xyz> (raw)
In-Reply-To: <CA+KyZp6SeL-ULK=jjquNcz23hb0nZXYa8wDW1sJJJ=xDiHicxw@mail.gmail.com>
References: <CA+KyZp6SeL-ULK=jjquNcz23hb0nZXYa8wDW1sJJJ=xDiHicxw@mail.gmail.com>

On Sun, Jan 03, 2021 at 08:46:19PM -0800, Alexander Mills wrote:
> I am trying to create 500 partitions using a loop:
> 
> do $$
> declare
> counter integer := 0;
> begin
> while counter <= 500 loop
>     PREPARE create_table(int) AS
>     CREATE TABLE mbk_auth_method_$1 PARTITION OF mbk_auth_method FOR
> VALUES WITH (modulus 500, remainder $1);
>     EXECUTE create_table (counter);
>     counter := counter + 1;
> end loop;
> end$$;
> 
> Anyone know how I can accomplish the above? Seems like this is a missing
> feature - to prepare a CREATE TABLE statement..

What if you simply used a FOR loop and EXECUTE with format()?  Say,
roughly, something like that:
FOR i IN 1..num_tables LOOP
  EXECUTE format('
    CREATE TABLE mbk_auth_method_%I PARTITION OF mbk_auth_method FOR
      VALUES WITH (modulus 500, remainder %I)', i, i);
END LOOP;
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (3+ messages)

Message-ID: <X/L0D8B8euaRuHBK@paquier.xyz>
Permalink:  ../X%2FL0D8B8euaRuHBK@paquier.xyz/
Also on:    postgresql.org/message-id/X/L0D8B8euaRuHBK@paquier.xyz

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, alexander.d.mills@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: how to prepare a create table statement
  In-Reply-To: <X/L0D8B8euaRuHBK@paquier.xyz>

* 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