pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
how to prepare a create table statement
3+ messages / 3 participants
[nested] [flat]

* how to prepare a create table statement
@ 2021-01-04 04:46 Alexander Mills <alexander.d.mills@gmail.com>
  2021-01-04 10:51 ` Re: how to prepare a create table statement Pavel Stehule <pavel.stehule@gmail.com>
  2021-01-04 10:55 ` Re: how to prepare a create table statement Michael Paquier <michael@paquier.xyz>
  0 siblings, 2 replies; 3+ messages in thread

From: Alexander Mills @ 2021-01-04 04:46 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

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$$;


problem is that a CREATE TABLE cannot be prepared statement..

Anyone know how I can accomplish the above? Seems like this is a missing
feature - to prepare a CREATE TABLE statement..


-- 
Alexander D. Mills
New cell phone # (415)730-1805
linkedin.com/in/alexanderdmills

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: how to prepare a create table statement
  2021-01-04 04:46 how to prepare a create table statement Alexander Mills <alexander.d.mills@gmail.com>
@ 2021-01-04 10:51 ` Pavel Stehule <pavel.stehule@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Pavel Stehule @ 2021-01-04 10:51 UTC (permalink / raw)
  To: Alexander Mills <alexander.d.mills@gmail.com>; +Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>

po 4. 1. 2021 v 11:31 odesílatel Alexander Mills <
alexander.d.mills@gmail.com> napsal:

> 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$$;
>
>
> problem is that a CREATE TABLE cannot be prepared statement..
>
> Anyone know how I can accomplish the above? Seems like this is a missing
> feature - to prepare a CREATE TABLE statement..
>

This is a bad idea. You should not use PREPARE statement in plpgsql code
ever. Your code looks like from MySQL :)

do $$
begin
  for i in 0..500
  loop
   execute format('CREATE TABLE %I PARTITION OF mbk_auth_method FOR VALUES
WITH (modulus 500, remainder %s)',
                  'mbk_auth_method_' || i, i);
  end loop;
end;
$$;

It can be a little bit messy, but EXECUTE from the plpgsql environment is a
different statement than EXECUTE from SQL environment. Dynamic statements
are "prepared" implicitly in plpgsql. You cannot use  PREPARE there.

Regards

Pavel

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: how to prepare a create table statement
  2021-01-04 04:46 how to prepare a create table statement Alexander Mills <alexander.d.mills@gmail.com>
@ 2021-01-04 10:55 ` Michael Paquier <michael@paquier.xyz>
  1 sibling, 0 replies; 3+ messages in thread

From: Michael Paquier @ 2021-01-04 10:55 UTC (permalink / raw)
  To: Alexander Mills <alexander.d.mills@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

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

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-01-04 10:55 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-01-04 04:46 how to prepare a create table statement Alexander Mills <alexander.d.mills@gmail.com>
2021-01-04 10:51 ` Pavel Stehule <pavel.stehule@gmail.com>
2021-01-04 10:55 ` Michael Paquier <michael@paquier.xyz>

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