Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aT7c9-0000Z3-Ik for pgsql-sql@arkaria.postgresql.org; Tue, 09 Feb 2016 12:41:53 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aT7c8-0004iV-DZ for pgsql-sql@arkaria.postgresql.org; Tue, 09 Feb 2016 12:41:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aT7c6-0004h3-J6 for pgsql-sql@postgresql.org; Tue, 09 Feb 2016 12:41:50 +0000 Received: from aibo.runbox.com ([91.220.196.211]) by magus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aT7c2-0003cs-EE for pgsql-sql@postgresql.org; Tue, 09 Feb 2016 12:41:49 +0000 Received: from [10.9.9.207] (helo=mailfront03.runbox.com) by bars.runbox.com with esmtp (Exim 4.71) (envelope-from ) id 1aT7bw-0008LS-K2; Tue, 09 Feb 2016 13:41:40 +0100 Received: from cpe-76-176-177-1.san.res.rr.com ([76.176.177.1] helo=seasyslap4) by mailfront03.runbox.com with esmtpsa (uid:561468 ) (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) id 1aT7bj-0003Vj-BZ; Tue, 09 Feb 2016 13:41:27 +0100 From: "Mike Sofen" To: "'Venkatesan, Sekhar'" , "'Adrian Klaver'" , "'David G. Johnston'" Cc: "'Tom Lane'" , References: <21082.1454994009@sss.pgh.pa.us> <56B980D1.7010509@aklaver.com> In-Reply-To: Subject: Re: Question on PostgreSQL DB behavior w.r.t JOIN and sort order. Date: Tue, 9 Feb 2016 04:41:16 -0800 Message-ID: <029801d16337$2ca30f90$85e92eb0$@runbox.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Content-Language: en-us Thread-Index: AQMXg80j7mym7Udm3sOj/1xyLd/21wJLCRJGAb94P/gAosthCwGaKjVFArpir0AC6zWOVQF0mHl4AkkkdaCcGg20kA== X-Pg-Spam-Score: -2.9 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Actually, the behavior you've seen in SQL Server may be a pure artifact of = the table structures underneath your queries.=20=20 Most database architects will (appropriately) put a primary key on every ta= ble and the default in SQL Server is to make primary keys clustered...and c= lustering arranges the physical storage of the rows in the increasing order= of that key. If that is what exists in the SQL Server db, then KH_.r_obje= ct_id would be a clustered PK and so of course would return rows in that or= der, automatically. As Kellerer said, otherwise it is random ordering, wit= hout an Order By clause. Postgres PKs are not clustered by default, so you'll experience the random = row ordering you mentioned. Cluster that column and you'll get the same be= havior...but read up on postgres clustering since it works very differently= than SQL Server. Mike -----Original Message----- From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org= ] On Behalf Of Venkatesan, Sekhar Sent: Monday, February 08, 2016 10:15 PM To: Adrian Klaver ; David G. Johnston Cc: Tom Lane ; pgsql-sql@postgresql.org Subject: Re: [SQL] Question on PostgreSQL DB behavior w.r.t JOIN and sort o= rder. My concern here is that I want to maintain consistency ( in our application= to retain sort order) between different databases. I don't see the issue in SQL Server and Oracle databases.=20 "SELECT KH_.r_object_id, KH_.object_name FROM dbo.dm_location_s AS = ZS_ INNER JOIN dbo.dm_sysobject_s AS KH_ ON ZS_.r_object_id =3D KH_.= r_object_id " The above query is sorted based on the first column in the select list. Sam= e is not happening in PostgreSQL. Is this something to do with collation setting in database? -----Original Message----- From: Adrian Klaver [mailto:adrian.klaver@aklaver.com] Sent: Tuesday, February 09, 2016 11:32 AM To: Venkatesan, Sekhar; David G. Johnston Cc: Tom Lane; pgsql-sql@postgresql.org Subject: Re: [SQL] Question on PostgreSQL DB behavior w.r.t JOIN and sort o= rder. On 02/08/2016 09:53 PM, Venkatesan, Sekhar wrote: > Yes. is there an option/configuration to tell the postgres=20 > optimizer/planner to generate plans to include the sort order instead=20 > of speed? What columns in a table would that be and then what order? > > *From:*David G. Johnston [mailto:david.g.johnston@gmail.com] > *Sent:* Tuesday, February 09, 2016 11:20 AM > *To:* Venkatesan, Sekhar > *Cc:* Tom Lane; pgsql-sql@postgresql.org > *Subject:* Re: [SQL] Question on PostgreSQL DB behavior w.r.t JOIN and=20 > sort order. > > On Monday, February 8, 2016, Venkatesan, Sekhar=20 > > wrote: > > Is there a way to tell the optimizer to retain the sort order if that=20 > is possible please? > > You mean, besides the ORDER BY clause? > > David J. > -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes = to your subscription: http://www.postgresql.org/mailpref/pgsql-sql --=20 Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql