Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEaFt-0007lz-Vq for pgsql-general@arkaria.postgresql.org; Tue, 23 Feb 2021 16:09:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lEaFs-00034E-PW for pgsql-general@arkaria.postgresql.org; Tue, 23 Feb 2021 16:09:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEaFs-000347-Im for pgsql-general@lists.postgresql.org; Tue, 23 Feb 2021 16:09:44 +0000 Received: from madoka.blicky.net ([51.15.19.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lEaFp-0006Ma-NR for pgsql-general@lists.postgresql.org; Tue, 23 Feb 2021 16:09:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=blicky.net; s=62x80x37apav; h=In-Reply-To:Content-Type:MIME-Version:References: Message-ID:Subject:To:From:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=WNCGQqhDVGXEU6TAwqnJmD1c/e0a0wKT58i57lE4Pow=; b=U4wH0ly7eTuGrtUZF0H3/OOtc6 2Ce8YJDQCrxdBogtzlkwNijbHQgQEj0tg/MykGFI/c26P7dKPDDxgnx0CcYjhu7NXr/mA0mq7JFQu PxUq9VRynq5vpVgOwHxOg6RO1q7NCBkCEzICsSck2OfdO06LLWW0rkJAbvAGpbkFo/sg=; Received: by madoka.blicky.net; Tue, 23 Feb 2021 17:09:39 +0100 Received: by gmai021; Tue, 23 Feb 2021 17:09:39 +0100 Date: Tue, 23 Feb 2021 17:09:39 +0100 From: Ayo To: pgsql-general@lists.postgresql.org Subject: Re: Custom base type and suboptimal query plans Message-ID: References: <3822526.1614093860@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3822526.1614093860@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk (forgot to cc the list, sorry for the duplicate, Tom!) On 2021-02-23, Tom Lane wrote: > Ayo writes: > > I have a database where I converted an integer primary key column to a > > custom base type that pretty much amounts to a wrapper around an > > integer, and now some queries are resulting in much slower query plans. > > Does Postgres have special optimizations for integers that are not > > available for custom types, or did I perhaps overlook something? > > The slow query isn't using the chars_pkey1 index, which makes one > wonder if you have a corresponding index in the custom-type case, The index exists and is usable in the custom-type case: => explain (analyze,buffers) SELECT count(*) FROM chars WHERE id BETWEEN 'c1000' AND 'c2000'; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=33.87..33.88 rows=1 width=8) (actual time=0.252..0.253 rows=1 loops=1) Buffers: shared hit=9 -> Index Only Scan using chars_pkey1 on chars (cost=0.29..31.47 rows=959 width=0) (actual time=0.042..0.179 rows=1001 loops=1) Index Cond: ((id >= 'c1000'::vndbid) AND (id <= 'c2000'::vndbid)) Heap Fetches: 0 Buffers: shared hit=9 Planning: Buffers: shared hit=97 Planning Time: 0.383 ms Execution Time: 0.308 ms (10 rows) > or if you fat-fingered something about the index operator class > for the custom type. As of v13 I don't think there's anything in > that area that custom types can't replicate ... but there certainly > is plenty of infrastructure for the standard types that you'll need > to build out if you want equivalent functionality. Good to know that this ought to be possible, at least. Is there documentation about what infrastructure exists and how it interacts with the planner? I've built upon https://www.postgresql.org/docs/13/xindex.html and implemented everything that seemed relevant for the type. No doubt I've missed something, but I can't really tell what that may be.