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 1hq6Ul-0002Bc-3i for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 01:55:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hq6Ui-00056f-JA for pgsql-general@arkaria.postgresql.org; Wed, 24 Jul 2019 01:55:04 +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 1hq6Ui-00054z-5t for pgsql-general@lists.postgresql.org; Wed, 24 Jul 2019 01:55:04 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hq6UW-0003gS-IS for pgsql-general@postgresql.org; Wed, 24 Jul 2019 01:55:03 +0000 Received: by mail-pf1-x442.google.com with SMTP id f17so16058621pfn.6 for ; Tue, 23 Jul 2019 18:54:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:cc:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=mpg4kvZ5pv4VcStlblPoVB4QWYrMqkbQfUdcZ/egJGA=; b=rC5cLY+G15X0Y6gi5HwsWlOizMZaBPTZEqde7E+C51QRUNm5NRPdBAPwrRU8IIL1wc +/+kw44LRLm9Vuyxw/+xpWJE9z0KRMqE6vkMEtsqz3aZ7ZRu6yKvskZRf/Mpu1+CGyui IyAmhx3Ph1S6Tu5El+wygjDd2mcVZ7/t8y1u0sTc3UOnzzqe5zpGfNpgukCNoA5bPJuy XmyNnMgQb8JuEiok0dXUBJoaUhA7IKeXXIpJbk/P/IsNmMkHzwvp6JnK3253/+xKx2y6 7UGgBrQOuyHbWWqZVycmPZum+4tCT2o4D02zEB0+a430lNJjiKUJ0Oj7c4EDTJODyTy8 k+vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:cc:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=mpg4kvZ5pv4VcStlblPoVB4QWYrMqkbQfUdcZ/egJGA=; b=t6Om3R75UDT8CRsSoLUiPRiYLHXfKuPEPQDEhn7sgSrFwyCYDNEq/66bEtePfzqYlI NTKEe4f+oY1MwNsrHi9OkuyX+ajrI2CJ8gf0nek5ubArc5+cv1qjbEQ/4SeN5mMReWP6 CBTHSUC2p3WhmODAw8n6GL/xmL1lC4lqFNwCC1EIV9PFK5ZYE2OpX1Zq8c00VWWloAi9 z16FM4vFeDv3PcOiY4Ly+pqtS1aelj5niHv8WKWSZlQQiIdwgodMP5T+vZsENecmuGNM g6TXhxBhqgh9Y448JioHxgQfLyCPxcNRbYJ+KYUOUdg0Q8vqhmQ4M6gSjI17BOcS1Q1r Y5hw== X-Gm-Message-State: APjAAAXv/1Jk+0pPJ25T7OnXdQjt/nv0Ztn/7emSOhEwxiYCNicaCdB0 leN9a8n7Fxn2qqXP4NTO0884AA== X-Google-Smtp-Source: APXvYqzlJYrh52KpEhbGmcZjLhZAZplPRBavp1HqJ7UQy4NB+1JWb+/xmkiNCH859CqLTw7YBWbmVA== X-Received: by 2002:a62:d0:: with SMTP id 199mr8354857pfa.253.1563933288702; Tue, 23 Jul 2019 18:54:48 -0700 (PDT) Received: from osaka.octopusnet (p6423254-ipngn31501marunouchi.tokyo.ocn.ne.jp. [153.170.181.254]) by smtp.gmail.com with ESMTPSA id cx22sm36888671pjb.25.2019.07.23.18.54.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 23 Jul 2019 18:54:47 -0700 (PDT) Subject: Re: Default ordering option To: Cyril Champier , pgsql-general@postgresql.org References: From: Ian Barwick Cc: Adrian Klaver Message-ID: <1bbdd8f9-454c-1402-964e-0057165338cb@2ndquadrant.com> Date: Wed, 24 Jul 2019 10:54:44 +0900 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 7/24/19 2:23 AM, Adrian Klaver wrote: > On 7/23/19 8:43 AM, Cyril Champier wrote: >> Hi, >> >> In this documentation , it is said: >> >>     If sorting is not chosen, the rows will be returned in an >>     unspecified order. The actual order in that case will depend on the >>     scan and join plan types and the order on disk, but it must not be >>     relied on. >> >> >> I would like to know if there is any way to change that to have a "real" random behaviour. >> >> My use case: >> At Doctolib, we do a lot of automatic tests. >> Sometimes, people forgot to order their queries. Most of the time, there is no real problem on production. Let say, we display a user list without order. >> When a developer writes a test for this feature, he will create 2 users A and B, then assert that they are displayed "[A, B]". >> 99% of the time the test will be ok, but sometimes, the displayed list will be "[B,A]", and the test will fail. >> >> One solution could be to ensure random order with an even distribution, so that such failing test would be detected quicker. >> >> Is that possible? Maybe with a plugin? > > Not that I know of. > > A possible solution given below: > > test_(postgres)> insert into t1 values (1, 'dog'), (2, 'cat'), (3, 'fish'); > INSERT 0 3 > > test_(postgres)> select * from t1 ; >  a |  b > ---+------ >  1 | dog >  2 | cat >  3 | fish > (3 rows) > > test_(postgres)> update  t1 set b = 'dogfish' where  a =1; > UPDATE 1 > > test_(postgres)> select * from t1 ; >  a |    b > ---+--------- >  2 | cat >  3 | fish >  1 | dogfish > (3 rows) > > An UPDATE reorders the rows. Maybe throw an UPDATE into the test after creating the users to force an 'out of order' result? An UPDATE without changing any values should have the same effect, e.g. : UPDATE t1 SET b = b WHERE a = 1; Something like this WITH x AS (SELECT * FROM t1 ORDER BY a DESC) UPDATE t1 t SET a = t.a FROM x WHERE t.a = x.a would shuffle the rows into reverse order, which might be enough to catch out any missing ORDER BY (this assumes nothing else will touch the table and reorder it before the test is run). You could also order by RANDOM() but there would be a chance the rows would end up in sequential order. Regards Ian Barwick -- Ian Barwick https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services