pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Cyril Champier <cyril.champier@doctolib.com>
To: Ian Barwick <ian.barwick@2ndquadrant.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Default ordering option
Date: Wed, 24 Jul 2019 07:48:42 -0700
Message-ID: <76f267af-80ee-1f37-c774-fe134c228686@aklaver.com> (raw)
In-Reply-To: <CAJaA8VcTtLQMXXABzPN6yJdAVsSOkHkmaivQVZ_iGyPoOV48tQ@mail.gmail.com>
References: <CAJaA8VcZCkBZeEMt45g-YR7c6ScHbB3PNURt6Z7AWZuuvX5umA@mail.gmail.com>
	<f464ff11-ce6c-4e94-63c6-46afb79c79c8@aklaver.com>
	<1bbdd8f9-454c-1402-964e-0057165338cb@2ndquadrant.com>
	<CAJaA8VcTtLQMXXABzPN6yJdAVsSOkHkmaivQVZ_iGyPoOV48tQ@mail.gmail.com>

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





view thread (16+ messages)  latest in thread

Message-ID: <76f267af-80ee-1f37-c774-fe134c228686@aklaver.com>
Permalink:  ../76f267af-80ee-1f37-c774-fe134c228686@aklaver.com/
Also on:    postgresql.org/message-id/76f267af-80ee-1f37-c774-fe134c228686@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, cyril.champier@doctolib.com, ian.barwick@2ndquadrant.com
  Subject: Re: Default ordering option
  In-Reply-To: <76f267af-80ee-1f37-c774-fe134c228686@aklaver.com>
  References: <CAJaA8VcZCkBZeEMt45g-YR7c6ScHbB3PNURt6Z7AWZuuvX5umA@mail.gmail.com> <f464ff11-ce6c-4e94-63c6-46afb79c79c8@aklaver.com> <1bbdd8f9-454c-1402-964e-0057165338cb@2ndquadrant.com> <CAJaA8VcTtLQMXXABzPN6yJdAVsSOkHkmaivQVZ_iGyPoOV48tQ@mail.gmail.com> <76f267af-80ee-1f37-c774-fe134c228686@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox