Skip to content

Instantly share code, notes, and snippets.

@jorgearimitsu
Created October 24, 2022 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorgearimitsu/373a59d3d95da3d395e5fa7552473ec1 to your computer and use it in GitHub Desktop.
Save jorgearimitsu/373a59d3d95da3d395e5fa7552473ec1 to your computer and use it in GitHub Desktop.
Type map query from rails 6.1.7
SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype, t.typtype, t.typbasetype
FROM pg_type as t
LEFT JOIN pg_range as r ON oid = rngtypid
WHERE
t.typname IN ('int2', 'int4', 'int8', 'oid', 'float4', 'float8', 'text', 'varchar', 'char', 'name', 'bpchar', 'bool', 'bit', 'varbit', 'timestamptz', 'date', 'money', 'bytea', 'point', 'hstore', 'json', 'jsonb', 'cidr', 'inet', 'uuid', 'xml', 'tsvector', 'macaddr', 'citext', 'ltree', 'line', 'lseg', 'box', 'path', 'polygon', 'circle', 'time', 'timestamp', 'numeric', 'interval')
OR t.typtype IN ('r', 'e', 'd')
OR t.typinput = 'array_in(cstring,oid,integer)'::regprocedure
OR t.typelem != 0
@jorgearimitsu
Copy link
Author

Result using EXPLAIN (ANALYZE, BUFFERS)

Hash Left Join  (cost=1.14..44650.38 rows=310104 width=86) (actual time=0.016..355.018 rows=203911 loops=1)
  Hash Cond: (t.oid = r.rngtypid)
  Buffers: shared hit=11089
  ->  Seq Scan on pg_type t  (cost=0.00..43835.21 rows=310104 width=82) (actual time=0.005..319.475 rows=203911 loops=1)
        Filter: ((typname = ANY ('{int2,int4,int8,oid,float4,float8,text,varchar,char,name,bpchar,bool,bit,varbit,timestamptz,date,money,bytea,point,hstore,json,jsonb,cidr,inet,uuid,xml,tsvector,macaddr,citext,ltree,line,lseg,box,path,polygon,circle,time,timestamp,numeric,interval}'::name[])) OR (typtype = ANY ('{r,e,d}'::"char"[])) OR ((typinput)::oid = '750'::oid) OR (typelem <> '0'::oid))
        Rows Removed by Filter: 273344
        Buffers: shared hit=11088
  ->  Hash  (cost=1.06..1.06 rows=6 width=8) (actual time=0.005..0.006 rows=6 loops=1)
        Buckets: 1024  Batches: 1  Memory Usage: 9kB
        Buffers: shared hit=1
        ->  Seq Scan on pg_range r  (cost=0.00..1.06 rows=6 width=8) (actual time=0.002..0.003 rows=6 loops=1)
              Buffers: shared hit=1
Planning:
  Buffers: shared hit=12
Planning Time: 0.347 ms
Execution Time: 363.835 ms

@jorgearimitsu
Copy link
Author

Upgrading to PostgreSQL 14 improved the performance:

Hash Left Join  (cost=1.24..19250.87 rows=306858 width=86) (actual time=0.016..138.272 rows=205837 loops=1)
  Hash Cond: (t.oid = r.rngtypid)
  Buffers: shared hit=9147
  ->  Seq Scan on pg_type t  (cost=0.10..18444.22 rows=306858 width=82) (actual time=0.007..101.006 rows=205837 loops=1)
        Filter: ((typname = ANY ('{int2,int4,int8,oid,float4,float8,text,varchar,char,name,bpchar,bool,bit,varbit,timestamptz,date,money,bytea,point,hstore,json,jsonb,cidr,inet,uuid,xml,tsvector,macaddr,citext,ltree,line,lseg,box,path,polygon,circle,time,timestamp,numeric,interval}'::name[])) OR (typtype = ANY ('{r,e,d}'::"char"[])) OR ((typinput)::oid = '750'::oid) OR (typelem <> '0'::oid))
        Rows Removed by Filter: 184826
        Buffers: shared hit=9146
  ->  Hash  (cost=1.06..1.06 rows=6 width=8) (actual time=0.004..0.006 rows=6 loops=1)
        Buckets: 1024  Batches: 1  Memory Usage: 9kB
        Buffers: shared hit=1
        ->  Seq Scan on pg_range r  (cost=0.00..1.06 rows=6 width=8) (actual time=0.002..0.003 rows=6 loops=1)
              Buffers: shared hit=1
Planning:
  Buffers: shared hit=12
Planning Time: 0.338 ms
Execution Time: 147.277 ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment