Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gobBa-0007MG-Cr for pgsql-general@arkaria.postgresql.org; Tue, 29 Jan 2019 21:44:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gobBY-00087O-R9 for pgsql-general@arkaria.postgresql.org; Tue, 29 Jan 2019 21:44:48 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gobBY-00087H-JE for pgsql-general@lists.postgresql.org; Tue, 29 Jan 2019 21:44:48 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gobBW-0008FN-1C for pgsql-general@postgresql.org; Tue, 29 Jan 2019 21:44:48 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id C020522215; Tue, 29 Jan 2019 16:44:44 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 29 Jan 2019 16:44:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=2 WOWGExwvPN+xyERxPJQCdJxbc98i5QhP9zZdgbTlpI=; b=wyJ1njVahBTFXp/gI SEMvleu/fBRzmqwFDLvG3y0xmbPGldKbfJRNgmPxSe4ts/WiD8lJOB2yTmpju5WV yREwL71CSEekyeKCkn7rzZZ6zYPX22s/shiD02FtjJGaHzZRKFHEoUGl5DU3pFLY 4GZd3sC4Lc4hF4UhxPXiJt2zmr3YzNMCM0xLwXKDc/VuPQc//oedagv1AR8rEIgF Y89a+SQaYcYyHfhOvhjKnYueVRuWkhko7GUuqcXv/ob7ft7hiyH0azj9sroiDdJI 8NC9X/jUEHt/1pFkAmCOCVxP2ldCoE1oA82Pd5q2c1GjKV+3cHIMeuWJzG5i9rxH 9/IpA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=2WOWGExwvPN+xyERxPJQCdJxbc98i5QhP9zZdgbTl pI=; b=O7RmTzJnqJ5ZokL6ELoiOwQCQbBC4JU//goWckk5Z9I2vHeNqj+t3BCIt LXrcAgdURbru28Lm+bOYnoKAB6WjR/CN6h5wBMxeRLzYAi7JJvLpJGG6hFR3mPNV r/gxT3sEtzaNBgPXG84kp5Qx23QFkUfZ1NlovhE9GfrdQ445ipjp1547nqqoUbdp fzm95fqhDt74CI6lUE0nlUbzdEnrIvDZGNd4Npn+4Pp4MuS7c/IQLyOFWeHsHYN3 x1oAQgfz8pyYhxJ1TUR5fi69A8dY9iTsgSbSBLsjUSVsTXGjN+a42yYb6KOu8phv 8vSC0i0WG4NLAnm8mFJSHysmd8SRA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrjedvgdduheehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpe hpohhsthhgrhgvshhqlhdrohhrghenucfkphepudejgedrvddurdejiedrudehfeenucfr rghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrh drtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from ranger.suse (174-21-76-153.tukw.qwest.net [174.21.76.153]) by mail.messagingengine.com (Postfix) with ESMTPA id 3B5A0E446A; Tue, 29 Jan 2019 16:44:43 -0500 (EST) Subject: Re: How to set parameters in 'options'? To: Stefan Keller , Postgres General References: <884a4b56-9f35-9baf-50f2-7ba9176896a5@aklaver.com> From: Adrian Klaver Message-ID: <1c105129-c579-d85e-a310-480efa3baa7e@aklaver.com> Date: Tue, 29 Jan 2019 13:44:42 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/29/19 10:03 AM, Stefan Keller wrote: > Many thanks to Igor and Adrian for your hints. > > Got it to work like this: > > create function link_server(_server text, _host text, _port text, _dbname text) > returns void as $$ > begin > execute format('create server %s > foreign data wrapper postgres_fdw > options (host %L, port %L, dbname %L)', _server, _host, _port, _dbname); > end; > $$ language plpgsql; I would go with %I instead of %s for _server. > > select link_server('other_db_server', '111.11.11.11', '5432', 'other_db'); > > But I actually hoped being able avoid "execute"... Why? > > :Stefan > > Am Di., 29. Jan. 2019 um 16:21 Uhr schrieb Adrian Klaver > : >> >> 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 > -- Adrian Klaver adrian.klaver@aklaver.com