Re: how to prepare a create table statement

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: Alexander Mills <alexander(dot)d(dot)mills(at)gmail(dot)com>
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: how to prepare a create table statement
Date: 2021-01-04 10:55:11
Message-ID: X/L0D8B8euaRuHBK@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2021-01-04 14:53:12 BUG #16803: create a table only one text/varchar column, storage options toast_tuple_target doesn't work
Previous Message Pavel Stehule 2021-01-04 10:51:03 Re: how to prepare a create table statement