Waiting for PostgreSQL 14 – Allow subscripting of hstore values.

On 11st of December 2020, Tom Lane committed patch:

Allow subscripting of hstore values.
 
This is basically a finger exercise to prove that it's possible for
an extension module to add subscripting ability.  Subscripted fetch
from an hstore is not different from the existing "hstore -> text"
operator.  Subscripted update does seem to be a little easier to
use than the traditional update method using hstore concatenation,
but it's not a fundamentally new ability.
 
However, there may be some value in the code as sample code, since
it shows what's basically the minimum-complexity way to implement
subscripting when one needn't consider nested container objects.
 
Discussion: https://postgr.es/m/3724341.1607551174@sss.pgh.pa.us

This is really cool. While this patch adds actual functionality, the backbone of this commit was actually this commit from two days earlier.

The general idea is that now, we will be able to provide subscripting ( value[subscript] ) for any datatype, not only arrays.

Hstore patch added support of subscripting to hstore. Which means that I can now:

$ select payload['a'], payload from z where id = 1 \gx
─[ RECORD 1 ]─────────────────────────────────────────────────────────────────────────────────────────────────────────
payload │ 47
payload │ "a"=>"47", "b"=>"35", "d"=>"97", "f"=>"42", "i"=>"75", "m"=>"35", "p"=>"41", "q"=>"86", "r"=>"56", "t"=>"60"

Of course, I could previously use custom operator:

$ select payload->'a', payload from z where id = 1 \gx
─[ RECORD 1 ]──────────────────────────────────────────────────────────────────────────────────────────────────────────
?column? │ 47
payload  │ "a"=>"47", "b"=>"35", "d"=>"97", "f"=>"42", "i"=>"75", "m"=>"35", "p"=>"41", "q"=>"86", "r"=>"56", "t"=>"60"

which, of course, it still available, but now we can also use [ subscript ].

One thing I need to caution you about is that while you can use it for selecting, using it for “where" clause is not indexable (yet?):

$ explain analyze select * from z where payload['a'] = '47';
                                                     QUERY PLAN
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 Gather  (cost=1000.00..5778.67 rows=1000 width=117) (actual time=1.538..15.274 rows=734 loops=1)
   Workers Planned: 2
   Workers Launched: 2
   ->  Parallel Seq Scan on z  (cost=0.00..4678.67 rows=417 width=117) (actual time=0.025..10.223 rows=245 loops=3)
         Filter: (payload['a'::text] = '47'::text)
         Rows Removed by Filter: 66422
 Planning Time: 0.118 ms
 Execution Time: 15.326 ms
(8 rows)

While you can use index when searching using @> operator:

$ explain analyze select * from z where payload @> 'a=>47'::hstore;
                                                    QUERY PLAN
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 Bitmap Heap Scan on z  (cost=56.16..131.96 rows=20 width=117) (actual time=5.975..9.452 rows=734 loops=1)
   Recheck Cond: (payload @> '"a"=>"47"'::hstore)
   Rows Removed by Index Recheck: 6586
   Heap Blocks: exact=3139
   ->  Bitmap Index Scan on hidx  (cost=0.00..56.15 rows=20 width=0) (actual time=5.719..5.719 rows=7320 loops=1)
         Index Cond: (payload @> '"a"=>"47"'::hstore)
 Planning Time: 0.200 ms
 Execution Time: 9.517 ms
(8 rows)

All in all, I find it great feature, and am expecting more datatypes to support this feature, starting, probably, with json.

Thanks to all involved.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.