Re: RangeType internal use - Mailing list pgsql-hackers

From Amit Langote
Subject Re: RangeType internal use
Date
Msg-id 54D46FF3.6050700@lab.ntt.co.jp
Whole thread Raw
In response to Re: RangeType internal use  (Kyotaro HORIGUCHI <horiguchi.kyotaro@lab.ntt.co.jp>)
List pgsql-hackers
Horiguchi-san,

On 06-02-2015 PM 04:34, Kyotaro HORIGUCHI wrote:
> Hi, from nearby:)
> 

Thank you!

>> I wonder why I cannot find a way to get a range type for a given (sub-)
>> type. I would like to build a RangeType from Datum's of lower and upper
>> bounds. Much like how construct_array() builds an ArrayType from a Datum
>> array of elements given elements' type info.
>>
>> Is there some way I do not seem to know? If not, would it be worthwhile
>> to make something like construct_range() that returns a RangeType given
>> Datum's of lower and upper bounds and subtype info?
> 
> make_range needs the range type itself.
> 
> On SQL interfalce, you can get range type coresponds to a base
> type by looking up the pg_range catalog.
> 
> SELECT rngtypid::regtype, rngsubtype::regtype
>  FROM pg_range WHERE rngsubtype = 'int'::regtype;
> 
>  rngtypid  | rngsubtype 
> -----------+------------
>  int4range | integer
> 
> But there's only one syscache for this catalog which takes range
> type id. So the reverse resolution rngsubtype->rngtype seems not
> available. TypeCahce has only comparison function info as surely
> available element related to range types but this wouldn't
> help. I think scanning the entire cache is not allowable even if
> possible.
> 
> Perhaps what is needed is adding RANGESUBTYPE syscache but I
> don't know whether it is allowable or not.
> 
> Thoughts?

Actually, I'm wondering if there is one-to-one mapping from rangetype to
subtype (and vice versa?), then this should be OK. But if not (that is
designers of range types thought there is not necessarily such a
mapping), then perhaps we could add, say, rngtypeisdefault flag to pg_range.

Perhaps following is not too pretty:

+
+/*
+ * get_rangetype_for_type
+ *
+ * returns a TypeCacheEntry for a range type of a given (sub-) type.
+ */
+TypeCacheEntry *
+get_rangetype_for_type(Oid subtypid)
+{
+    Relation    relation;
+    SysScanDesc scan;
+    HeapTuple    rangeTuple;
+    Oid            rngsubtype;
+    Oid            rngtypid = InvalidOid;
+
+    relation = heap_open(RangeRelationId, AccessShareLock);
+
+    scan = systable_beginscan(relation, InvalidOid, false,
+                              NULL, 0, NULL);
+
+    while ((rangeTuple = systable_getnext(scan)) != NULL)
+    {
+        rngsubtype = ((Form_pg_range) GETSTRUCT(rangeTuple))->rngsubtype;
+
+        if (rngsubtype == subtypid)
+        {
+            rngtypid = ((Form_pg_range) GETSTRUCT(rangeTuple))->rngtypid;
+            break;
+        }
+    }
+
+    systable_endscan(scan);
+    heap_close(relation, AccessShareLock);
+
+    return(rngtypid != InvalidOid
+                ? lookup_type_cache(rngtypid, TYPECACHE_RANGE_INFO): NULL);
+}

Thanks,
Amit




pgsql-hackers by date:

Previous
From: Kyotaro HORIGUCHI
Date:
Subject: Re: RangeType internal use
Next
From: Etsuro Fujita
Date:
Subject: Re: ExplainModifyTarget doesn't work as expected