pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Stefan Keller <sfkeller@gmail.com>
To: Postgres General <pgsql-general@postgresql.org>
Subject: Re: How to set parameters in 'options'?
Date: Tue, 29 Jan 2019 07:21:46 -0800
Message-ID: <884a4b56-9f35-9baf-50f2-7ba9176896a5@aklaver.com> (raw)
In-Reply-To: <CAFcOn2_a2Lf2QBu1BMkuUf11RfmQDCz4JGTP=-Bq45XQCb8Gaw@mail.gmail.com>
References: <CAFcOn2_a2Lf2QBu1BMkuUf11RfmQDCz4JGTP=-Bq45XQCb8Gaw@mail.gmail.com>

On 1/29/19 6:40 AM, Stefan Keller wrote:
> Hi,
> 
> I'd like to write a function like this:
> 
> create function foo(_host text, _port text, _dbname text)
> returns void as $$
>    create server _server
>      foreign data wrapper postgres_fdw
>      options (host _host, port _port, dbname _dbname);
>      -- ERROR: syntax error at or near "_host"
> $$ language sql;
> 
> In order to e.g. do:
> select foo('111.11.11.11', '5432', 'mydb');
> 
> How can I set the parameters in 'options' using those variables?

If you are going to use plpgsql, then format:

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

https://www.postgresql.org/docs/10/plpgsql-statements.html#PLPGSQL-QUOTE-LITERAL-EXAMPLE

So something like:

execute format('create server _server
     foreign data wrapper postgres_fdw
     options (host $1, port $2, dbname $3') USING _host, _port, _dbname

Not sure where the _server is coming from so that might have to be dealt 
with.

> 
> :Stefan
> 
> P.S. Actually I'll put this code in a plggsql function later on.
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (5+ messages)  latest in thread

Message-ID: <884a4b56-9f35-9baf-50f2-7ba9176896a5@aklaver.com>
Permalink:  ../884a4b56-9f35-9baf-50f2-7ba9176896a5@aklaver.com/
Also on:    postgresql.org/message-id/884a4b56-9f35-9baf-50f2-7ba9176896a5@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, sfkeller@gmail.com
  Subject: Re: How to set parameters in 'options'?
  In-Reply-To: <884a4b56-9f35-9baf-50f2-7ba9176896a5@aklaver.com>
  References: <CAFcOn2_a2Lf2QBu1BMkuUf11RfmQDCz4JGTP=-Bq45XQCb8Gaw@mail.gmail.com> <884a4b56-9f35-9baf-50f2-7ba9176896a5@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