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 1tJNtn-002ejK-6S for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 02:16:55 +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 1tJNsm-009XOC-DX for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 02:15:53 +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 1tJNsm-009XO4-6y for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 02:15:53 +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 1tJNsg-001HfL-0H for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 02:15:52 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfout.stl.internal (Postfix) with ESMTP id 3E3EE11401EE; Thu, 5 Dec 2024 21:15:45 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Thu, 05 Dec 2024 21:15:45 -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=1733451345; x=1733537745; bh=JGdseo+y3rfe8/EOP6SCUvmgsi79zsA1qo+QIqSiZDw=; b= C1T8Wwi0xF0gSiRBgxbvGfjn4L+L8SJs+EfXs4CXxJQDANQ4scKzC5kdr4QOqh+8 UVRWP0PA+zh+bcZc1OBkfPW4jMzEf9EJaVI+SHbSgnSwGt25B+keeEQ1hYTPPy72 DurfVAoAj6jd80OegITBZAQwtkYhtVFM7yFLCSwHJtdMCfboRgB3g+8Qfc7eBQYY iIj+mZ0HeGe+necTYOJT1RRNCkweRihA8O6O8cdXc/c9lC81dI5Jo66zvBrDAwxW bLtpm/JOfkNG681i74vrP24alyg5nhzZiFUO5jTCVpPNoIhCfcxGjVth0B/qtRe9 kE8kYrMf5Z0JbQHMLpkr1A== 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=1733451345; x=1733537745; bh=J Gdseo+y3rfe8/EOP6SCUvmgsi79zsA1qo+QIqSiZDw=; b=aE4RD65YSmskryocG Kgz4NqIYW/WNm7VrV2mE0QFEpymIETDtp7z5SUtjhKQv16M/acLyBGDa/nOTvsho L62WwvB6VU6Qt5B6dUgjV/gMUXFvYEAZHhV/g9Fn9dTGkxgiDKbDSM554M+RUXSA +dWiBf/0RL79VgsaF+QqX/hwlhB0sxRVjv3YeOePHg4j7DbBrfsNV1z/VGHaRl7U I/1XgAiWoGrcLVvsQ7f1FuRsZaLHKBDhxqO3MvdJ2Gjxaqb2OpHOg6Kvwz71rUd5 ryhEOKfWgYFegVgzY5QXydXKu0FpJqnMudM/mlGZXzJG/+BlTErvFulpnEozjszp JCRwA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrieekgdegfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpefkffggfgfuhffvfhgjtgfgsehtkeertddtvdejnecu hfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrg hklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepffffieeujeehheejueekheet gfevffdvhfekgeeltdeftdetledttdejtdelhedvnecuffhomhgrihhnpehpohhsthhgrh gvshhqlhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhl fhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprh gtphhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopegujhhohhhnshho nhesphhrohhgmhgrnhdruhhspdhrtghpthhtohepphhshigtohhpgheslhhishhtshdrph hoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 5 Dec 2024 21:15:44 -0500 (EST) Message-ID: <567f0d1f-c130-4abb-b2e4-dd6abdf89549@aklaver.com> Date: Thu, 5 Dec 2024 18:15:43 -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 18:00, Adrian Klaver wrote: > On 12/5/24 17:37, Adrian Klaver wrote: >> On 12/5/24 17:27, Daniel Johnson wrote: >> 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'))) > > Then there is the alternative I keep forgetting about, the function make_interval(): https://www.postgresql.org/docs/current/functions-datetime.html make_interval ( [ years int [, months int [, weeks int [, days int [, hours int [, mins int [, secs double precision ]]]]]]] ) → interval Then you could pass parameters to the function. -- Adrian Klaver adrian.klaver@aklaver.com