Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO6qI-0003il-NF for pgsql-general@arkaria.postgresql.org; Wed, 17 Aug 2022 00:23:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oO6qH-0005Py-5W for pgsql-general@arkaria.postgresql.org; Wed, 17 Aug 2022 00:23:29 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO6qF-0005Pp-IP for pgsql-general@lists.postgresql.org; Wed, 17 Aug 2022 00:23:28 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oO6q8-0006iQ-74 for pgsql-general@lists.postgresql.org; Wed, 17 Aug 2022 00:23:26 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id BA3AB5C00F8; Tue, 16 Aug 2022 20:23:17 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Tue, 16 Aug 2022 20:23:17 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1660695797; x= 1660782197; bh=FexjQFLhbgkb5DcSg0iVRT+T9sob7qAPPzMz2RgH8zU=; b=i jelvNCZbvvSjcWt9D6gQOfy63wCTzc1pmCjYtLAllXCxLcqIV/aEBpl/vMWdGRuO Uq1IGl4XimYa5ka1/lLVv54SFvyXJrtG0IGc99HnTtRwuhKDNNEOqfq+6jh4W6cN PcWrl+ccqXBgVQmIrYfvdmf+2CnsPlBYcOZgDbcA/ScK8ui2+k78sJ7UiLtg3odl QWx4eZW/LaHlSsfrLcQ0YHaSgnx4Sa778SC8wlhE7EySvudmnSvxHPAE/7vYb4Ip 3zKbae6dqGdyKzCnhfg95kBOWWbv/c6mzktkqQ0T7rROULY1xvFZnAm/TCrFSv/N MUFQeUZWDUhwkZwNCzvbw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1660695797; x=1660782197; bh=F exjQFLhbgkb5DcSg0iVRT+T9sob7qAPPzMz2RgH8zU=; b=lk3DDWnIarDEyfksX 0ozQ+q76JSSWg9PWrata2etpLaABInfemeyiKC8JOt5C1mvRE2gtH0OJYNqQjFeB iY8Iv03rRjMNlBqAignV+Ee9NulwPMGrurr1w5huVgzyza8XaWgnl0/ey27inTci It9Max0QdmvpWnE9jhYRvrY58eQfsk6wymqgZlaT5dXuiOOBd5op/XhEhTjINaq4 7JT9JHDxUM7tTFx7fYJcZ4/eeuF9oEsp+wqkZkrEK1zgVSVawjjCzNkV/vTb+ZGk 5FXqVVnwZt/Ci8UKtkWjPLqcfpq0ZjNF6H4PDyUpSQurJK5x/GgyqqSFq/Y6Jcjt zEgwg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdehhedgfeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvfhfhjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpeevvdekiefhudfhhfdtkeelgeffudeigfetjeegfffg hfetfeelledugfelleegveenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 16 Aug 2022 20:23:16 -0400 (EDT) Message-ID: <80215721-6a73-9dea-4fbb-52002194f717@aklaver.com> Date: Tue, 16 Aug 2022 17:23:16 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: plpython/python string formatting Content-Language: en-US To: Ted Toth , pgsql-general@lists.postgresql.org References: From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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