Re: plpython/python string formatting

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Ted Toth <txtoth(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: plpython/python string formatting
Date: 2022-08-17 00:23:16
Message-ID: 80215721-6a73-9dea-4fbb-52002194f717@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 8/16/22 16:42, Ted Toth wrote:
> I've just started playing with plpython but ran into a issue when I
> was trying to use standard python string formatting to generate a SQL
> string for example:
> s = "EXECUTE format('CREATE INDEX %s ON %s USING (column_name)' %
> (index_name, table_name))"
>
> but plpython then tried to run the EXECUTE instead of just setting
> variable 's'. Why does this happen?

Or if you want to correctly quote the identifiers something like:

create table test_tbl(id integer);

DO $$
index_name = 'test'
table_name = 'test_tbl'
s = 'CREATE INDEX "' + index_name + '" ' + 'ON "' + table_name +
'" (id)'
plpy.notice(s)
plpy.execute(s)
$$ LANGUAGE plpythonu;

NOTICE: CREATE INDEX "test" ON "test_tbl" (id)
DO

\d test_tbl
Table "public.test_tbl"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+---------
id | integer | | |
Indexes:
"test" btree (id)

>
> Ted
>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Aravind Phaneendra 2022-08-17 03:46:52 Regarding availability of 32bit client drivers for postgresql 13/14
Previous Message Adrian Klaver 2022-08-16 23:55:59 Re: plpython/python string formatting