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 1tJN8I-002ZcG-Ss for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 01:27:51 +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 1tJN8G-009RBY-Co for psycopg@arkaria.postgresql.org; Fri, 06 Dec 2024 01:27:49 +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 1tJN8G-009RBP-1H for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 01:27:49 +0000 Received: from mail-oo1-xc29.google.com ([2607:f8b0:4864:20::c29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tJN8D-001H4N-Su for psycopg@lists.postgresql.org; Fri, 06 Dec 2024 01:27:47 +0000 Received: by mail-oo1-xc29.google.com with SMTP id 006d021491bc7-5f201a932b2so710923eaf.3 for ; Thu, 05 Dec 2024 17:27:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=progman-us.20230601.gappssmtp.com; s=20230601; t=1733448464; x=1734053264; darn=lists.postgresql.org; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Vfn8FblYwhGf/qSk4EX+v/oveLRiUVjVSR0WlAOAkC0=; b=MS9J+KlDytXcTSAzyLUhz/kToXECQd58BHudURkM/p9GvCoIzyP7VszpfkDJ0A3jwz pXn0EL5I9r33ppZ+SUA6O6VDM40PDYckz0amgnKERoP6lJshRH3sC3+A+xf3DCTJpiQ3 cbHP1DqfhxFwzt0XIWpAegKzKBCN3LaUi4n/iSc7bF/zYKJQ/Llu3hhUyl4t28tC0hqp R8nreYVaiqAlxWTtf4nvicCC8SktrKjZ+dWgP4PJ9ML+SGfnoXM4HPeFaBXBfS9VXDAE qjk4ibGX0vjGewuhVua6ODd+tw29uKvGhPlHDNhAbi8R0U95P11KKXPEZWAP9ZO8JgIl UqQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733448464; x=1734053264; h=content-transfer-encoding:subject:from:to:content-language :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Vfn8FblYwhGf/qSk4EX+v/oveLRiUVjVSR0WlAOAkC0=; b=RH/9VQIaW+Oy8CsjwAg8TzI+RwFiJ1sXZ0KI57bsIUC1FJAxQoj53gRNeE2X1BYUA/ nd54d7d5w/Ipn/80wuJcVkGoicMl4PfSs2IwpG2MxTFJ4iZ19A7P8AGKOCVpIK5u9nJ+ +gjx+IUZUjofYbzUOJETWQqJMlWf4jY2Ya36Ok4Wp/4sGmEvJDQtVw/AqRx2r8yBK6ue jdp+PhSCF3qZe616ik0OU9G/XNJEdsWrhekJMwN6MFmfP/OSGAEYbX5Heg4CsQK8lByb QTFfVxJcSdvkamfIBv2+vjZj9nms7fV5XvuEFg5ZdBr+wSoNjtpG60frxORc3+uMj6Jy TPQw== X-Gm-Message-State: AOJu0Yw0RmnaKF/7K4BuPlidAqD/MkfQ0KABqGrvzHmxTRAlFo6wQytQ 4l8iuJJ6Z3Nhc5os23XskrXUGBDqFD9fiG+LQHyZrbWTiefNLrWoGrbyEAIi5JW9lYku1H8XUZn u X-Gm-Gg: ASbGncuiwrjsRTHJNxb5+7QnISiYcfiGunnOMQ6ndsY+mMwbQ+sf2FqeaJpunw0Yg0Z YqLHHCbMz+hz0+dyJzfqAqjcddaBXQeAiuIRBls/Madoj8vmHyesqZSH6tGpvjbrRKlwDLp60/m fZKuayy7XfMxlmO2B3SHfKPyA99omU8ZiuhsqNQTpp5tPHXFECoB5alWdybEx+1ziLiL83GkZst FV6jfIqeTcXGP6hM44NR+SsNXOzNqAYmVRcoCD/Wyx83pDEJ43/knlpP8/m45ID1TmqVD3Rgavv cQ0Hqd32uQ2eXpwOdkKdHkc3+sn+ X-Google-Smtp-Source: AGHT+IE27QaWD39Iwx5g9mnk8amNoTstbWBLqONsplT0BlWwqvoPjfaZ+y3dze2JgDqRia1o3N8Yhw== X-Received: by 2002:a05:6820:1ad5:b0:5f2:9aa:582a with SMTP id 006d021491bc7-5f28713111fmr977000eaf.7.1733448464587; Thu, 05 Dec 2024 17:27:44 -0800 (PST) Received: from [192.168.240.43] (69-110-49-32.lightspeed.okcbok.sbcglobal.net. [69.110.49.32]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-5f279246368sm525514eaf.20.2024.12.05.17.27.44 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 05 Dec 2024 17:27:44 -0800 (PST) Message-ID: Date: Thu, 5 Dec 2024 19:27:43 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: psycopg@lists.postgresql.org From: Daniel Johnson Subject: Parameter in SQL query being misinterpreted 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 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. 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