Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNdd-002ctm-KQ for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 02:00:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tJNdZ-009TGT-8Q for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 02:00:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNdZ-009TGL-1j for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 02:00:10 +0000 Received: from fout-b1-smtp.messagingengine.com ([202.12.124.144]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNdU-001HYN-Iw for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 02:00:08 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 3D4FF114012E; Thu, 5 Dec 2024 21:00:03 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Thu, 05 Dec 2024 21:00:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1733450403; x=1733536803; bh=mnBT6LD2MLX6WxkyOmkXU8rC9B4BYv9vkVM6I/6R+AM=; b= ZKhzhTG/uyhufmT0CcgM+7nfbmSsymi+SgH+9xserqGE7onARu5Ya7a79Ukc1IL4 yaIhV6MKq4vSXn7TXyx2mPwp4zRsJanEhcXNUs5WtroPe9HMrgp4JILioYNTdKS+ VUIw3UK4WH/tV7JV/KmwYnebF+6NG0FocCQpgZCKxWmSML5UADhiFe3o1azbcHg9 blsOTeWcqTAJHqW2mG5cIA6LUcca4oevsARHasxIsUzDz7VCunsx7r5vWKzjj4Oz SrtkwKVVrhSOKjZgD+vGre5frh5g/s1vYaV8X+RZttOI6QPFuTAW/SeuWe9zZjj0 /911dR7cZsRcnySy4VRr9Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1733450403; x=1733536803; bh=m nBT6LD2MLX6WxkyOmkXU8rC9B4BYv9vkVM6I/6R+AM=; b=zf7pdjOvtE13aEJWI PIj9VeFxU1BxyAjVecN3Zhb6LeWu6UHI3HZPvlfKNVW8OhZfWTcCEfvJ4eQR5/kr jtQHNvaEwwT5x2RNu2j2rg76YryDuCNbCA4j3+kI8srJTHoDCeEQlRxWsF5uudvH Qq/n9dKj8xTNFZrWCtBwXG84pf/Aju1t9gTivBXDNJWMlB/VaKuyRj37oDhWpBCl 1sjeFr0FepKYJg1DuwWFwMBIqcK37WMNlmHY211R3EqS4D0e4VJRs1RxnbAIgTdk jw1PYFZo6qBY4hGZ8hC0HDeMCv+u5EXeVzFil+8hSGErpy5fb4PXdkxY5Xexc5SC Aolxw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrieekgdegtdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpefkffggfgfuhffvfhgjtgfgsehtkeertddtvdejnecu hfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrg hklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepgffgleduleehfeelffekjeef leehteejfeduieelvdeffffhgffgtdeuleetgfeunecuffhomhgrihhnpehpshihtghoph hgrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpth htohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepughjohhhnhhsohhnsehp rhhoghhmrghnrdhushdprhgtphhtthhopehpshihtghophhgsehlihhsthhsrdhpohhsth hgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 5 Dec 2024 21:00:01 -0500 (EST) Message-ID: Date: Thu, 5 Dec 2024 18:00:00 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Parameter in SQL query being misinterpreted From: Adrian Klaver To: Daniel Johnson , psycopg@lists.postgresql.org References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/5/24 17:37, Adrian Klaver wrote: > On 12/5/24 17:27, Daniel Johnson wrote: >> Good day!  I've run into a quirk executing an SQL statement and am not >> sure if it's a fundamental issue with my query or a bug in psycopg3. >> >> In short, I'm inserting/updating a row using both NOW() and 'NOW() >> plus a varying value'.  The NOW() value is fine, but what ends up in >> the database for the second one is just "NOW() + 4 seconds".  I >> finally realized this is because of how the command is being >> processed, the variable becomes "$4" and apparently the dollar sign is >> lost. >> >> I'm guessing this is an example of why one of the bullet points at >> https://www.psycopg.org/psycopg3/docs/basic/params.html says "The >> placeholder must not be quoted".  :-/  I would appreciate any advice >> on how to work around this while still passing in the value safely > > Pretty sure the below does not work because the placeholder is becoming > part of the literal string: > > NOW() + INTERVAL '%(NextDBLog)s SECOND' > > You might try something like: > > NOW() + INTERVAL || ' ' ||  %(NextDBLog)s || ' SECOND' > > If that does work then you will need to use the psycopg.sql module to > build that part of the query >> What I ended getting to work: from psycopg import sql cur.execute(sql.SQL("select now(), now() + ({} || ' seconds')::interval ").format(sql.Literal(4))) cur.fetchone() (datetime.datetime(2024, 12, 5, 17, 57, 55, 670218, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')), datetime.datetime(2024, 12, 5, 17, 57, 59, 670218, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific'))) -- Adrian Klaver adrian.klaver@aklaver.com