Hi,
On Tue, Dec 26, 2023 at 10:03 PM Peter Eisentraut <peter@eisentraut.org> wrote:
>
> On 24.12.23 03:03, Michael Paquier wrote:
> > - Use a DO block of a PL function, say with something like that to
> > ensure an amount of N queries? Say with something like that after
> > tweaking pg_stat_statements.track:
> > CREATE OR REPLACE FUNCTION create_tables(num_tables int)
> > RETURNS VOID AS
> > $func$
> > BEGIN
> > FOR i IN 1..num_tables LOOP
> > EXECUTE format('
> > CREATE TABLE IF NOT EXISTS %I (id int)', 't_' || i);
> > END LOOP;
> > END
> > $func$ LANGUAGE plpgsql;
>
> I tried it like this first, but this doesn't register as separately
> executed commands for pg_stat_statements.
I was a bit surprised by that so I checked locally. It does work as
expected provided that you set pg_stat_statements.track to all:
=# select create_tables(5);
=# select queryid, query from pg_stat_statements where query like 'CREATE%';
queryid | query
----------------------+-----------------------------------------
-4985234599080337259 | CREATE TABLE IF NOT EXISTS t_5 (id int)
-790506371630237058 | CREATE TABLE IF NOT EXISTS t_2 (id int)
-1104545884488896333 | CREATE TABLE IF NOT EXISTS t_3 (id int)
-2961032912789520428 | CREATE TABLE IF NOT EXISTS t_4 (id int)
7273321309563119428 | CREATE TABLE IF NOT EXISTS t_1 (id int)