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 1tJNI6-002anx-Et for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 01:37:58 +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 1tJNI3-009Rus-Pz for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 01:37:57 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNI3-009Ruk-F1 for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 01:37:56 +0000 Received: from fhigh-b2-smtp.messagingengine.com ([202.12.124.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tJNHx-001H7r-Uj for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 01:37:55 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfhigh.stl.internal (Postfix) with ESMTP id 6974325401AE; Thu, 5 Dec 2024 20:37:48 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-08.internal (MEProxy); Thu, 05 Dec 2024 20:37:48 -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=1733449068; x=1733535468; bh=cB0nGp66xOYV7TSWHTaFIrJZd0I1Ma9/sMi5UJq6/Xk=; b= hl6fF1YaP4HZoys+FCRQ1vGS9IHNoDKWhdaBQFZAINV+qi1twRiZbkkCgguHtl78 BZQXyC9LfluPdDUEBYC0E+OA3yNeL6onHn6nBaB6wry8OIgizjVq5wTYe/VdEE7X Z9ixvu79xZmPWZWxgYUJ9hpU/v+lkj4G/h4vv5pHAIZ1N8a4kP7AHLEn4QaIu6iC iuq5wheHMM7d552hcxv0YXKfSL9lBzDFXCcRxctI8UjXhiAGj7G4ImU0yBaGPi7K rJx603lM123rzSSysWDgSauLV0r7gftGX9H4mrZIMq0AxV06T1Zf/QWW0ynFckHb KgPHg+pGShgc3Q4ji0vDqg== 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=1733449068; x=1733535468; bh=c B0nGp66xOYV7TSWHTaFIrJZd0I1Ma9/sMi5UJq6/Xk=; b=aEdYpPnXeQru3xlca C917IgZYWWhp5DfpLcFps4oTuohSgGgVgEJYJM6IKWTHFrmC7nOTS1/+TfQfqpu+ bxLILQF2z2ibqWlU5Kz0W2gRXUlCq4wrzGkJsFJwCx8GrZhPB+FzZDKpw9gB3LhQ 41Rga1aNekp/4bGEtpl/xLwh4840e78h1wxkHz+o3O8m+1b6HiuPlM4de1oALuuR pTFK65mjf0vVLHOqyBNJiFhjhbaY7ND5a+u1LL2PCnlmMhfwNvb18NsP1n5cYh1H WKaw7TdKEbxBrf3Yy76tKfyWQloBqwcD7+1yPBX17JCjIDoCA74w0kVdCqHG9Iip nAGpg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrieekgdefhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertddtvdejnecu hfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrg hklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepudelgffhleegueetleefteeu ieffgedtkedvledtueeuledvtdekgfdviefgiefgnecuffhomhgrihhnpehpshihtghoph hgrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpth htohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepughjohhhnhhsohhnsehp rhhoghhmrghnrdhushdprhgtphhtthhopehpshihtghophhgsehlihhsthhsrdhpohhsth hgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 5 Dec 2024 20:37:47 -0500 (EST) Message-ID: Date: Thu, 5 Dec 2024 17:37:46 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Parameter in SQL query being misinterpreted To: Daniel Johnson , psycopg@lists.postgresql.org References: Content-Language: en-US From: Adrian Klaver 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: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 > > > Here's the command from Python.  The value we're looking at is for > "nextupdate".  "Stats" is a dictionary which contains "NextDBLog" as an > integer whose value is something between 120 and 800. > =-=-=-=-=-=-=-=-=- > dbRWcur.execute(''' >     INSERT INTO backendstatus >         (nodeid, debug, started, lastupdated, nextupdate, version, > platform, python, cpucores, state, uptime, taskstats) >     VALUES >         (%(nodeid)s, %(debug)s, %(laststarteddb)s, NOW(), NOW() + > INTERVAL '%(NextDBLog)s SECOND', %(version)s, %(platform)s, %(python)s, > %(cpucores)s, %(state)s, %(uptime)s, %(statsstring)s) >     ON CONFLICT >         (nodeid) >     DO UPDATE SET >         debug       = excluded.debug, >         started     = excluded.started, >         lastupdated = excluded.lastupdated, >         nextupdate  = excluded.nextupdate, >         version     = excluded.version, >         platform    = excluded.platform, >         python      = excluded.python, >         cpucores    = excluded.cpucores, >         state       = excluded.state, >         uptime      = excluded.uptime, >         taskstats   = excluded.taskstats; >     ''', Stats) > =-=-=-=-=-=-=-=-=- > > > After the command is processed this is the contents of > dbRWcur._query.query.decode('utf-8'). > =-=-=-=-=-=-=-=-=- > INSERT INTO backendstatus >     (nodeid, debug, started, lastupdated, nextupdate, version, > platform, python, cpucores, state, uptime, taskstats) > VALUES >     ($1, $2, $3, NOW(), NOW() + INTERVAL '$4 SECOND', $5, $6, $7, $8, > $9, $10, $11) > ON CONFLICT >     (nodeid) > DO UPDATE SET >     debug       = excluded.debug, >     started     = excluded.started, >     lastupdated = excluded.lastupdated, >     nextupdate  = excluded.nextupdate, >     version     = excluded.version, >     platform    = excluded.platform, >     python      = excluded.python, >     cpucores    = excluded.cpucores, >     state       = excluded.state, >     uptime      = excluded.uptime, >     taskstats   = excluded.taskstats; > =-=-=-=-=-=-=-=-=- > > This is being run on Rocky Linux v9.5 (essentially Red Hat / RHEL).  The > relevant installed packages are: >     python3-3.9.19-8.el9_5.1.x86_64 >     python3-psycopg3-3.1.18-4.el9.noarch  (from EPEL) >     postgresql-server-15.8-2.module+el9.5.0+28955+a22540b0.x86_64 > > > > Daniel Johnson > djohnson@progman.us > > > -- Adrian Klaver adrian.klaver@aklaver.com