pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ted Toth <txtoth@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: plpython/python string formatting
Date: Tue, 16 Aug 2022 17:23:16 -0700
Message-ID: <80215721-6a73-9dea-4fbb-52002194f717@aklaver.com> (raw)
In-Reply-To: <CAFPpqQESdqgk4ZM4n7Rxzh9X1VU77kjOt0xk1SL6rMPmVsVfFQ@mail.gmail.com>
References: <CAFPpqQESdqgk4ZM4n7Rxzh9X1VU77kjOt0xk1SL6rMPmVsVfFQ@mail.gmail.com>

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.klaver@aklaver.com





view thread (3+ messages)

Message-ID: <80215721-6a73-9dea-4fbb-52002194f717@aklaver.com>
Permalink:  ../80215721-6a73-9dea-4fbb-52002194f717@aklaver.com/
Also on:    postgresql.org/message-id/80215721-6a73-9dea-4fbb-52002194f717@aklaver.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, txtoth@gmail.com
  Subject: Re: plpython/python string formatting
  In-Reply-To: <80215721-6a73-9dea-4fbb-52002194f717@aklaver.com>
  References: <CAFPpqQESdqgk4ZM4n7Rxzh9X1VU77kjOt0xk1SL6rMPmVsVfFQ@mail.gmail.com> <80215721-6a73-9dea-4fbb-52002194f717@aklaver.com>

* 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