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 1sjETo-00Gaor-Su for pgsql-general@arkaria.postgresql.org; Wed, 28 Aug 2024 08:56:41 +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 1sjETm-0023fU-VD for pgsql-general@arkaria.postgresql.org; Wed, 28 Aug 2024 08:56:39 +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 1sjETm-0023fM-G3 for pgsql-general@lists.postgresql.org; Wed, 28 Aug 2024 08:56:39 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sjETj-001tYH-Fo for pgsql-general@lists.postgresql.org; Wed, 28 Aug 2024 08:56:38 +0000 Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-429ec9f2155so56615245e9.2 for ; Wed, 28 Aug 2024 01:56:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1724835396; x=1725440196; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=BwvqWIpLmGbiDhRsVfaa6nCUj/q7XtMsjQRgx7dshqg=; b=jP3QTVLrNQZiB5LWb1fo0tx1SIlYCcoaFu9Zs5s6PKjGZ2Sqi9J27gj7mWZacC/YM5 7tbrbaXDVuMyUrlDrDQtKfS03pU7rIGy8UQbAzGlfKOMfv+h5NBn9zWYEYNvPnYT5sD0 Q3fkWT9Iq7vKttWc6LR/0l05kkLLD3J4Owgf0zfszDc5ERCnbhWjbVyU+VZ45oCfB8cV b6g+9N0U9YcBNb0sbPfuKTxW+4qh2J+7sjRTB30JT9BtS43O8BUg5iQMABRUnHaPQaMh 3MUBBOo3TKlwejDXwBjJBfItT+Tq+vvX/lP7es5YtlddCaYJEyHtzlD+oh2bG1U9rFat p7bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724835396; x=1725440196; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=BwvqWIpLmGbiDhRsVfaa6nCUj/q7XtMsjQRgx7dshqg=; b=WI9Ld4/mI5jOpSr8A2mrojNAlNon93Ax4jar2T14ViEGGVaE0ko5UB+rjXAoHRxaW7 nvC5sOCKFTCDoPd6Goc+g35oIhjym9qfOIMMyEJIjP5oo+e8GheRUhZjeVDmH289l079 kBdT6SzSCST+pbFdXyqJbFFBa7go3Lk+YcqorN9EpUBIM4c8EHcTpfnkIYwo70CWAXiw RyXlGXcRzFbMGkP+0KtWK67JuQUXYMnDbWtuKeGglOku9oE80l3mwqS0/46Z9A2RSljP H0mz1pKYG3jzjYvKTAY63za0r/nCJttDrum+FG/VRM4YGk5otjWVwjrkmQJ0r3ngCFUj kCmQ== X-Forwarded-Encrypted: i=1; AJvYcCUXxWpHupxadghVmX8g05kACayTDtYLHT9wKJgyur50VFpFM5MES0RmCJSgWaUwRS54nqOMM2xDxGvbGJ1S@lists.postgresql.org X-Gm-Message-State: AOJu0Yy2J+vFcxd7ITvyj09EW22k5xf+BaB8wbmJ0eRTU5DHM6oRi5Ot H0NGOdfcBy+bUARIQxnwoqhGROXugwFUjDAB2/nHTGQmlrcvSV+H2MNWFroE1v8= X-Google-Smtp-Source: AGHT+IGb4/zxzd5PcUiBk+61xKm3aLAagusvdZhOrVnvDsLUwkUmeZgNvIGLSnUkL7U5IaDd6yiTEQ== X-Received: by 2002:a05:600c:35cc:b0:426:60e4:c691 with SMTP id 5b1f17b1804b1-42acc8de777mr104981785e9.11.1724835395421; Wed, 28 Aug 2024 01:56:35 -0700 (PDT) Received: from [192.168.241.211] ([41.66.99.77]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-42ba639687csm14202575e9.8.2024.08.28.01.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Aug 2024 01:56:35 -0700 (PDT) Message-ID: <9e38681b60ada2a1161cc278344678ca563bce83.camel@cybertec.at> Subject: Re: Strange behaviors with ranges From: Laurenz Albe To: Jean-Christophe Boggio , pgsql-general@lists.postgresql.org Date: Wed, 28 Aug 2024 10:56:34 +0200 In-Reply-To: <77691006-45f1-4ef9-bc1b-2eecd44f7d0d@thefreecat.org> References: <77691006-45f1-4ef9-bc1b-2eecd44f7d0d@thefreecat.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2024-08-27 at 19:29 +0200, Jean-Christophe Boggio wrote: > I have 2 very confusing behaviors when using ranges. >=20 > It all started with this query: >=20 > =C2=A0=C2=A0=C2=A0=C2=A0 WITH rangespaliers AS ( > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT numrange( qtep1+1= =C2=A0=C2=A0 , qtep2,=C2=A0 '[]') AS rangep FROM=20 > paliers JOIN tmp_limitcontrats USING(idcontrat) > --=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 SELECT numrange( qtep1+1=C2= =A0=C2=A0 , qtep2,=C2=A0 '[]') AS rangep FROM=20 > paliers WHERE idcontrat=3D1003 > =C2=A0=C2=A0=C2=A0 =C2=A0) > =C2=A0=C2=A0=C2=A0 =C2=A0,rangespaliers2 AS ( > =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 select * > =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 FROM rangespaliers > =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 WHERE rangep <> NUMRANGE(NULL= , NULL) -- ERROR IS HERE > =C2=A0=C2=A0=C2=A0 =C2=A0) > =C2=A0=C2=A0=C2=A0 =C2=A0select * from rangespaliers2; >=20 > When I run this query, I get the error "Range lower bound must be less= =20 > than or equal to range upper bound". >=20 > (a) If I comment out the line marked "ERROR IS HERE", I don't have an=20 > error (but I'm missing the filter of course). >=20 > (b) Also, if I uncomment line 3 and comment out line 2, I get the=20 > correct behavior. Very strange thing is that tmp_limitcontrats has only= =20 > one row which contains "idcontrat=3D1003". >=20 > Now, in that table "paliers", the line for idcontrat=3D1003 has value NUL= L=20 > for both qtep1 and qtep2. So the final behavior should be an empty=20 > result set. The explanation is in the execution plans. With your sample data, the plan looks like Hash Join Hash Cond: (tmp_limitcontrats.idcontrat =3D paliers.idcontrat) -> Seq Scan on tmp_limitcontrats -> Hash -> Seq Scan on paliers Filter: (numrange(((qtep1 + 1))::numeric, (qtep2)::numeric) = <> '(,)'::numrange) If you remove the WHERE condition from the second CTE, the plan becomes Hash Join Hash Cond: (tmp_limitcontrats.idcontrat =3D paliers.idcontrat) -> Seq Scan on tmp_limitcontrats -> Hash -> Seq Scan on paliers In the second case, "rangep" is never used, so PostgreSQL optimizes the query so that it does not calculate the column at all, which avoids the runtime error. Yours, Laurenz Albe