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 1jMYNC-0004Wa-8U for pgsql-general@arkaria.postgresql.org; Thu, 09 Apr 2020 14:41:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jMYNB-0000Dl-5x for pgsql-general@arkaria.postgresql.org; Thu, 09 Apr 2020 14:41:41 +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 1jMYNA-00009G-Uv for pgsql-general@lists.postgresql.org; Thu, 09 Apr 2020 14:41:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jMYN8-00031w-LK for pgsql-general@postgresql.org; Thu, 09 Apr 2020 14:41:39 +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 039Efbdb005557; Thu, 9 Apr 2020 10:41:37 -0400 From: Tom Lane To: David Gauthier cc: Postgres General Subject: Re: order by not working in view ? In-reply-to: References: Comments: In-reply-to David Gauthier message dated "Thu, 09 Apr 2020 10:33:26 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <5554.1586443297.1@sss.pgh.pa.us> Date: Thu, 09 Apr 2020 10:41:37 -0400 Message-ID: <5555.1586443297@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David Gauthier writes: > In the copy/paste below, first 2 lines returned by a select on the view, > why didn't it sort on start_datetime correctly ? Putting an ORDER BY in a view is a bit dangerous (last I looked, it wasn't even legal in standard SQL). Yeah, the view will sort, but there is nothing compelling the calling query to preserve the ordering. EXPLAIN would give you more info, but I'm betting that the IN is being converted to a semijoin and then done with a non-order-preserving join method. regards, tom lane