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 1sj0fR-00CfNg-D4 for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2024 18:11:45 +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 1sj0fO-00AkLz-Ty for pgsql-general@arkaria.postgresql.org; Tue, 27 Aug 2024 18:11:43 +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 1sj0fO-00AkLp-DQ for pgsql-general@lists.postgresql.org; Tue, 27 Aug 2024 18:11:43 +0000 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sj0fK-001nMX-Vm for pgsql-general@lists.postgresql.org; Tue, 27 Aug 2024 18:11:42 +0000 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-202089e57d8so37039755ad.0 for ; Tue, 27 Aug 2024 11:11:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20230601.gappssmtp.com; s=20230601; t=1724782297; x=1725387097; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=ghd5tQwY8/xM672eyMK4a5kEdbp0cMzGBd+GKUDLH3E=; b=vJWwEN7uo9cYO0hmZtNlbsHz/yK8hPIJd+ZXB85WiS9/ML4tZqwOxhEWurU8kyQcDs CD2bPGulV3NqFXMxVaFOnbkQ6wrb9MEXKxQbbMJiFHrId7PxuZ4flotXzg/4LLSQYBJf UsBgA/RMgB9aeZa5HpX2ujkfmJDlWaz6Bi+IboUCJ9KKCFMRvNUZ+PTmxowk+b+Q6KTw IXUb2t4M6EEjdkbztGnWAWneqygoRe0ytYlnwSbuEl/UBmtMg9KBAtybILCk0NhsZJVR JytNX09zMJrReWykboo567igAsWCElUDXfIxaimHXtldsUqtFPjO7e+deSq5iQs75t7M llWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724782297; x=1725387097; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ghd5tQwY8/xM672eyMK4a5kEdbp0cMzGBd+GKUDLH3E=; b=CnDd1T7tYl/ju/pMDJ3CYirzErcZJDGYOlrLlgmMbMqzMP2/Nq6nKh/T7oHnhVPWRc MvzDTAxz/nlCSHgESBB4Ocm4ceLpYK//MfAfp3UFoX+f5ztOV/jYXCayZBpSCYC5kNz6 tiWwXGvCBPSeW6z3osJCwdxf903Ip9HfgKsxkLaJXtuMCWDlVJXIpshWe1nH18yXSW52 sS9lVxY3d7b5gaBPiDkp+lsc20duXTWQYxYWgXOlU/5LZOzWIQbQHUPkz0UAmtURmhaF rO4GYPPb/kA5QSzLAGV2+tKUzWMlcsKh//1OlsbYjhyK3vgi+QkXPPPSAo6AkVypRgbd CwEQ== X-Gm-Message-State: AOJu0YxmBimEYn52kaBLUCQETT06ipmHUGmLrgDSOWw0WAfq1VKMHpaq d5DAX/9/1ZOvXNglhYVmJLxD+A8BHg/S4BBmy/K59wwOaHtWtBIHl5eCXSolluWFrIRDMkeuMQA r X-Google-Smtp-Source: AGHT+IH+/4IlAdFeylWtBpOlTcdOEBree9T8w+LCYyUD/Q4MCxAhltiTiLT86JMKcQUX8dRHJxqvQQ== X-Received: by 2002:a17:903:41c9:b0:1ff:43a8:22f2 with SMTP id d9443c01a7336-204f6834354mr457325ad.24.1724782296721; Tue, 27 Aug 2024 11:11:36 -0700 (PDT) Received: from [192.168.2.139] ([50.39.255.79]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-2038560a2e9sm85232745ad.216.2024.08.27.11.11.35 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 27 Aug 2024 11:11:36 -0700 (PDT) Message-ID: Date: Tue, 27 Aug 2024 11:11:35 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Strange behaviors with ranges To: pgsql-general@lists.postgresql.org References: <77691006-45f1-4ef9-bc1b-2eecd44f7d0d@thefreecat.org> Content-Language: en-US From: Paul Jungwirth In-Reply-To: <77691006-45f1-4ef9-bc1b-2eecd44f7d0d@thefreecat.org> 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 8/27/24 10:29, Jean-Christophe Boggio wrote: > I have 2 very confusing behaviors when using ranges. > > It all started with this query: > >     WITH rangespaliers AS ( >         SELECT numrange( qtep1+1   , qtep2,  '[]') AS rangep FROM paliers JOIN tmp_limitcontrats > USING(idcontrat) > --        SELECT numrange( qtep1+1   , qtep2,  '[]') AS rangep FROM paliers WHERE idcontrat=1003 >     ) >     ,rangespaliers2 AS ( >         select * >         FROM rangespaliers >         WHERE rangep <> NUMRANGE(NULL, NULL) -- ERROR IS HERE >     ) >     select * from rangespaliers2; > > When I run this query, I get the error "Range lower bound must be less than or equal to range upper > bound". > > (a) If I comment out the line marked "ERROR IS HERE", I don't have an error (but I'm missing the > filter of course). > > (b) Also, if I uncomment line 3 and comment out line 2, I get the correct behavior. Very strange > thing is that tmp_limitcontrats has only one row which contains "idcontrat=1003". The issue is the order-of-operations used by the planner. If I put EXPLAIN on your last query, I see: Hash Join (cost=16.64..109.90 rows=2410 width=64) Hash Cond: (tmp_limitcontrats.idcontrat = paliers.idcontrat) -> Seq Scan on tmp_limitcontrats (cost=0.00..35.50 rows=2550 width=4) -> Hash (cost=14.27..14.27 rows=189 width=12) -> Seq Scan on paliers (cost=0.00..14.27 rows=189 width=12) Filter: (numrange(((qtep1 + 1))::numeric, (qtep2)::numeric) <> '(,)'::numrange) So we are applying that filter to every row in paliers, not just the one with idcontrat = 1003. Indeed this simplified version also fails: SELECT numrange(qtep1+1,qtep2) as rangep FROM paliers; Yours, -- Paul ~{:-) pj@illuminatedcomputing.com