pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
plpython/python string formatting
3+ messages / 2 participants
[nested] [flat]

* plpython/python string formatting
@ 2022-08-16 23:42  Ted Toth <txtoth@gmail.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Ted Toth @ 2022-08-16 23:42 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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?

Ted





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

* Re: plpython/python string formatting
@ 2022-08-16 23:55  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ted Toth <txtoth@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Adrian Klaver @ 2022-08-16 23:55 UTC (permalink / raw)
  To: Ted Toth <txtoth@gmail.com>; pgsql-general@lists.postgresql.org

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?

Best guess is you are seeing cross platform collision with:

https://www.postgresql.org/docs/current/functions-string.html#FUNCTIONS-STRING-FORMAT

Why not?:

s = format('CREATE INDEX %s ON %s USING (column_name)' %
(index_name, table_name))


> 
> Ted
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





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

* Re: plpython/python string formatting
@ 2022-08-17 00:23  Adrian Klaver <adrian.klaver@aklaver.com>
  parent: Ted Toth <txtoth@gmail.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Adrian Klaver @ 2022-08-17 00:23 UTC (permalink / raw)
  To: Ted Toth <txtoth@gmail.com>; pgsql-general@lists.postgresql.org

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





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


end of thread, other threads:[~2022-08-17 00:23 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 23:42 plpython/python string formatting Ted Toth <txtoth@gmail.com>
2022-08-16 23:55 ` Adrian Klaver <adrian.klaver@aklaver.com>
2022-08-17 00:23 ` Adrian Klaver <adrian.klaver@aklaver.com>

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