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 1mRAIQ-0007Ke-4J for pgsql-general@arkaria.postgresql.org; Fri, 17 Sep 2021 09:36:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mRAIO-0001GN-Ur for pgsql-general@arkaria.postgresql.org; Fri, 17 Sep 2021 09:36:36 +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 1mRAIO-0001GF-MB for pgsql-general@lists.postgresql.org; Fri, 17 Sep 2021 09:36:36 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mRAIM-0004tS-IT for pgsql-general@lists.postgresql.org; Fri, 17 Sep 2021 09:36:35 +0000 Received: by mail-ed1-x529.google.com with SMTP id h17so27764613edj.6 for ; Fri, 17 Sep 2021 02:36:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=DEiPPYa28Eqyhxe8qmlubF+XnRFhTpuAWnzi/peYnnQ=; b=qjzCycCGSD0RkNc9f0gPAS2d7gF1IwgIhoTh2pt7oGG1nvaiKhqgz8xWJWKgbwkYVU ZNsTcxbSI7t+SHkkFjE3WiEa+fu1JPHjO04PPX+S0Kb/B9khX/0c5UDaRuVg/vw6+CNj 3RompdpbXHjFmb8DtQVoUvNyFY/+ZtG/tON2ftETv4z1OVynRlPnsNwXesZnujH/use5 I8WBbqOAjaFyR99gM6PpQPn2+4Z2uAoD5ectX8sMkE+da31AxJ5bblBtw4eZunIeVodx /5UVv7RmVS1/tdVxnp8sS2A8InDn3lyuqNecQt4pGemsKwRFoutq+uyWgPKlkEeEKPoc BC5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=DEiPPYa28Eqyhxe8qmlubF+XnRFhTpuAWnzi/peYnnQ=; b=f1sNNr8tZaDZcu6RO0Vm7dVAUW6v/u4Lud4HUj8mDUC+48SuzzvHz9izQAVQUy01U9 zG6xk2MaR3GNVk0VujnoEf87SiSpmX/jMtvQjiSxNjglEUpnXQ0VneIo94vP6Iz7C7cA SH5+VKo7kT1aw89YFzs8iDDz8msn7iU5+mCBwU6R/3SaEkqbhiTza+dZD1ent1Qie5OT AqnWBJexl7r1JcV/32GNMn0ecCQnzN6sqVQgqFjXCbLdAgNRFS5fcXTUnVSb9xNaa45f TrV3aMr/rjrie3lHeb6WXMvo+h0oXGMJNttDod7xOz5gw1tZ1aD/ByUtbLOwGltMpurD u+Kg== X-Gm-Message-State: AOAM533bDDAvNCGUpw8rzVzukCU1pMnySgXIDgjQ86/sJP9SjE7zoZII svYuwFycQ4IqTrNGiBbUd6E= X-Google-Smtp-Source: ABdhPJxTAkujyeNfrtyIuUqQJGrRNG7zT3g5ia5jSa+pYEfAGL+M4sbZwFSZZs3XWU8iFGDa/M/aEg== X-Received: by 2002:a17:906:7053:: with SMTP id r19mr10942853ejj.476.1631871392887; Fri, 17 Sep 2021 02:36:32 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id la1sm2076831ejc.48.2021.09.17.02.36.32 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Sep 2021 02:36:32 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: SELECT FOR UPDATE returns zero rows with CTE From: Alban Hertroys In-Reply-To: Date: Fri, 17 Sep 2021 11:36:31 +0200 Cc: pgsql-general@lists.postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Roman Guryanov X-Mailer: Apple Mail (2.3608.120.23.2.7) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 17 Sep 2021, at 8:32, Roman Guryanov = wrote: >=20 > Hello, could you check my problem. > Why does SELECT FOR UPDATE return 0 rows in the scenario below? = (execution in transaction) >=20 > If delete 'FOR UPDATE', 1 row returned >=20 > Test case: > DROP TABLE IF EXISTS > t1; >=20 > CREATE TABLE t1 (_pk serial, t1c1 integer, t1c2 integer > , t1c3 text); >=20 > insert into t1 (t1c1, t1c2, t1c3) values(123456789, 100, = 'string_value_1' > ); (=E2=80=A6cut everything related to unused t2=E2=80=A6) >=20 > WITH > cte1=20 > AS > ( =20 > UPDATE > t1 > SET t1c3 =3D 'string_value_1' > WHERE t1c1 =3D 123456789 > returning t1c1, t1c2 > ), > cte2=20 > AS > ( > SELECT * FROM > t1 =20 > WHERE t1c1 =3D 123456789 =20 > AND t1c2 =3D (SELECT t1c2 FROM cte1) > FOR UPDATE > ) >=20 > SELECT * FROM cte2; Most likely the outer select returns 0 rows because you locked the rows = you expected in cte2 and didn=E2=80=99t perform an update on those = locked rows yet. I suspect your intention for this query is to first lock the rows, then = update them and then select them, but instead you start with updating = them, then lock those rows after the fact and then you try to select = those locked rows. Also, selecting the updated rows by t1c2 in cte2 seems rather risky, as = that is a rather different selection criterium than you use for the = actual update. It=E2=80=99s okay for this single-row example, but if you = had a table full of data, you would now have locked all rows with the = value t1c2 =3D 100 for update. If that update never happens (or the = locking doesn=E2=80=99t get rolled back), well=E2=80=A6 Regards, Alban Hertroys -- There is always an exception to always.