pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Dominique Devienne <ddevienne@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Seq Scan because of stats or because of cast?
Date: Mon, 13 Mar 2023 09:53:42 -0400
Message-ID: <1753797.1678715622@sss.pgh.pa.us> (raw)
In-Reply-To: <CAFCRh-_Ptmhdj3Ly-bdZLqxzG6UijtE=9zitcHhNMGB0A5V93Q@mail.gmail.com>
References: <CAFCRh-_Ptmhdj3Ly-bdZLqxzG6UijtE=9zitcHhNMGB0A5V93Q@mail.gmail.com>

Dominique Devienne <ddevienne@gmail.com> writes:
> I figured that query would use the "pg_auth_members_role_member_index"
> index,
> but instead it's using a sequential scan.
> And I'm wondering is this is because the cardinality of that catalog is
> small (172),
> which is just an artifact of my dev-testing, or whether that's because I
> cast roleid
> to an int4, preventing the use of the index?

Both.

> Is there a way to know why the index is not used, in any of my attempts?

For testing purposes, you could set enable_seqscan = off and then see
whether the plan changes.  When I try this example I get

regression=# explain select * from pg_auth_members WHERE roleid = ANY(array[1::oid,2::oid,3::oid]);
                           QUERY PLAN                           
----------------------------------------------------------------
 Seq Scan on pg_auth_members  (cost=0.00..1.04 rows=3 width=19)
   Filter: (roleid = ANY ('{1,2,3}'::oid[]))
(2 rows)

regression=# set enable_seqscan to 0;
SET
regression=# explain select * from pg_auth_members WHERE roleid = ANY(array[1::oid,2::oid,3::oid]);
                                                QUERY PLAN                                                 
-----------------------------------------------------------------------------------------------------------
 Index Scan using pg_auth_members_role_member_index on pg_auth_members  (cost=0.13..12.44 rows=3 width=19)
   Index Cond: (roleid = ANY ('{1,2,3}'::oid[]))
(2 rows)

So it's clearly not going to use the index until pg_auth_members gets
a great deal larger than it is on my test installation --- but it's
capable of doing so once it looks cost-attractive.

> PS: libpq uses unsigned int for Oid. So do OIDs go above 2GB in practice?

Yes, eventually.

>     I'm asking, since I'm casting to ::int4, thus if they do, then that
> case might overflow.

I'd use int8.

> PPS: Are OIDs recycled / reused? Or are they monotonically increasing?
>   What happens when the Cluster runs out of OIDs?

The counter wraps around.  But it does so at 2^32 not 2^31.

>   Are they Cluster-wide unique or it depends on the OID type?

They're unique per catalog.  We don't attempt to guarantee
more than that.  In practice, they'll be unique across the
installation until after the first OID wraparound, and then
not so much.

			regards, tom lane





view thread (4+ messages)  latest in thread

Message-ID: <1753797.1678715622@sss.pgh.pa.us>
Permalink:  ../1753797.1678715622@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1753797.1678715622@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, ddevienne@gmail.com
  Subject: Re: Seq Scan because of stats or because of cast?
  In-Reply-To: <1753797.1678715622@sss.pgh.pa.us>
  References: <CAFCRh-_Ptmhdj3Ly-bdZLqxzG6UijtE=9zitcHhNMGB0A5V93Q@mail.gmail.com> <1753797.1678715622@sss.pgh.pa.us>

* 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