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 1hqIZa-0005AU-4e for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 14:48:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hqIZY-0007rn-LK for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 14:48: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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqIZX-0007rU-5x for pgsql-general@lists.postgresql.org; Wed, 24 Jul 2019 14:48:52 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqIZT-0002h3-1O for pgsql-general@postgresql.org; Wed, 24 Jul 2019 14:48:50 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 9962B21C0F; Wed, 24 Jul 2019 10:48:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 24 Jul 2019 10:48:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm3; bh=3 zMl62KpwaTrX1hZckyk+FE2rqu/FjYkReYGaCRN6jo=; b=CBetcD62MYJa+1aXx Dgjm/pFLuZazKZ6tnh4WWThUwA24X88VO85EBywc5aSa6m27uZmqINA1An90GPUM HAf3Vbh059KeCFK9MNRaSsmjjQhUSMDZkxL7sBV8cMLFdLYU5K/3nm1Dlzb7vkOM YUaHvHeWzlIfm+MaprGynykqX+BmjNnGjQ+WTtP+M+NKkFnC29iWwdV/DEbmnSC+ S5X1oTSFOg6oaTs/jaBKCFVGBlMgQL2M0CAcwV/fqyLbe13g2IAJfj2FdhgU1/FN IHlVlJX08TRJuqlnJCm41b5Wcq08IsHraz6f42tJNdQlRd0IwHZmJy9jpEFoutJW Yetow== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=3zMl62KpwaTrX1hZckyk+FE2rqu/FjYkReYGaCRN6 jo=; b=Fb2Yadx75nH12EQeiAjb4SrOVGZIvs12oj9mxvyKcUJ7TUyvM8FAU8fJm XUzMB2cMTlTo3/goxz3fJ9nQHQ2uozkmIlOlxg9/W2GCGs2pOh3r+jcuM6onytX/ JMrjad4tTDpO/l1/n5Z6ZPdfVMPwbp9JaLZsn/UqFXkaQj4IX1ruY9/174hho2ne kwxJ90M7tz9K5yc8OXrvMevUfSBHyDYN+ju2uM+kju4aJ1YEDlaUzJEwPkuVNQRH UReaarGzVSkGA+y5S2bQmtj/MX7CjAkEbr2XRGksQwssn1GgmkdeAnvOWCDrKoK7 /pMO/0lmcWz/waOpMFFquWcyOBKMw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduvddrkedtgdejhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecukfhppeejuddrvdduvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhho mheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvg hrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 6CE8C80059; Wed, 24 Jul 2019 10:48:43 -0400 (EDT) Subject: Re: Default ordering option To: Cyril Champier , Ian Barwick Cc: pgsql-general@postgresql.org References: <1bbdd8f9-454c-1402-964e-0057165338cb@2ndquadrant.com> From: Adrian Klaver Message-ID: <76f267af-80ee-1f37-c774-fe134c228686@aklaver.com> Date: Wed, 24 Jul 2019 07:48:42 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/24/19 1:45 AM, Cyril Champier wrote: > Thanks for your answers. > Unfortunately the update trick only seems to work under certain conditions. > > I do this to shuffle my patients table: > UPDATE "patients" > SET "updated_at" = NOW() > WHERE "patients"."id" = (SELECT "patients"."id" FROM "patients" ORDER BY > random() LIMIT 1) > > Then indeed, this query returns different order: > SELECT * > FROM "patients" > > But this one (because it use an index?) always returns values in the > same order: > SELECT "id" > FROM "patients" Hmm, I don't see that: test=# \d t1 Table "public.t1" Column | Type | Collation | Nullable | Default --------+-------------------+-----------+----------+--------- a | integer | | not null | b | character varying | | | Indexes: "t1_pkey" PRIMARY KEY, btree (a) test=# select * from t1; a | b ---+--------- 2 | cat 3 | fish 1 | dogfish (3 rows) test=# select a from t1; a --- 2 3 1 (3 rows) Are you sure there is nothing going on between the first and second queries e.g. ROLLBACK? > > > > And for the other suggestion, I cannot blindly add 'ORDER BY random()' > to every select, > because of the incompatibility with distinct and union, and the way we > use our orm. > Are you talking about the production or test queries above? -- Adrian Klaver adrian.klaver@aklaver.com