Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7vmW-0000Vi-Kx for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 05:34:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h7vmU-000761-UR for pgsql-general@arkaria.postgresql.org; Sun, 24 Mar 2019 05:34:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7vmU-00075u-JD for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 05:34:50 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h7vmN-0000pA-ON for pgsql-general@lists.postgresql.org; Sun, 24 Mar 2019 05:34:49 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x2O5Yfa5032148; Sun, 24 Mar 2019 01:34:41 -0400 From: Tom Lane To: Steve Baldwin cc: pgsql-general@lists.postgresql.org Subject: Re: regr_slope returning NULL In-reply-to: References: Comments: In-reply-to Steve Baldwin message dated "Sun, 24 Mar 2019 13:45:23 +1100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <32146.1553405681.1@sss.pgh.pa.us> Date: Sun, 24 Mar 2019 01:34:41 -0400 Message-ID: <32147.1553405681@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Steve Baldwin writes: > Consider the following: > ... > log=# select id, regr_slope(elapsed, ts) as trend from sb1 group by id; > id | trend > ------+---------------------- > c742 | > 317e | > 5fe6 | 5.78750952760444e-06 > 3441 | > (4 rows) Hm, I get regression=# select id, regr_slope(elapsed, ts) as trend from sb1 group by id; id | trend ------+----------------------- c742 | 19.607858781290517 317e | -1.0838511987808963 5fe6 | 5.787509483586743e-06 3441 | -3.828395463097356 (4 rows) What platform are you doing this on, and what exactly is the PG version? > If pg is correctly returning NULL, I'd be interested to understand the > circumstances under which this can occur. The source code shows two cases in which NULL would be returned: /* if N is 0 we should return NULL */ if (N < 1.0) PG_RETURN_NULL(); /* per spec, return NULL for a vertical line */ if (Sxx == 0) PG_RETURN_NULL(); Maybe the cases you're looking at are sufficiently numerically ill-conditioned that you could get Sxx == 0 depending on platform- specific roundoff error, but it seems fishy. regards, tom lane