summaryrefslogtreecommitdiffstats
path: root/contrib/hstore
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/hstore')
-rw-r--r--contrib/hstore/.gitignore4
-rw-r--r--contrib/hstore/Makefile36
-rw-r--r--contrib/hstore/data/hstore.data1001
-rw-r--r--contrib/hstore/expected/hstore.out1601
-rw-r--r--contrib/hstore/hstore--1.1--1.2.sql53
-rw-r--r--contrib/hstore/hstore--1.2--1.3.sql17
-rw-r--r--contrib/hstore/hstore--1.3--1.4.sql99
-rw-r--r--contrib/hstore/hstore--1.4--1.5.sql14
-rw-r--r--contrib/hstore/hstore--1.4.sql550
-rw-r--r--contrib/hstore/hstore--1.5--1.6.sql12
-rw-r--r--contrib/hstore/hstore--1.6--1.7.sql27
-rw-r--r--contrib/hstore/hstore--1.7--1.8.sql17
-rw-r--r--contrib/hstore/hstore.control6
-rw-r--r--contrib/hstore/hstore.h205
-rw-r--r--contrib/hstore/hstore_compat.c359
-rw-r--r--contrib/hstore/hstore_gin.c212
-rw-r--r--contrib/hstore/hstore_gist.c621
-rw-r--r--contrib/hstore/hstore_io.c1499
-rw-r--r--contrib/hstore/hstore_op.c1278
-rw-r--r--contrib/hstore/hstore_subs.c297
-rw-r--r--contrib/hstore/sql/hstore.sql386
21 files changed, 8294 insertions, 0 deletions
diff --git a/contrib/hstore/.gitignore b/contrib/hstore/.gitignore
new file mode 100644
index 0000000..5dcb3ff
--- /dev/null
+++ b/contrib/hstore/.gitignore
@@ -0,0 +1,4 @@
+# Generated subdirectories
+/log/
+/results/
+/tmp_check/
diff --git a/contrib/hstore/Makefile b/contrib/hstore/Makefile
new file mode 100644
index 0000000..c4e339b
--- /dev/null
+++ b/contrib/hstore/Makefile
@@ -0,0 +1,36 @@
+# contrib/hstore/Makefile
+
+MODULE_big = hstore
+OBJS = \
+ $(WIN32RES) \
+ hstore_compat.o \
+ hstore_gin.o \
+ hstore_gist.o \
+ hstore_io.o \
+ hstore_op.o \
+ hstore_subs.o
+
+EXTENSION = hstore
+DATA = hstore--1.4.sql \
+ hstore--1.7--1.8.sql \
+ hstore--1.6--1.7.sql \
+ hstore--1.5--1.6.sql \
+ hstore--1.4--1.5.sql \
+ hstore--1.3--1.4.sql hstore--1.2--1.3.sql \
+ hstore--1.1--1.2.sql
+PGFILEDESC = "hstore - key/value pair data type"
+
+HEADERS = hstore.h
+
+REGRESS = hstore
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/hstore
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/hstore/data/hstore.data b/contrib/hstore/data/hstore.data
new file mode 100644
index 0000000..b7391da
--- /dev/null
+++ b/contrib/hstore/data/hstore.data
@@ -0,0 +1,1001 @@
+line=>1, date=>CB, node=>AA
+cleaned=>f, status=>59, line=>2, disabled=>f, node=>CBB
+indexed=>t, status=>35, line=>3, disabled=>f, wait=>CAA, subtitle=>BA, user=>CCA
+line=>4, disabled=>t, space=>BB
+cleaned=>f, line=>5, wait=>BB, query=>CAC, coauthors=>ACA, node=>CBA
+world=>CB, query=>CBC, indexed=>f, line=>6, pos=>92, date=>AAB, space=>CB, coauthors=>ACA, node=>CBC
+state=>98, org=>43, line=>7, pos=>97
+auth=>BB, title=>CAC, query=>BA, status=>94, line=>8, coauthors=>BBB
+auth=>BAC, title=>CAA, wait=>CA, bad=>t, query=>AA, indexed=>t, line=>9, pos=>56
+title=>AAC, bad=>t, user=>AAB, query=>AC, line=>10, node=>AB
+world=>CAC, user=>AB, query=>ACA, indexed=>t, line=>11, space=>CB
+line=>12, pos=>72, abstract=>BBA, space=>AAC
+
+world=>CC, query=>AA, line=>14, disabled=>f, date=>CAC, coauthors=>AB
+org=>68, title=>BBB, query=>BAC, line=>15, public=>f
+org=>73, user=>AA, indexed=>t, line=>16, date=>CCC, public=>t, coauthors=>AB
+indexed=>f, line=>17
+state=>23, auth=>BCC, org=>38, status=>28, line=>18, disabled=>f, abstract=>CB
+state=>99, auth=>CA, indexed=>t, line=>19, date=>BA
+wait=>CBA, user=>BBA, indexed=>t, line=>20, disabled=>f, abstract=>BA, date=>ABA
+org=>10, query=>AC, indexed=>f, line=>21, disabled=>t, abstract=>CA, pos=>44
+state=>65, title=>AC, user=>AAC, cleaned=>t, status=>93, line=>22, abstract=>ABC, node=>CCC
+subtitle=>AC, user=>CCC, line=>23
+state=>67, world=>ACB, bad=>t, user=>CB, line=>24, disabled=>t
+
+state=>65, title=>CBC, wait=>AAC, bad=>t, query=>ACA, line=>26, disabled=>f, space=>CA
+auth=>BAA, state=>68, indexed=>t, line=>27, space=>BA
+indexed=>f, line=>28, disabled=>t, space=>CC, node=>BB
+auth=>BAB, org=>80, title=>BBA, query=>BBC, status=>3, line=>29
+title=>AC, status=>16, cleaned=>t, line=>30
+state=>39, world=>AAB, user=>BB, line=>31, disabled=>t
+wait=>BC, bad=>f, query=>AA, line=>32, coauthors=>CAC
+line=>33, pos=>97
+title=>AA, world=>CCA, wait=>CC, bad=>t, status=>86, line=>34, disabled=>t, node=>ACA
+
+world=>BCC, title=>ACB, org=>61, status=>99, cleaned=>t, line=>36, pos=>76, space=>ACC, coauthors=>AA, node=>CB
+title=>CAA, cleaned=>f, line=>37, abstract=>ACA, node=>BC
+auth=>BC, title=>BA, world=>ACA, indexed=>t, line=>38, abstract=>AAA, public=>t
+org=>90, line=>39, public=>f
+state=>16, indexed=>t, line=>40, pos=>53
+auth=>AAB, wait=>CAC, status=>44, line=>41
+subtitle=>ACA, bad=>t, line=>42
+org=>19, world=>BC, user=>ABA, indexed=>f, line=>43, disabled=>t, pos=>48, abstract=>CAB, space=>CCB
+indexed=>f, line=>44
+indexed=>t, line=>45
+status=>84, line=>46, date=>CCC
+state=>94, title=>BAB, bad=>t, user=>BBB, indexed=>t, line=>47, public=>f
+org=>90, subtitle=>BAC, query=>CAC, cleaned=>f, line=>48, disabled=>t, abstract=>CC, pos=>17, space=>BCA
+world=>CBC, line=>49
+org=>24, line=>50, date=>CA, public=>f
+world=>BC, indexed=>t, status=>44, line=>51, pos=>59, date=>BA, public=>t
+org=>98, line=>52
+title=>CA, world=>ABC, subtitle=>CBB, line=>53, abstract=>BBA, date=>ACB, node=>CA
+user=>BAB, cleaned=>t, line=>54
+subtitle=>CAA, line=>55, disabled=>f, pos=>55, abstract=>AB, public=>f, coauthors=>AA
+wait=>CC, user=>CC, cleaned=>t, line=>56, pos=>73, node=>ABC
+title=>BCC, wait=>ABC, indexed=>t, line=>57, disabled=>f
+org=>42, title=>BB, line=>58, disabled=>t, public=>t, coauthors=>BCC
+wait=>CAB, title=>CCB, query=>BAC, status=>66, line=>59, disabled=>t
+user=>CAC, line=>60
+user=>BBB, line=>61, disabled=>f, pos=>31
+org=>18, line=>62, coauthors=>CCC, node=>CA
+line=>63, coauthors=>AB
+org=>25, wait=>CA, line=>64, abstract=>BA, date=>BBB
+title=>CB, wait=>CC, bad=>f, user=>BBB, line=>65, abstract=>ACA, public=>t
+line=>66, coauthors=>AC
+state=>20, wait=>CCB, bad=>t, line=>67, abstract=>CB
+state=>79, wait=>BAC, bad=>f, status=>11, line=>68, abstract=>BC, public=>t, coauthors=>CBA
+state=>39, title=>CCA, bad=>f, query=>BBA, line=>69, pos=>42, public=>f
+title=>BC, subtitle=>CA, query=>BC, line=>70
+
+bad=>t, query=>BBB, line=>72
+state=>35, world=>CC, bad=>f, line=>73, space=>BB, public=>f
+title=>ACC, wait=>CAB, subtitle=>CB, status=>19, line=>74, disabled=>f, space=>BAA, coauthors=>CBC, node=>AC
+subtitle=>BCB, indexed=>f, status=>83, line=>75, public=>t
+state=>32, line=>76, disabled=>f, pos=>66, space=>CC
+state=>43, cleaned=>t, line=>77
+
+state=>97, wait=>CBA, indexed=>f, cleaned=>f, line=>79, abstract=>CB, date=>ACC, public=>f
+user=>AAB, line=>80, pos=>85, date=>AC
+world=>AC, wait=>CC, subtitle=>AAB, bad=>f, cleaned=>f, line=>81, pos=>91, node=>CCC
+
+org=>87, bad=>f, user=>AAC, query=>CCC, line=>83, disabled=>f, abstract=>AC, date=>CCA, public=>f
+state=>50, line=>84
+wait=>AA, subtitle=>AA, query=>BB, status=>97, line=>85, disabled=>t, abstract=>CB
+
+subtitle=>CA, query=>BC, line=>87
+
+title=>CC, line=>89, disabled=>f, pos=>49, date=>CCB, space=>CB, node=>BB
+auth=>CC, wait=>AA, title=>BC, bad=>t, line=>90
+state=>37, org=>85, indexed=>f, line=>91, space=>CAA, public=>t, coauthors=>BA
+wait=>BBB, title=>BBC, org=>95, subtitle=>AC, line=>92, pos=>23, date=>AC, public=>t, space=>BBC
+org=>48, user=>AC, line=>93, space=>CCC
+
+state=>77, wait=>ABA, subtitle=>AC, user=>BA, status=>43, line=>95, public=>f
+title=>CA, indexed=>t, status=>26, line=>96
+auth=>BCA, subtitle=>ACC, user=>CA, line=>97, disabled=>f, node=>ACB
+query=>BB, line=>98, coauthors=>AB
+
+auth=>AA, title=>ACB, org=>58, subtitle=>AC, bad=>f, cleaned=>f, line=>100, space=>ACC, public=>t
+subtitle=>AAB, bad=>f, line=>101, public=>t
+subtitle=>AAA, indexed=>f, cleaned=>f, line=>102, disabled=>t, pos=>35
+
+world=>CAC, org=>10, query=>AAA, cleaned=>t, status=>79, indexed=>t, line=>104, pos=>65, public=>f, node=>BAB
+bad=>f, line=>105, abstract=>BA, node=>CBB
+world=>BB, wait=>BAA, title=>ACA, line=>106, date=>CBC, space=>BA
+state=>92, wait=>CAC, title=>AAA, bad=>f, line=>107, abstract=>CBC, date=>BCC, public=>f
+title=>CCC, indexed=>t, line=>108, abstract=>ACB, public=>f, coauthors=>ABB
+auth=>BB, query=>ACC, status=>68, line=>109
+user=>CC, cleaned=>f, indexed=>t, line=>110, date=>BAA, space=>BCB
+auth=>CC, org=>4, wait=>BAC, bad=>f, indexed=>f, line=>111, pos=>55, node=>BBC
+line=>112, disabled=>t
+org=>66, cleaned=>t, indexed=>f, line=>113, pos=>96
+world=>CA, title=>ACA, org=>83, query=>BAC, user=>BBC, indexed=>f, line=>114
+subtitle=>BCC, line=>115, space=>AA, public=>t, node=>CBA
+state=>77, status=>23, line=>116
+bad=>f, status=>4, line=>117, node=>CC
+state=>99, title=>BCC, query=>AC, status=>98, line=>118, date=>BA
+status=>55, line=>119, public=>f, coauthors=>BBA, node=>BCA
+query=>CAA, status=>40, indexed=>f, line=>120, disabled=>f, coauthors=>CA
+title=>BBC, org=>82, subtitle=>ACB, line=>121, abstract=>BB, node=>CC
+state=>66, world=>AB, subtitle=>BA, query=>CB, line=>122, abstract=>BBC, pos=>65, date=>BAB
+state=>96, title=>CBC, status=>44, line=>123, abstract=>BA, space=>ACA, node=>AAC
+auth=>CA, state=>59, bad=>f, cleaned=>f, line=>124, pos=>41, date=>BBA, coauthors=>ABB
+wait=>ACC, line=>125
+org=>30, wait=>CBB, subtitle=>CCA, cleaned=>t, line=>126, date=>AC, node=>ABC
+
+auth=>BBA, org=>66, subtitle=>CCB, bad=>t, cleaned=>f, line=>128, abstract=>BB, public=>t, coauthors=>BA
+subtitle=>AC, bad=>f, user=>BAA, line=>129, date=>BCB, node=>BAC
+wait=>CC, subtitle=>CA, line=>130, disabled=>f, pos=>49, node=>BA
+indexed=>f, line=>131, pos=>79, date=>AAA, node=>CAC
+wait=>AC, world=>CB, title=>AAA, user=>ABC, indexed=>f, status=>15, line=>132, coauthors=>BA
+state=>96, bad=>t, line=>133, disabled=>f, space=>BAC, coauthors=>ABA
+world=>BAC, line=>134
+title=>CCC, line=>135, coauthors=>CC
+cleaned=>t, line=>136
+bad=>t, query=>CCA, user=>CA, cleaned=>f, line=>137, disabled=>t
+
+world=>CC, subtitle=>BBB, line=>139
+wait=>CA, status=>2, line=>140
+world=>BC, bad=>f, user=>BBC, query=>ACB, line=>141, pos=>33, space=>ACA
+state=>92, title=>CA, bad=>t, query=>AB, line=>142, abstract=>BA, date=>ABB, space=>BC, coauthors=>CAA
+state=>79, query=>AB, user=>CCA, indexed=>t, cleaned=>t, line=>143, public=>t
+org=>37, query=>CA, cleaned=>t, line=>144, disabled=>t, date=>CC
+wait=>AC, title=>CBA, user=>AAA, status=>24, line=>145, date=>CBC, public=>f, coauthors=>BAC, node=>ACC
+user=>CA, indexed=>t, line=>146, disabled=>f, coauthors=>BA
+wait=>BC, org=>35, bad=>f, query=>CBB, line=>147, date=>AAA, public=>f, space=>BBB
+org=>56, user=>AB, indexed=>t, line=>148
+
+title=>CBB, org=>78, subtitle=>CBA, bad=>t, user=>AAB, line=>150, disabled=>t, abstract=>BAC
+world=>CCA, query=>BC, cleaned=>t, indexed=>f, line=>151, abstract=>BC, pos=>43, coauthors=>AB, node=>CBA
+auth=>ABA, status=>13, line=>152, date=>AA
+world=>CA, line=>153, space=>CBC
+world=>BA, user=>BBB, status=>72, line=>154
+auth=>ABB, line=>155, disabled=>t, node=>BBC
+world=>BBB, bad=>f, line=>156, abstract=>CBC
+line=>157, pos=>60, node=>ACC
+line=>158, node=>CC
+line=>159, public=>t
+
+query=>BA, status=>53, cleaned=>f, line=>161, public=>t
+line=>162, date=>CC
+
+title=>BC, bad=>f, query=>CC, line=>164, abstract=>CCB, date=>BA
+status=>36, line=>165
+title=>AB, bad=>f, status=>64, line=>166, abstract=>AB, coauthors=>AA, node=>AA
+wait=>AA, line=>167
+subtitle=>CBC, user=>AC, cleaned=>f, line=>168, disabled=>t, coauthors=>BAB, node=>CC
+state=>34, status=>73, cleaned=>t, line=>169, abstract=>BC, public=>f, space=>BBC, node=>BAA
+state=>10, auth=>BBB, bad=>t, indexed=>f, status=>34, line=>170, abstract=>BC
+subtitle=>AAA, bad=>f, user=>ACA, status=>53, line=>171, disabled=>f, date=>AAA
+subtitle=>CB, query=>CC, indexed=>t, line=>172, node=>BBC
+state=>5, world=>ABC, bad=>f, line=>173, public=>f
+subtitle=>AC, line=>174
+auth=>AC, org=>72, query=>CA, indexed=>f, cleaned=>t, line=>175, disabled=>t, pos=>54
+title=>BCB, bad=>f, line=>176, pos=>35, coauthors=>AAC, node=>ABB
+title=>BB, cleaned=>t, status=>26, line=>177
+state=>61, wait=>BB, world=>CB, query=>BAB, line=>178, abstract=>BB, date=>CBB, space=>CA, node=>AB
+wait=>CA, cleaned=>f, indexed=>t, line=>179, space=>CBC
+org=>68, line=>180
+wait=>ABB, subtitle=>CCC, cleaned=>t, line=>181, abstract=>BC, coauthors=>BA
+title=>ACA, subtitle=>AAB, line=>182, node=>BAC
+
+
+subtitle=>BA, query=>BBB, indexed=>t, cleaned=>t, line=>185, node=>BCC
+org=>6, title=>BCC, user=>BA, line=>186, pos=>67, abstract=>CBA, coauthors=>CBB, node=>CBC
+org=>50, title=>CAB, subtitle=>CB, query=>CBB, line=>187, coauthors=>CA, node=>CC
+bad=>f, line=>188, node=>CCB
+org=>4, world=>AAC, query=>CAC, line=>189, pos=>90, node=>AC
+state=>86, line=>190, pos=>79
+org=>98, title=>AAC, cleaned=>t, line=>191, space=>BC, coauthors=>AA
+wait=>CAA, bad=>f, user=>BC, status=>23, line=>192, disabled=>t, date=>CA, coauthors=>BBB
+status=>26, line=>193, disabled=>t
+world=>CA, subtitle=>CCC, query=>ABB, status=>86, line=>194, pos=>97, space=>CAC
+cleaned=>t, line=>195
+state=>53, org=>84, wait=>BC, query=>BCC, line=>196, disabled=>t, abstract=>AAC, node=>CAC
+state=>25, status=>70, cleaned=>f, line=>197, disabled=>t, space=>AA, public=>f
+org=>82, subtitle=>AAC, line=>198
+org=>87, bad=>t, status=>69, line=>199, public=>f
+wait=>CC, org=>60, subtitle=>BCA, bad=>t, cleaned=>f, indexed=>t, line=>200, date=>BA
+state=>9, world=>CAA, org=>78, user=>ACB, cleaned=>t, line=>201, disabled=>t, abstract=>ACC, public=>f
+state=>50, world=>AAA, title=>CAA, user=>AB, status=>37, line=>202, disabled=>f
+org=>36, subtitle=>CB, query=>BAA, status=>35, line=>203, abstract=>CC
+auth=>CCC, bad=>t, query=>CB, status=>84, line=>204, disabled=>f, date=>BB
+auth=>AC, query=>BA, indexed=>f, line=>205, date=>AAB, space=>ABB
+state=>30, world=>CCA, query=>CC, user=>BAA, line=>206
+title=>CAB, wait=>BAB, bad=>t, query=>BCB, indexed=>t, status=>48, cleaned=>t, line=>207, node=>ACB
+state=>97, subtitle=>BC, status=>99, line=>208, abstract=>CB
+title=>CA, world=>BBA, bad=>t, indexed=>f, cleaned=>f, status=>82, line=>209, disabled=>f, pos=>44, space=>ACA
+line=>210, public=>t
+line=>211, space=>BBC, node=>AAA
+wait=>BAA, org=>50, line=>212, abstract=>BB, public=>t, space=>AB
+line=>213, pos=>57, date=>CC, space=>AC
+state=>23, user=>BAB, query=>BCB, line=>214, abstract=>BAB
+world=>ACB, org=>21, line=>215, abstract=>AC, public=>f
+state=>14, wait=>ACB, org=>79, title=>BB, subtitle=>BA, line=>216
+wait=>BC, line=>217, date=>BB
+wait=>AC, user=>BB, indexed=>f, status=>83, line=>218
+auth=>BC, org=>9, user=>BA, status=>31, line=>219, disabled=>f
+state=>80, world=>BA, wait=>CA, line=>220, pos=>65, node=>CAC
+wait=>AC, subtitle=>ABB, status=>79, indexed=>t, line=>221, abstract=>AC, pos=>33, space=>BA
+state=>69, org=>83, world=>CBC, subtitle=>CAC, cleaned=>f, line=>222, space=>BC, node=>CCA
+line=>223, abstract=>BC
+
+world=>BB, title=>BC, bad=>f, query=>BBC, cleaned=>f, line=>225, disabled=>f, public=>t
+line=>226, date=>AC
+auth=>CB, subtitle=>AB, indexed=>f, status=>2, line=>227, pos=>53, space=>AB, coauthors=>BCA
+title=>ABA, org=>36, line=>228, space=>AA
+world=>AB, line=>229, pos=>78, date=>BC, space=>CC
+wait=>BBC, org=>47, cleaned=>t, status=>5, line=>230, pos=>2, date=>CCA
+line=>231, coauthors=>CB
+state=>1, user=>CAA, cleaned=>f, line=>232, date=>BA, public=>t, coauthors=>AAA, node=>BCC
+auth=>AB, world=>CAC, query=>BC, cleaned=>t, line=>233, pos=>47, space=>AB, node=>AB
+title=>CAA, line=>234, pos=>9, public=>t, node=>AB
+auth=>CCA, title=>AA, org=>6, subtitle=>CA, cleaned=>t, status=>12, indexed=>f, line=>235, space=>ABB
+auth=>CA, bad=>f, query=>BC, status=>61, cleaned=>f, line=>236, disabled=>t, public=>t
+user=>BCB, line=>237, pos=>70, node=>CBA
+query=>CCB, line=>238, disabled=>t, coauthors=>BAB, node=>BC
+auth=>AC, org=>73, title=>CA, bad=>f, status=>94, line=>239, abstract=>CC
+subtitle=>BC, indexed=>f, line=>240, disabled=>t
+auth=>AAC, org=>73, title=>CB, bad=>t, query=>CA, cleaned=>t, line=>241, disabled=>f, public=>f
+line=>242, public=>f
+auth=>AC, title=>BC, status=>61, line=>243, disabled=>f
+auth=>ABB, bad=>f, indexed=>f, line=>244, abstract=>BAB, date=>ABC, coauthors=>BC
+query=>BA, line=>245, disabled=>f, space=>BAB
+world=>BCC, bad=>f, indexed=>f, line=>246, disabled=>t, pos=>80, public=>f, coauthors=>BC
+indexed=>t, line=>247
+wait=>CCA, subtitle=>CBB, bad=>f, line=>248, pos=>83, public=>f, space=>BA
+
+auth=>ABA, org=>13, title=>BA, bad=>f, indexed=>t, line=>250, disabled=>f, abstract=>BBA, date=>AB
+state=>37, title=>AAA, bad=>f, line=>251, disabled=>f, coauthors=>CBC
+auth=>ACB, world=>AC, title=>CAA, subtitle=>BCA, bad=>f, status=>32, line=>252, pos=>84
+query=>BA, indexed=>f, status=>0, line=>253, abstract=>CCB, pos=>48, date=>AC, space=>AAC
+subtitle=>BBA, line=>254, node=>AAA
+query=>AC, user=>CAA, status=>13, line=>255, public=>t, coauthors=>BCC
+auth=>AAA, state=>31, line=>256
+
+
+wait=>AC, query=>AAA, cleaned=>t, indexed=>f, line=>259, pos=>89, coauthors=>BCA, node=>BC
+world=>CC, query=>BB, line=>260
+
+org=>99, bad=>f, user=>ABA, line=>262, abstract=>BA, coauthors=>BCC
+auth=>CAC, world=>CBC, subtitle=>CA, bad=>f, status=>22, line=>263, pos=>4, public=>t, node=>BB
+wait=>BB, subtitle=>BCC, indexed=>t, line=>264, node=>CAC
+subtitle=>BB, query=>CBB, line=>265
+state=>35, query=>AA, line=>266, coauthors=>AAA
+status=>6, line=>267, pos=>66
+auth=>BAA, subtitle=>CCA, bad=>f, query=>CCB, line=>268, public=>t, space=>CAB, node=>CAC
+world=>AC, org=>58, user=>AC, line=>269, node=>AB
+auth=>BCB, org=>36, title=>AB, line=>270, abstract=>CAB, date=>CAB, public=>t, coauthors=>CB, node=>AB
+cleaned=>t, line=>271
+world=>ACC, cleaned=>t, status=>11, line=>272, disabled=>f, abstract=>AA, space=>BCA, node=>BA
+cleaned=>t, line=>273, pos=>50, public=>t
+status=>95, line=>274, abstract=>BB, coauthors=>AC
+auth=>BCC, state=>80, cleaned=>t, line=>275, abstract=>AC
+wait=>BA, line=>276
+org=>62, subtitle=>CAA, query=>BA, user=>BCC, indexed=>f, line=>277, disabled=>f, abstract=>ACA, date=>AB
+org=>63, bad=>t, line=>278, pos=>26, coauthors=>BA
+auth=>CBB, indexed=>f, line=>279, pos=>40, space=>CA, coauthors=>CC
+auth=>BA, line=>280, abstract=>AAA, public=>t, coauthors=>CAC
+org=>10, status=>16, line=>281, date=>CCC, space=>AC
+org=>76, user=>BBC, indexed=>f, line=>282, pos=>56, node=>CBA
+auth=>CA, subtitle=>AB, query=>AA, indexed=>t, line=>283, disabled=>f, coauthors=>ABC, node=>CAA
+title=>BA, status=>91, line=>284, pos=>7, coauthors=>BB
+wait=>CCA, line=>285, public=>t
+world=>AC, line=>286, disabled=>t
+line=>287, abstract=>AAA
+user=>CCB, status=>50, line=>288, public=>f
+state=>41, world=>CCC, query=>AA, line=>289, disabled=>t, pos=>49, public=>f
+wait=>CBC, line=>290, abstract=>CCA, space=>BBC
+auth=>CCB, world=>BAB, user=>CCC, status=>93, line=>291, pos=>77, node=>BAC
+wait=>BCC, org=>8, user=>AC, cleaned=>t, line=>292, disabled=>t, pos=>67, date=>AA
+org=>56, query=>BCA, line=>293, pos=>81, coauthors=>AAA, node=>CAB
+world=>CB, subtitle=>CBC, bad=>t, query=>ACB, indexed=>f, line=>294, pos=>58, date=>BC, node=>CB
+wait=>BC, user=>CA, line=>295
+world=>ABA, wait=>BA, user=>BB, status=>65, line=>296, pos=>45, date=>BB
+
+
+auth=>BA, user=>AA, indexed=>f, line=>299, space=>ABA, public=>f, coauthors=>BC
+line=>300, space=>ABA
+state=>36, org=>16, world=>BBC, status=>13, line=>301, public=>f
+subtitle=>CB, user=>BC, line=>302, date=>AA, coauthors=>CAC
+wait=>CBC, indexed=>t, cleaned=>t, line=>303, date=>ACC, public=>t
+user=>CAC, status=>81, line=>304, node=>CAB
+title=>CBB, org=>89, subtitle=>CAA, user=>CCA, indexed=>t, line=>305
+state=>10, title=>CBA, org=>66, cleaned=>t, line=>306, pos=>59, coauthors=>CAC
+
+auth=>AAA, world=>AC, wait=>ACA, subtitle=>BAA, status=>64, line=>308, node=>CCA
+state=>31, world=>CCC, title=>BCB, cleaned=>f, status=>11, line=>309, disabled=>t, date=>AA
+title=>BC, subtitle=>CB, indexed=>f, line=>310, disabled=>t, abstract=>BA, space=>ACA
+wait=>ABB, cleaned=>t, indexed=>f, line=>311, space=>CAB
+
+subtitle=>CA, line=>313
+org=>91, title=>CAB, line=>314, date=>CA
+
+state=>65, line=>316, node=>CC
+line=>317, space=>AA
+
+wait=>AA, indexed=>t, line=>319
+wait=>BB, org=>42, world=>AC, subtitle=>ACC, indexed=>t, line=>320, disabled=>t
+
+auth=>CAC, line=>322
+
+line=>324, pos=>38, space=>CC, node=>BBC
+title=>CC, line=>325, public=>t, coauthors=>BAC, node=>ACC
+world=>CC, subtitle=>BBC, bad=>f, user=>BA, line=>326, date=>AAA, space=>AA
+state=>81, title=>BC, wait=>BA, indexed=>f, status=>48, line=>327, coauthors=>AB
+line=>328, space=>ABB
+line=>329, date=>CCA
+
+auth=>BB, world=>BAB, subtitle=>BA, query=>ABB, line=>331, disabled=>t, date=>AAA, node=>BC
+auth=>ABA, title=>CC, user=>CBA, line=>332, disabled=>t, space=>ACC
+org=>98, subtitle=>ACB, line=>333, abstract=>BC, public=>f, coauthors=>BC, node=>ABA
+
+world=>BC, subtitle=>BAC, user=>AB, query=>BAA, cleaned=>t, line=>335, space=>AC, node=>BAA
+state=>76, indexed=>t, cleaned=>f, line=>336, node=>CAC
+org=>95, status=>84, line=>337
+
+world=>BBA, title=>BCC, subtitle=>ACB, query=>BA, line=>339, space=>ABC, node=>AC
+title=>CBB, user=>CBA, cleaned=>t, line=>340, public=>t, space=>CB, coauthors=>CAB
+wait=>AA, status=>82, line=>341
+world=>CC, line=>342
+auth=>BAB, title=>CAC, query=>BCC, indexed=>t, line=>343
+org=>77, world=>BAC, subtitle=>AA, user=>ABA, line=>344
+state=>99, org=>56, world=>CC, title=>CAB, wait=>CB, subtitle=>BCC, line=>345, pos=>65
+state=>68, org=>97, title=>AA, indexed=>t, line=>346, node=>CC
+state=>3, title=>CBC, user=>BAA, status=>98, line=>347, disabled=>t, pos=>96, date=>BBA
+auth=>BAA, world=>ABB, line=>348, disabled=>f, abstract=>ACA, pos=>66, space=>CCC, coauthors=>CBB, node=>BC
+
+status=>54, line=>350
+wait=>CC, query=>ABA, user=>AB, status=>76, cleaned=>f, line=>351, abstract=>CBA
+line=>352, disabled=>t, public=>f
+state=>93, org=>92, status=>88, line=>353, space=>AB, coauthors=>CB
+org=>34, wait=>ABC, world=>CBA, bad=>f, query=>BB, indexed=>f, line=>354, date=>CB, public=>t
+wait=>CBA, title=>CAC, cleaned=>t, indexed=>t, line=>355, pos=>9, date=>CAA
+user=>BC, indexed=>f, cleaned=>t, status=>73, line=>356, disabled=>t, space=>CB
+state=>20, cleaned=>f, line=>357, pos=>28, abstract=>CCB, space=>BC
+state=>17, wait=>ABC, query=>CB, cleaned=>f, status=>4, line=>358, disabled=>f
+
+state=>83, world=>CC, org=>53, cleaned=>f, status=>64, line=>360, abstract=>CBC, coauthors=>BC
+title=>BB, indexed=>f, line=>361
+state=>49, wait=>BCA, line=>362
+world=>CCC, title=>CA, query=>CCC, cleaned=>t, line=>363, space=>AA, coauthors=>AAC
+state=>8, wait=>BBB, line=>364, pos=>70, public=>f, space=>BAA, coauthors=>AB
+state=>20, indexed=>f, status=>87, cleaned=>t, line=>365, public=>t
+
+state=>92, title=>CCC, subtitle=>CAB, status=>39, line=>367
+state=>54, org=>38, line=>368
+
+auth=>ACA, subtitle=>CBC, status=>52, line=>370, date=>ACC, public=>t
+indexed=>t, line=>371, pos=>98, node=>CBA
+world=>BA, status=>40, line=>372, coauthors=>AA
+
+query=>BA, indexed=>f, cleaned=>t, line=>374, date=>BCC
+query=>CA, indexed=>t, line=>375, public=>f
+auth=>CCA, wait=>BBC, bad=>f, status=>91, line=>376, abstract=>BBC, date=>ABA
+user=>BA, query=>CB, status=>86, indexed=>f, line=>377, pos=>83, abstract=>BCC, space=>CBC, public=>t
+title=>ACA, org=>15, wait=>CBC, status=>85, line=>378
+state=>57, bad=>t, line=>379, abstract=>BC, date=>CAC
+world=>CC, cleaned=>t, line=>380
+title=>CB, subtitle=>AC, line=>381, public=>f
+
+
+status=>12, line=>384, coauthors=>CC
+auth=>BAC, bad=>f, line=>385, abstract=>CBB, public=>f, space=>BBC
+
+
+world=>BBC, bad=>t, status=>71, cleaned=>f, line=>388, node=>BB
+cleaned=>f, line=>389
+state=>73, line=>390
+wait=>BB, org=>5, subtitle=>BAA, bad=>f, indexed=>f, line=>391, public=>f, node=>BAA
+auth=>CCC, org=>51, bad=>f, cleaned=>t, line=>392, space=>AC, node=>CC
+
+line=>394, abstract=>ACC, public=>t
+org=>44, subtitle=>BAC, query=>BAC, line=>395
+wait=>BC, line=>396
+state=>68, world=>AB, title=>ABB, user=>CBC, cleaned=>f, indexed=>t, line=>397, abstract=>BA, pos=>11
+world=>CA, title=>AB, subtitle=>BC, user=>BCB, line=>398
+bad=>t, query=>BCC, line=>399
+wait=>BB, user=>BB, cleaned=>t, indexed=>f, line=>400, date=>BC, public=>f
+
+wait=>BA, line=>402
+title=>AC, subtitle=>BCB, query=>BA, line=>403
+
+auth=>BA, org=>19, query=>CCB, line=>405, pos=>82, date=>CAA
+state=>26, world=>CB, subtitle=>AB, cleaned=>f, line=>406, disabled=>t, date=>AC
+
+state=>11, bad=>t, indexed=>t, line=>408, pos=>79, abstract=>BA, date=>CB, space=>BBA
+auth=>AC, status=>59, line=>409
+org=>15, line=>410, disabled=>t, date=>BAC, space=>CCA
+
+
+state=>65, world=>AB, status=>69, line=>413, space=>BA
+
+title=>CCB, line=>415
+title=>BAB, subtitle=>CA, indexed=>f, line=>416, public=>t
+wait=>CAB, user=>CAB, cleaned=>t, line=>417, date=>BC, coauthors=>BBA
+subtitle=>ABA, user=>BB, query=>AA, indexed=>t, line=>418, pos=>8, space=>BB, coauthors=>CBA
+state=>11, indexed=>t, line=>419, node=>AA
+state=>86, cleaned=>f, line=>420, pos=>2, node=>CBC
+org=>73, line=>421, disabled=>f
+query=>BAC, user=>CB, status=>69, line=>422
+status=>22, line=>423
+auth=>CB, wait=>CCA, world=>AAB, line=>424, disabled=>f, space=>BA, public=>f
+state=>81, world=>AC, subtitle=>CBA, bad=>t, cleaned=>f, indexed=>t, line=>425, date=>AAB, coauthors=>BC, node=>BAC
+wait=>CB, query=>BCC, status=>97, line=>426
+org=>47, query=>CB, cleaned=>t, line=>427, date=>CC
+org=>33, query=>AC, status=>48, indexed=>f, line=>428, disabled=>t, abstract=>BC, space=>ACC
+org=>10, query=>AB, line=>429, pos=>77, date=>BC
+line=>430, pos=>7, abstract=>CCA, space=>AA
+bad=>f, user=>CA, query=>CAB, line=>431, node=>AC
+auth=>CA, bad=>f, line=>432
+
+auth=>BAA, org=>98, title=>CCC, world=>BAC, line=>434, public=>t
+state=>54, wait=>AA, user=>BBA, indexed=>f, line=>435, disabled=>t, pos=>12, space=>AB
+world=>AC, title=>CA, query=>AAA, line=>436, space=>AB, coauthors=>AA
+auth=>CB, wait=>CCC, bad=>f, line=>437, pos=>42, date=>ABC, space=>AB, coauthors=>ABC
+auth=>CBB, title=>BB, query=>CB, line=>438, pos=>15, abstract=>BC, node=>BBB
+title=>CC, line=>439, disabled=>f
+title=>AB, line=>440, disabled=>f
+org=>3, bad=>t, user=>BCB, query=>AB, indexed=>f, cleaned=>t, line=>441, disabled=>f, space=>BA, node=>BB
+state=>62, user=>BCC, status=>12, line=>442, pos=>58, date=>CC, node=>CB
+world=>BCB, bad=>t, line=>443, space=>AAB
+state=>56, bad=>f, cleaned=>f, line=>444, disabled=>f, date=>CA, space=>BBB, public=>t
+
+org=>31, world=>ABC, cleaned=>t, line=>446, disabled=>t, public=>t, coauthors=>CB
+state=>54, indexed=>t, line=>447
+state=>98, title=>AC, wait=>AAC, world=>BC, bad=>f, line=>448, disabled=>t, public=>t, node=>ABB
+world=>AAC, indexed=>t, line=>449, disabled=>t, pos=>61
+org=>56, title=>CA, line=>450
+auth=>BBB, line=>451, pos=>58, date=>BB, space=>ABA
+auth=>AB, world=>CA, cleaned=>t, line=>452
+bad=>t, line=>453, disabled=>t, abstract=>AC, pos=>20, date=>ABB, node=>CAB
+
+state=>91, wait=>AC, org=>96, world=>AA, subtitle=>BBC, query=>AA, cleaned=>t, line=>455, public=>f
+status=>99, line=>456, disabled=>t
+org=>86, line=>457, public=>t, coauthors=>AC
+status=>14, cleaned=>t, line=>458, disabled=>t
+world=>AB, user=>CB, query=>AAB, line=>459, pos=>66, public=>f, node=>BBA
+state=>58, world=>BB, wait=>CBA, title=>BCA, line=>460, pos=>95, abstract=>CCA, space=>BC, coauthors=>CB
+
+auth=>CAC, title=>AB, query=>BBA, user=>CB, line=>462, abstract=>BCC, pos=>89, coauthors=>ABB
+org=>13, bad=>f, query=>AA, status=>49, line=>463, disabled=>f
+bad=>t, cleaned=>f, line=>464, coauthors=>BB
+org=>14, query=>BA, line=>465, pos=>25, abstract=>BBA, space=>AAA, node=>CAC
+org=>63, title=>CA, subtitle=>ACC, query=>BAC, status=>76, line=>466, abstract=>ACA
+wait=>BA, subtitle=>BC, line=>467, disabled=>f, abstract=>AC
+org=>76, title=>CA, query=>AB, line=>468, public=>f
+state=>95, world=>AC, bad=>f, status=>65, cleaned=>f, line=>469, disabled=>f
+wait=>AB, subtitle=>AA, bad=>f, user=>CC, query=>BBC, status=>6, line=>470, date=>CCC
+state=>82, bad=>t, indexed=>t, line=>471, date=>BB, coauthors=>AAA
+
+state=>12, auth=>ACB, world=>CBC, bad=>f, indexed=>t, line=>473, date=>CA, space=>ABB, coauthors=>CC
+subtitle=>AA, bad=>f, user=>ACC, line=>474, pos=>86, abstract=>CAC, space=>BBA
+cleaned=>t, line=>475
+title=>CC, wait=>BB, status=>6, line=>476, abstract=>ACC, date=>CB, space=>BA, public=>t
+state=>96, wait=>BA, org=>30, subtitle=>BB, user=>CBB, status=>19, line=>477
+state=>78, org=>99, title=>CC, line=>478, node=>BAB
+world=>CBC, bad=>f, line=>479, date=>ACB, public=>t, node=>CB
+state=>0, query=>ABC, status=>65, line=>480, disabled=>t, space=>CBA, node=>BA
+auth=>BAC, org=>24, subtitle=>BBC, bad=>f, user=>CAC, line=>481, date=>BBB, public=>t, coauthors=>CBA
+org=>18, bad=>t, cleaned=>f, status=>3, indexed=>t, line=>482, date=>BB, coauthors=>ACC
+wait=>CB, user=>AC, line=>483, disabled=>f
+world=>AC, subtitle=>AA, query=>AAB, line=>484, disabled=>t, space=>CAA
+line=>485, pos=>2, space=>CA
+org=>42, indexed=>f, line=>486, date=>CB
+org=>3, wait=>CAA, subtitle=>CA, cleaned=>t, line=>487, disabled=>t
+org=>68, subtitle=>CCB, query=>CAA, cleaned=>f, status=>46, line=>488, pos=>87, public=>f, node=>BC
+
+status=>60, cleaned=>f, line=>490, space=>CC, node=>BCB
+state=>42, org=>9, subtitle=>CBA, user=>BA, status=>96, line=>491, pos=>36
+state=>16, title=>BCC, user=>ABC, indexed=>f, status=>24, line=>492, disabled=>t, node=>CBC
+auth=>CC, wait=>BBB, line=>493, disabled=>f, public=>f, coauthors=>AB
+
+wait=>BB, title=>BBC, subtitle=>BA, status=>3, cleaned=>f, line=>495, disabled=>f, coauthors=>AB, node=>BAC
+
+query=>CC, indexed=>f, line=>497, coauthors=>CAC, node=>BC
+auth=>BBA, state=>68, line=>498
+state=>21, title=>CCB, wait=>AAA, subtitle=>CCC, user=>BAA, indexed=>t, line=>499, coauthors=>BB
+auth=>AAA, subtitle=>CC, bad=>t, user=>CC, indexed=>t, line=>500, disabled=>t, date=>AB, node=>AC
+auth=>BB, title=>CCA, user=>BA, cleaned=>t, line=>501, pos=>37, space=>BA, public=>f
+auth=>BCA, line=>502, date=>BA
+world=>ABA, bad=>t, indexed=>f, line=>503, disabled=>t, abstract=>AC, pos=>1, public=>f
+auth=>BBB, subtitle=>ACB, line=>504, space=>AC, node=>BB
+auth=>CAC, state=>19, title=>ACA, wait=>BA, query=>CC, line=>505
+subtitle=>BC, cleaned=>f, indexed=>f, line=>506, date=>CAB, public=>f, node=>ABC
+state=>87, wait=>CCC, query=>CAC, user=>CBB, line=>507, abstract=>BBC, date=>AA, coauthors=>CA
+auth=>AC, subtitle=>BC, bad=>f, query=>ABA, user=>CBB, indexed=>t, cleaned=>f, line=>508, coauthors=>BA
+auth=>AA, title=>ABA, subtitle=>CCA, query=>CC, line=>509, pos=>27, node=>CBB
+org=>5, title=>CAC, subtitle=>BBB, line=>510, pos=>76, abstract=>AAB, space=>AA
+bad=>t, line=>511
+wait=>ACB, indexed=>f, line=>512
+auth=>CBA, world=>BA, bad=>t, user=>CBA, query=>CC, line=>513, public=>f, coauthors=>CC
+
+state=>97, wait=>BB, line=>515, date=>CBC, space=>CA
+auth=>CBC, line=>516, disabled=>t
+state=>91, user=>CCA, line=>517, coauthors=>BA, node=>CBA
+bad=>f, cleaned=>t, line=>518, space=>AAB
+
+title=>CA, cleaned=>f, status=>38, line=>520
+auth=>BCA, world=>AC, org=>71, user=>CA, line=>521, abstract=>AAB
+bad=>t, line=>522, pos=>28, abstract=>BAA
+line=>523, coauthors=>CBC, node=>AAB
+status=>51, cleaned=>f, line=>524
+query=>AAB, line=>525, disabled=>t, date=>AA, public=>t, coauthors=>CA
+org=>15, user=>AC, cleaned=>f, line=>526, coauthors=>CAC, node=>BAB
+world=>ABA, line=>527, disabled=>t, public=>t
+auth=>BBC, state=>48, bad=>f, line=>528, abstract=>BB, date=>BAC, space=>BA, public=>t
+auth=>BA, wait=>CAC, subtitle=>ABC, query=>CB, indexed=>f, cleaned=>f, line=>529, disabled=>t, date=>CA
+wait=>AC, world=>ABA, org=>55, bad=>t, indexed=>t, line=>530, pos=>32, space=>BCA, public=>t
+title=>CBC, wait=>BAA, line=>531
+world=>AA, line=>532, pos=>35, space=>AAB, public=>t
+line=>533, space=>AB, coauthors=>BA
+auth=>CBC, world=>BB, line=>534, space=>ACA, coauthors=>CBB
+wait=>ACA, status=>47, line=>535, public=>t, node=>BAA
+org=>16, subtitle=>BBB, line=>536, abstract=>AC, space=>CB, coauthors=>CC, node=>CBC
+wait=>AAB, line=>537, abstract=>AB, space=>CAC
+query=>CAC, line=>538
+world=>AC, query=>AAA, indexed=>f, status=>18, line=>539, pos=>62, space=>BC, coauthors=>BAC
+org=>30, world=>AA, query=>BC, user=>BAC, status=>12, cleaned=>t, line=>540, space=>AB
+org=>30, user=>CCB, query=>BB, cleaned=>f, line=>541, disabled=>t, public=>t, node=>CBA
+
+subtitle=>ABB, bad=>t, line=>543
+subtitle=>BBB, bad=>t, line=>544, pos=>43, coauthors=>ABB
+
+subtitle=>AB, user=>BA, line=>546, node=>CB
+title=>BBB, user=>AA, line=>547, abstract=>CBB, pos=>45
+wait=>CCB, title=>AC, world=>AAA, line=>548, abstract=>BBC, pos=>23, coauthors=>ACC
+org=>55, subtitle=>BA, line=>549, disabled=>t, date=>CB, space=>AA
+org=>39, cleaned=>t, line=>550, public=>f
+state=>41, auth=>CC, world=>CB, line=>551, space=>AAB
+
+state=>26, bad=>f, query=>BAA, status=>84, indexed=>t, line=>553, disabled=>f, coauthors=>CC, node=>CBB
+world=>ABA, user=>CCC, query=>ABB, line=>554, space=>ABC, node=>AAA
+state=>18, wait=>CCB, bad=>t, user=>BA, line=>555, space=>CC, coauthors=>BB, node=>BBB
+auth=>AA, state=>71, subtitle=>AA, query=>ACC, indexed=>t, line=>556, space=>BAB, public=>f
+indexed=>t, cleaned=>t, line=>557, disabled=>f, abstract=>AB
+auth=>BCC, title=>ACB, world=>BCA, user=>BAB, cleaned=>f, line=>558, space=>BB, coauthors=>CBC
+
+auth=>ACC, org=>18, wait=>AB, status=>1, indexed=>t, line=>560
+status=>8, line=>561, abstract=>BA, public=>f
+state=>27, title=>ABA, bad=>t, query=>AAB, indexed=>f, line=>562, pos=>86, public=>t, coauthors=>BA
+
+title=>BAC, wait=>CCC, user=>BA, line=>564, disabled=>f, date=>BB, public=>t, space=>CB, coauthors=>CCB
+wait=>CAA, line=>565, pos=>80, space=>AB
+auth=>CBB, subtitle=>BCA, user=>CB, line=>566, abstract=>BC, date=>AB
+title=>CCB, status=>78, line=>567, pos=>68, node=>BA
+auth=>BC, query=>AB, line=>568, space=>AB, node=>BB
+
+line=>570, pos=>54
+world=>BBB, user=>CC, indexed=>t, line=>571, abstract=>CC, coauthors=>BA, node=>ABB
+state=>41, line=>572
+subtitle=>CBC, cleaned=>t, line=>573, node=>BCB
+title=>ABA, line=>574, pos=>27, space=>CC
+status=>29, indexed=>f, cleaned=>f, line=>575, pos=>52, public=>f, coauthors=>ACC
+title=>BBB, org=>86, wait=>AAA, user=>CC, query=>CA, line=>576, disabled=>f, date=>AB, node=>BC
+line=>577, abstract=>CAA, date=>BB
+auth=>CCC, subtitle=>BBB, query=>ABA, line=>578, pos=>99, space=>CCB, public=>t, coauthors=>ACA, node=>ACB
+wait=>BCC, line=>579
+state=>99, world=>BAC, user=>CA, line=>580
+state=>55, world=>AAA, title=>AAA, cleaned=>f, line=>581, date=>AC, public=>t, node=>AA
+query=>ACC, cleaned=>t, line=>582, disabled=>f
+auth=>AAB, query=>BAC, line=>583
+auth=>AA, user=>BAC, line=>584
+
+org=>96, wait=>BC, bad=>f, cleaned=>f, status=>96, line=>586, pos=>95
+auth=>BC, subtitle=>BCB, bad=>t, user=>BBC, line=>587, pos=>79, node=>BA
+state=>55, line=>588
+title=>ABC, world=>AB, subtitle=>CBC, user=>BA, query=>BAB, line=>589, date=>AC, node=>CB
+world=>BAA, bad=>f, user=>AAB, cleaned=>f, indexed=>f, line=>590
+title=>CB, wait=>BC, subtitle=>BAC, cleaned=>t, line=>591, disabled=>f, abstract=>CBB, public=>f, node=>ACC
+user=>BC, line=>592, public=>f
+
+wait=>CC, org=>57, title=>BAC, line=>594, abstract=>AA
+auth=>BBC, state=>3, world=>AAC, query=>BA, line=>595, coauthors=>BB
+
+subtitle=>CC, user=>CC, line=>597
+wait=>BBA, user=>AAA, line=>598, space=>ACB, node=>AA
+auth=>BB, user=>ABA, line=>599, abstract=>AB, node=>BA
+
+world=>AAA, user=>BB, cleaned=>f, line=>601, space=>AC, coauthors=>ABB
+title=>CAB, bad=>f, line=>602, coauthors=>ABB
+
+world=>CCC, org=>79, line=>604
+org=>56, query=>AB, cleaned=>t, indexed=>t, status=>20, line=>605, public=>t, coauthors=>ACA
+auth=>BBC, org=>13, subtitle=>CC, bad=>t, user=>ABC, line=>606, date=>CA, public=>f
+query=>BA, line=>607
+bad=>t, line=>608, pos=>12, coauthors=>CB
+bad=>f, status=>42, line=>609
+
+bad=>t, line=>611
+auth=>CCA, subtitle=>BC, bad=>t, query=>CAA, cleaned=>f, line=>612, public=>f, node=>CBA
+org=>65, query=>BC, line=>613
+
+wait=>BAC, title=>AAB, user=>CAC, line=>615, pos=>69, space=>CC, node=>AAC
+bad=>f, line=>616, abstract=>AB, pos=>65, coauthors=>BBB
+
+org=>38, world=>BA, line=>618, coauthors=>AA, node=>BC
+cleaned=>f, line=>619, disabled=>f
+auth=>BC, line=>620, pos=>79, date=>AB, coauthors=>BAA, node=>CB
+auth=>CAA, title=>CB, user=>BAC, cleaned=>f, line=>621, public=>f, space=>CBA
+
+bad=>f, status=>12, line=>623
+auth=>BBB, wait=>BAC, org=>36, title=>AB, indexed=>f, cleaned=>f, line=>624, date=>AB, coauthors=>CB
+wait=>AA, subtitle=>AB, query=>CCB, line=>625, node=>CBB
+wait=>BC, subtitle=>BA, bad=>t, user=>AA, line=>626, pos=>3, date=>BB
+org=>28, user=>BC, query=>AC, status=>63, line=>627, pos=>45, public=>t, node=>BC
+query=>BC, status=>47, line=>628, disabled=>f, date=>CA, public=>f
+
+wait=>CB, line=>630, pos=>67, coauthors=>AC
+org=>33, world=>BBB, query=>BB, status=>92, line=>631
+state=>65, title=>AC, world=>CBC, query=>CBC, line=>632, date=>CAC, space=>CC, coauthors=>CC
+
+auth=>CC, query=>BCA, status=>46, line=>634, disabled=>f, pos=>69
+wait=>CB, line=>635, pos=>34
+state=>9, wait=>CC, status=>23, line=>636, disabled=>t, date=>BB, space=>AC
+user=>CCB, indexed=>f, cleaned=>t, line=>637, pos=>65, date=>AA, public=>t
+auth=>BC, wait=>AB, title=>BB, bad=>t, line=>638, abstract=>ACC, date=>BC, public=>f
+state=>44, auth=>BC, world=>CBC, line=>639, disabled=>f, date=>CAA
+world=>CB, title=>ACB, user=>BA, query=>AA, line=>640, disabled=>t, space=>AC
+state=>37, line=>641, disabled=>t, pos=>66
+world=>AAA, bad=>t, user=>AAA, query=>BA, line=>642, disabled=>t, coauthors=>CBC
+world=>BA, title=>ABB, org=>96, bad=>f, query=>AAA, status=>75, cleaned=>f, line=>643, space=>BA
+state=>36, org=>66, subtitle=>AA, query=>CA, cleaned=>t, status=>79, line=>644, date=>CB
+wait=>BC, line=>645, date=>CBA, space=>BCB, public=>t, node=>ABA
+auth=>BB, org=>37, query=>CAA, indexed=>t, line=>646, abstract=>CBA, coauthors=>CBA
+
+
+state=>58, world=>BAB, org=>11, user=>CC, line=>649
+title=>CB, status=>19, line=>650, disabled=>f, public=>f, coauthors=>AA
+user=>BBC, indexed=>t, line=>651, disabled=>t, pos=>8
+query=>CC, cleaned=>t, indexed=>f, line=>652, pos=>67, date=>AA
+auth=>AAC, line=>653, disabled=>t, public=>f, coauthors=>AAA, node=>CBB
+bad=>t, query=>AC, line=>654, disabled=>f
+world=>CCA, org=>15, bad=>f, user=>CCC, line=>655, public=>t, space=>CC
+line=>656, coauthors=>BBB
+title=>BA, line=>657, date=>ACB
+user=>BC, query=>CC, cleaned=>t, line=>658, pos=>51, abstract=>BA
+subtitle=>CCA, user=>CCA, cleaned=>f, line=>659, abstract=>BA, pos=>95, date=>CA
+auth=>CA, state=>23, org=>19, bad=>f, user=>BCB, indexed=>f, line=>660, date=>ABA
+state=>64, org=>97, bad=>f, indexed=>f, line=>661, space=>BAB, coauthors=>BB, node=>BA
+status=>11, line=>662
+title=>BCC, org=>44, subtitle=>ACB, cleaned=>f, line=>663, pos=>58
+auth=>ABB, bad=>t, line=>664, pos=>82, coauthors=>CC, node=>AB
+bad=>f, cleaned=>t, status=>25, line=>665, disabled=>f, abstract=>BB, public=>t
+wait=>AC, user=>CB, line=>666, pos=>71, abstract=>ACA, coauthors=>CBB
+title=>AA, bad=>t, user=>BB, line=>667, date=>CA, space=>BC, node=>CC
+
+auth=>AAB, line=>669
+wait=>AAC, query=>ABA, status=>35, line=>670, disabled=>f, pos=>56
+org=>3, line=>671
+state=>46, bad=>f, cleaned=>t, line=>672
+state=>30, org=>9, status=>72, line=>673, abstract=>ACA, coauthors=>CB
+auth=>BB, wait=>CA, title=>BBB, bad=>t, user=>AAA, status=>86, indexed=>f, line=>674, node=>BCC
+indexed=>t, line=>675, pos=>63
+bad=>t, query=>CBB, status=>5, line=>676, abstract=>CCC, public=>f, space=>BB
+title=>BBB, org=>60, bad=>t, cleaned=>f, line=>677, pos=>82, date=>BAA, space=>BB, coauthors=>CAA
+
+
+state=>73, bad=>f, cleaned=>f, line=>680, abstract=>CA, date=>CCA, space=>CB
+state=>92, query=>CC, line=>681, abstract=>AB, date=>BBB, public=>t, coauthors=>CBA
+subtitle=>CCA, line=>682
+world=>BAC, subtitle=>AC, line=>683, disabled=>t, abstract=>AA, pos=>55, space=>AC, node=>CA
+state=>75, world=>ACA, query=>BC, line=>684, coauthors=>AAC
+status=>21, line=>685
+state=>39, wait=>CB, title=>CBC, query=>BB, cleaned=>t, line=>686, disabled=>f
+world=>CCA, wait=>AB, user=>CC, query=>BB, cleaned=>t, line=>687
+auth=>CAC, state=>94, wait=>ACC, title=>BBC, user=>BB, line=>688, disabled=>f, pos=>16, coauthors=>AAC
+
+org=>43, line=>690
+
+state=>4, title=>CA, subtitle=>AA, query=>BC, line=>692, pos=>57, date=>BCA, public=>f, coauthors=>ABB
+wait=>BBA, line=>693
+auth=>BCA, bad=>f, user=>BBA, line=>694, disabled=>f, date=>CC, public=>t, coauthors=>CB
+state=>66, wait=>BB, user=>CC, indexed=>t, line=>695, pos=>99, space=>BCA
+org=>1, line=>696, disabled=>f, space=>BCC, coauthors=>BC
+auth=>BC, cleaned=>t, indexed=>f, line=>697, space=>CBB
+wait=>AC, indexed=>f, line=>698, pos=>44
+wait=>AA, title=>BBB, org=>31, indexed=>t, line=>699, disabled=>f
+auth=>BB, world=>ACC, bad=>t, indexed=>f, line=>700, abstract=>CB, pos=>5, space=>ACB, node=>CC
+cleaned=>f, line=>701, space=>CB
+line=>702, space=>CCC
+world=>CA, subtitle=>ABA, line=>703, pos=>5, date=>BA, coauthors=>AB
+
+line=>705, date=>BBB
+state=>10, query=>CB, status=>70, line=>706, abstract=>ABA, date=>BC
+auth=>CB, line=>707
+wait=>BBA, cleaned=>t, line=>708, pos=>94, date=>CBC
+state=>86, org=>5, world=>BB, indexed=>f, line=>709, date=>BBB, space=>CA, public=>t
+world=>ACA, query=>ABC, status=>40, line=>710, disabled=>t, public=>t, node=>CA
+bad=>t, line=>711
+query=>AB, line=>712, coauthors=>BBC, node=>AA
+user=>ABB, line=>713, public=>f, space=>AAA, node=>BBA
+auth=>AC, wait=>BAC, bad=>t, line=>714, public=>f
+line=>715, abstract=>CA, public=>f
+user=>AC, indexed=>t, line=>716, coauthors=>CB
+state=>4, title=>ABB, org=>26, indexed=>t, line=>717, public=>t, coauthors=>CCA, node=>AC
+wait=>CA, title=>CCA, world=>CCC, line=>718, abstract=>ACA
+auth=>ACA, org=>29, subtitle=>AA, user=>CA, status=>24, indexed=>t, line=>719, public=>f, node=>CA
+
+line=>721, disabled=>t, abstract=>BAC
+world=>BC, line=>722
+state=>27, auth=>AA, title=>BC, world=>CC, query=>BCC, line=>723, disabled=>t, pos=>9, public=>f, node=>BCC
+org=>78, wait=>ABA, cleaned=>t, indexed=>t, line=>724, date=>ACB, space=>AA
+state=>60, line=>725
+
+
+wait=>ABA, title=>CAC, user=>CCC, line=>728
+wait=>CC, indexed=>t, status=>39, line=>729, disabled=>t, public=>f
+auth=>CB, subtitle=>BBA, line=>730, coauthors=>CAC
+world=>CBB, line=>731, space=>BCB
+cleaned=>t, line=>732
+org=>67, bad=>t, line=>733, pos=>9, node=>ACC
+world=>BC, wait=>CAC, org=>58, subtitle=>ACC, bad=>t, query=>CAA, line=>734, abstract=>BCA, pos=>1, public=>t
+state=>45, query=>AB, indexed=>f, line=>735, pos=>82, date=>BC, public=>f, coauthors=>BA
+state=>68, title=>BC, cleaned=>t, status=>34, line=>736, disabled=>t, node=>BBB
+auth=>AC, line=>737
+line=>738, date=>BA, space=>CCC, public=>f
+line=>739, node=>BAA
+org=>72, title=>BC, line=>740, pos=>51, coauthors=>CA
+state=>72, user=>CCB, query=>ACA, line=>741
+org=>80, subtitle=>BBA, bad=>t, user=>BC, line=>742, pos=>52, coauthors=>BCA
+
+query=>BC, line=>744, abstract=>AB, public=>f, node=>BAC
+world=>CAC, line=>745
+auth=>CBB, title=>AA, user=>AB, line=>746, pos=>35, public=>f, space=>AAB
+state=>69, world=>AB, org=>78, subtitle=>BA, bad=>f, line=>747, node=>AAA
+bad=>t, line=>748, public=>t
+wait=>BC, org=>47, query=>BBB, line=>749
+title=>BBB, line=>750
+org=>33, query=>CB, line=>751, disabled=>t
+subtitle=>BB, line=>752, space=>CC
+org=>89, line=>753
+auth=>ABA, line=>754, coauthors=>ACC
+subtitle=>BA, line=>755, pos=>47
+state=>81, subtitle=>CB, query=>AB, status=>25, cleaned=>f, line=>756, pos=>72, date=>BA, coauthors=>BCA
+state=>46, status=>88, line=>757, disabled=>f, public=>t
+world=>AB, line=>758, disabled=>t, abstract=>BB, coauthors=>AAA
+query=>AC, line=>759, abstract=>AAB
+auth=>BC, indexed=>f, line=>760, abstract=>BA, node=>CAA
+state=>10, auth=>BAC, title=>BC, query=>BCA, cleaned=>t, line=>761, disabled=>t, space=>ACC, coauthors=>ABA
+line=>762, disabled=>t, pos=>43
+world=>CBA, user=>BBC, indexed=>t, line=>763
+wait=>ACB, query=>BA, status=>22, line=>764, pos=>70, abstract=>BAC, public=>f, space=>BC
+
+line=>766, disabled=>f, abstract=>CBC, date=>CA
+title=>CC, bad=>t, user=>BCC, indexed=>f, line=>767, date=>BCB, node=>AAA
+title=>CB, line=>768, abstract=>AA, node=>ABB
+org=>21, user=>ABC, line=>769, abstract=>BB, date=>CBB, space=>CC
+auth=>AC, org=>66, user=>CC, line=>770, public=>f, space=>CA, coauthors=>AA
+org=>58, line=>771, coauthors=>BCC, node=>AC
+
+auth=>BC, wait=>CC, line=>773, abstract=>ACC, pos=>98, date=>CCC, space=>ABB, node=>CB
+
+query=>BC, user=>AC, indexed=>t, line=>775, abstract=>AAA
+subtitle=>BAA, indexed=>f, line=>776
+line=>777, pos=>33, date=>CCB, public=>t
+world=>BCA, bad=>t, line=>778
+auth=>CA, line=>779, date=>AC, space=>CAC
+title=>BB, bad=>f, cleaned=>t, line=>780, disabled=>f, date=>BAB, space=>ACB
+auth=>CAC, title=>AAB, subtitle=>CA, bad=>f, line=>781, disabled=>f, space=>CB
+state=>78, auth=>AC, bad=>t, status=>46, line=>782, abstract=>CCA, pos=>97, public=>t
+user=>BBA, line=>783
+
+state=>63, title=>CA, cleaned=>t, line=>785, abstract=>BA, space=>BCC
+line=>786, node=>CAC
+line=>787, pos=>65
+line=>788, space=>ABB
+
+org=>14, line=>790, abstract=>CAB, coauthors=>BBC
+subtitle=>CBA, cleaned=>f, line=>791, disabled=>f, pos=>57, node=>CB
+auth=>CAA, org=>84, wait=>AB, indexed=>t, status=>51, line=>792, abstract=>CC
+org=>72, bad=>t, line=>793, space=>ACA
+
+auth=>BC, state=>76, wait=>CC, user=>ABB, cleaned=>f, line=>795, pos=>99, abstract=>CA
+wait=>CCA, world=>CBC, line=>796, date=>CB, public=>f
+state=>49, line=>797, coauthors=>CC
+wait=>BBB, title=>ABB, org=>74, line=>798, disabled=>f, pos=>34, space=>BB
+line=>799, abstract=>CB
+state=>84, user=>ABB, cleaned=>f, status=>18, line=>800, disabled=>t, date=>CCA, node=>BA
+state=>81, auth=>CB, world=>CA, user=>CAA, line=>801, date=>AC, space=>CBC, coauthors=>BCB
+org=>4, line=>802, disabled=>f, abstract=>ABA, public=>f
+auth=>CBC, state=>99, cleaned=>t, line=>803, disabled=>t, space=>BC, node=>BBC
+auth=>AC, wait=>CA, cleaned=>f, line=>804, pos=>54, date=>BAA, public=>t, space=>AB
+
+auth=>BCB, wait=>BCC, subtitle=>AAA, line=>806
+line=>807, disabled=>f, space=>ACA
+org=>96, query=>CBA, line=>808, disabled=>f, pos=>74, space=>CA, public=>f
+
+state=>12, title=>AA, bad=>f, status=>20, line=>810, disabled=>t, coauthors=>CAC, node=>AB
+auth=>ABC, line=>811, date=>CA
+title=>AB, indexed=>f, line=>812, disabled=>f, node=>AAC
+
+world=>CBA, status=>15, line=>814, abstract=>CBA
+status=>49, line=>815, pos=>49
+subtitle=>CAB, line=>816
+
+
+world=>CAC, title=>CB, wait=>AA, query=>CA, indexed=>t, line=>819, disabled=>t
+auth=>ABB, wait=>AC, query=>CC, cleaned=>t, indexed=>f, line=>820, abstract=>AA, public=>f, node=>AB
+org=>5, wait=>BA, indexed=>t, line=>821, node=>AB
+title=>CC, wait=>CC, bad=>f, query=>BCC, indexed=>f, line=>822, pos=>27, date=>CB, node=>CBA
+query=>BC, status=>28, line=>823, public=>f
+status=>1, line=>824, abstract=>BB
+
+auth=>AA, title=>BC, query=>CA, status=>33, line=>826
+state=>9, title=>BB, subtitle=>ACC, bad=>t, query=>BA, status=>41, line=>827, abstract=>ACB, public=>f
+auth=>AB, subtitle=>CAB, line=>828, public=>f, coauthors=>AB, node=>BAC
+line=>829, disabled=>f, public=>t, node=>CBC
+auth=>BAB, line=>830
+wait=>BBA, bad=>t, indexed=>f, line=>831, space=>BB
+org=>70, wait=>BC, world=>AC, indexed=>t, status=>96, line=>832, disabled=>t, space=>AB
+state=>8, world=>BAB, bad=>t, indexed=>t, status=>18, line=>833, date=>BA, space=>BA
+query=>AB, line=>834
+bad=>t, status=>5, line=>835
+world=>BAC, subtitle=>BB, bad=>f, user=>AB, indexed=>f, cleaned=>t, line=>836
+line=>837, public=>f
+line=>838, pos=>7
+auth=>CA, query=>ABB, indexed=>t, line=>839, public=>t
+wait=>CC, bad=>f, line=>840, date=>AAB, public=>f, coauthors=>BCB
+auth=>AB, state=>97, org=>24, line=>841, pos=>41, node=>BC
+wait=>BB, world=>CBA, user=>BAA, status=>18, line=>842, date=>BAB, public=>t
+title=>BA, subtitle=>CA, query=>CCB, line=>843, space=>BB
+auth=>BB, world=>ACA, line=>844, pos=>29
+state=>65, org=>40, query=>CAA, user=>AB, indexed=>f, cleaned=>f, line=>845
+title=>CB, cleaned=>f, indexed=>f, line=>846
+wait=>CAA, indexed=>f, line=>847, disabled=>f
+
+title=>CB, query=>CC, line=>849, abstract=>CB, pos=>10, public=>t
+auth=>CB, subtitle=>BA, cleaned=>t, line=>850, disabled=>t, pos=>84
+org=>45, wait=>BA, query=>CCC, line=>851, date=>BCA, coauthors=>CAB
+state=>84, title=>CB, line=>852, pos=>71, space=>CA
+line=>853, public=>t, node=>AA
+subtitle=>BC, user=>AB, cleaned=>t, line=>854, public=>t
+state=>26, wait=>BA, world=>BBB, user=>BB, status=>53, line=>855, abstract=>ABA, pos=>72, space=>AC
+line=>856, public=>f, coauthors=>CA
+wait=>ABB, subtitle=>CBA, line=>857, pos=>44
+auth=>ABA, wait=>CC, org=>0, bad=>t, query=>BB, line=>858, disabled=>f, public=>t
+bad=>f, user=>AA, line=>859, pos=>90
+world=>BCC, title=>BAA, bad=>f, user=>CAA, query=>BBC, line=>860, date=>CAA, space=>BCB, public=>t, coauthors=>CB
+title=>BAB, world=>BC, subtitle=>AA, cleaned=>f, status=>9, line=>861, pos=>95
+title=>AC, line=>862
+wait=>CB, bad=>f, status=>89, line=>863, coauthors=>AB
+subtitle=>ACC, indexed=>t, cleaned=>t, line=>864
+title=>AB, subtitle=>CBB, query=>ACA, indexed=>t, line=>865, disabled=>t
+title=>BC, world=>BB, query=>AA, user=>ACB, status=>43, cleaned=>f, line=>866, coauthors=>CBC, node=>ACB
+
+org=>25, wait=>AC, indexed=>f, line=>868, disabled=>f, abstract=>CA, pos=>48
+bad=>t, status=>34, line=>869, pos=>32, date=>AC, public=>t, node=>AA
+state=>33, wait=>AAC, indexed=>t, status=>20, line=>870, abstract=>BA
+wait=>CCC, subtitle=>AC, line=>871, disabled=>f, space=>BA, public=>t, coauthors=>BCC
+status=>49, line=>872
+state=>90, title=>ACC, world=>CBB, subtitle=>BAB, bad=>f, status=>94, line=>873, abstract=>CB
+title=>BCB, line=>874
+cleaned=>f, line=>875
+wait=>BAA, subtitle=>BBC, line=>876
+auth=>AB, org=>35, bad=>f, indexed=>f, line=>877, coauthors=>BBA
+line=>878, public=>f
+
+auth=>CB, wait=>CBC, indexed=>f, line=>880, public=>t
+query=>CC, status=>4, line=>881, disabled=>t, node=>CA
+title=>BB, line=>882
+state=>53, bad=>f, cleaned=>f, status=>63, line=>883, coauthors=>BAA
+auth=>ACA, world=>AC, user=>CBC, line=>884, date=>BCA, node=>BBC
+auth=>BAB, state=>11, world=>CB, org=>77, query=>BA, cleaned=>f, line=>885, space=>AC
+world=>CA, user=>CA, line=>886, node=>CB
+state=>32, org=>50, wait=>AA, line=>887, disabled=>f, space=>BBA, public=>f
+cleaned=>t, line=>888, pos=>70, node=>ABC
+org=>63, user=>AAB, query=>BB, line=>889, date=>BC, space=>CBB, node=>ABC
+wait=>BB, user=>BB, query=>CB, line=>890, space=>BB, coauthors=>BA, node=>ABC
+auth=>BC, world=>CC, subtitle=>CB, line=>891, public=>f, coauthors=>BC
+state=>13, org=>38, line=>892, coauthors=>BC, node=>ABC
+auth=>CC, world=>CAC, line=>893, date=>BBA, node=>CBC
+
+auth=>AA, line=>895, coauthors=>BB
+auth=>AA, state=>76, status=>85, line=>896, date=>CCC, public=>t, coauthors=>AB
+auth=>AB, indexed=>t, cleaned=>f, line=>897
+line=>898, coauthors=>CBB
+
+wait=>ACC, line=>900, abstract=>BBA
+auth=>AA, wait=>BCB, cleaned=>f, line=>901, abstract=>AAC
+state=>68, title=>AC, subtitle=>BB, line=>902
+state=>41, wait=>ABA, bad=>f, user=>BBA, status=>46, line=>903, node=>AAB
+
+cleaned=>f, line=>905, pos=>33
+bad=>f, query=>BA, line=>906, pos=>48, space=>CB, public=>t
+query=>CB, indexed=>t, line=>907, pos=>41, abstract=>CBB, space=>BA, public=>f, node=>BC
+title=>AB, line=>908
+auth=>BC, title=>CB, line=>909, disabled=>f, space=>CA, public=>t, coauthors=>BC
+world=>AA, user=>ABA, indexed=>f, line=>910, abstract=>CC
+auth=>CCA, indexed=>f, line=>911, date=>AC, public=>f
+world=>AAB, bad=>t, line=>912
+subtitle=>CBB, line=>913, public=>t
+wait=>CB, line=>914, disabled=>f, pos=>71, date=>BA, space=>CBA, public=>f, coauthors=>BB
+org=>67, wait=>CA, bad=>f, line=>915, disabled=>f, public=>t
+
+line=>917, date=>CB
+auth=>CBB, world=>AAA, status=>83, indexed=>f, line=>918, disabled=>t, date=>CBA, coauthors=>ACC
+wait=>AB, title=>BA, status=>33, line=>919, disabled=>f
+wait=>ACB, cleaned=>f, line=>920, abstract=>AA, coauthors=>BCB
+wait=>ABB, org=>40, world=>BC, subtitle=>CA, user=>AAC, status=>14, indexed=>t, line=>921, pos=>66
+auth=>BA, org=>22, wait=>BAB, bad=>t, user=>ACC, status=>32, line=>922
+world=>BA, query=>CAB, status=>0, line=>923
+org=>84, bad=>t, line=>924, coauthors=>BAB
+auth=>ACC, subtitle=>AAA, query=>CCA, cleaned=>f, line=>925, pos=>60, space=>BC
+wait=>BC, subtitle=>CCC, bad=>f, cleaned=>f, indexed=>f, line=>926, public=>f, coauthors=>AC
+auth=>CA, state=>91, org=>6, world=>AA, wait=>ABB, query=>AAC, line=>927, date=>CA, node=>BAC
+world=>BCA, query=>AA, user=>BBC, line=>928, disabled=>f
+world=>CBC, user=>CBC, line=>929, date=>CAC
+world=>BCB, bad=>f, user=>BB, line=>930
+auth=>CA, world=>AA, query=>ABA, user=>AA, indexed=>t, line=>931, coauthors=>BBC
+auth=>BAA, bad=>t, line=>932, disabled=>f, pos=>93, abstract=>CCA, date=>BBA, coauthors=>AA
+line=>933, space=>CAB, node=>AB
+
+status=>60, cleaned=>t, indexed=>t, line=>935, pos=>35, space=>CAB, node=>BBB
+wait=>AAA, bad=>t, status=>26, line=>936, abstract=>ACB, space=>BBA, coauthors=>CCC
+title=>BA, bad=>t, line=>937, date=>BBA, public=>t
+
+query=>BA, user=>BA, line=>939, disabled=>t
+line=>940, date=>CC
+wait=>CAB, query=>BCA, user=>BC, line=>941, pos=>8, coauthors=>ACC
+line=>942, space=>BA
+auth=>CA, org=>11, line=>943, pos=>99
+org=>83, line=>944, disabled=>f, date=>BBA, space=>AC, node=>AC
+world=>CCA, line=>945, node=>BC
+org=>95, title=>CA, world=>BA, line=>946, pos=>36, coauthors=>CA
+state=>93, line=>947
+cleaned=>f, status=>5, line=>948, abstract=>BB, public=>f, coauthors=>ABC
+world=>CA, org=>61, bad=>f, query=>CC, cleaned=>t, line=>949, pos=>14, space=>CC
+state=>91, line=>950, abstract=>BA, date=>AB
+auth=>BBC, line=>951, date=>BB
+auth=>BAB, line=>952, disabled=>t, node=>AAA
+auth=>CAA, subtitle=>ABA, bad=>t, line=>953
+auth=>CA, wait=>BB, org=>12, user=>BCC, cleaned=>f, line=>954, public=>f, coauthors=>AA
+org=>93, cleaned=>t, line=>955, disabled=>t, public=>t, node=>ACA
+line=>956, pos=>10
+org=>74, world=>CCC, subtitle=>AB, user=>AAA, cleaned=>t, line=>957, pos=>70, public=>t, node=>CC
+state=>51, line=>958
+world=>CCA, title=>BCB, user=>AB, indexed=>t, line=>959, disabled=>t, pos=>21, date=>CBC
+org=>86, wait=>BC, query=>BB, user=>AA, indexed=>t, line=>960, pos=>58, date=>AB
+line=>961, node=>CC
+auth=>BCB, world=>ACC, subtitle=>CA, bad=>t, user=>BA, indexed=>f, line=>962, public=>f
+
+status=>37, line=>964
+state=>70, status=>76, indexed=>f, line=>965, disabled=>t, space=>BB
+
+state=>67, world=>CA, title=>AA, line=>967, abstract=>BA, space=>BAA
+auth=>CA, world=>AA, bad=>t, query=>BC, status=>53, indexed=>f, line=>968, date=>AB, node=>BAA
+query=>AC, cleaned=>t, line=>969, abstract=>BC, space=>CAB, coauthors=>BAA
+wait=>BCA, world=>CB, title=>BC, indexed=>f, line=>970, disabled=>t, pos=>70, date=>AB
+
+subtitle=>BC, query=>AA, line=>972
+line=>973, public=>t
+org=>75, world=>AAB, subtitle=>BB, user=>CC, line=>974, space=>CA
+auth=>BCB, cleaned=>t, line=>975
+title=>BAC, user=>CB, line=>976, public=>f
+subtitle=>BAC, indexed=>f, cleaned=>f, line=>977, disabled=>f, abstract=>ABC, space=>ABA
+state=>63, bad=>f, line=>978, pos=>93, node=>AAC
+
+cleaned=>f, line=>980, abstract=>CCB
+state=>40, title=>ABA, subtitle=>CAB, query=>BC, line=>981, date=>CA, coauthors=>AB
+
+auth=>ABA, subtitle=>ACC, user=>AA, query=>AC, cleaned=>t, line=>983, date=>ACB, node=>CB
+state=>32, title=>ABC, org=>58, status=>95, line=>984, disabled=>t, pos=>6, space=>CBB
+title=>BCC, subtitle=>CCC, user=>BBC, line=>985, public=>f, coauthors=>CCB, node=>AA
+subtitle=>ACA, query=>BCC, status=>43, cleaned=>t, indexed=>t, line=>986, abstract=>CAC
+
+world=>CAB, org=>21, indexed=>t, line=>988, abstract=>ABC
+title=>CBC, status=>66, line=>989
+
+line=>991, abstract=>BA, node=>BBB
+line=>992, disabled=>t, pos=>29, public=>f
+state=>53, wait=>CB, subtitle=>CCC, line=>993, date=>CAC, public=>f, coauthors=>BB
+wait=>CBA, title=>CA, subtitle=>BB, user=>BAA, line=>994, disabled=>t, date=>BB, coauthors=>CCC, node=>CC
+title=>BB, user=>AA, query=>CAA, status=>43, line=>995, pos=>6, abstract=>CC, public=>t
+wait=>AC, query=>BA, line=>996, coauthors=>BB, node=>CCC
+auth=>BC, title=>CAC, subtitle=>BA, line=>997, date=>BAA
+wait=>AB, user=>ABC, line=>998, pos=>41, node=>CAC
+state=>4, title=>AC, bad=>t, status=>59, line=>999, disabled=>t
+user=>BC, line=>1000
+wait=>NULL, line=>1000
diff --git a/contrib/hstore/expected/hstore.out b/contrib/hstore/expected/hstore.out
new file mode 100644
index 0000000..64a3272
--- /dev/null
+++ b/contrib/hstore/expected/hstore.out
@@ -0,0 +1,1601 @@
+CREATE EXTENSION hstore;
+-- Check whether any of our opclasses fail amvalidate
+SELECT amname, opcname
+FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
+WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
+ amname | opcname
+--------+---------
+(0 rows)
+
+set escape_string_warning=off;
+--hstore;
+select ''::hstore;
+ hstore
+--------
+
+(1 row)
+
+select 'a=>b'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select ' a=>b'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select 'a =>b'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select 'a=>b '::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select 'a=> b'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select '"a"=>"b"'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select ' "a"=>"b"'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select '"a" =>"b"'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select '"a"=>"b" '::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select '"a"=> "b"'::hstore;
+ hstore
+----------
+ "a"=>"b"
+(1 row)
+
+select 'aa=>bb'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select ' aa=>bb'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select 'aa =>bb'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select 'aa=>bb '::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select 'aa=> bb'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select '"aa"=>"bb"'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select ' "aa"=>"bb"'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select '"aa" =>"bb"'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select '"aa"=>"bb" '::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select '"aa"=> "bb"'::hstore;
+ hstore
+------------
+ "aa"=>"bb"
+(1 row)
+
+select 'aa=>bb, cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>bb , cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>bb ,cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>bb, "cc"=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>bb , "cc"=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>bb ,"cc"=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>"bb", cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>"bb" , cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>"bb" ,cc=>dd'::hstore;
+ hstore
+------------------------
+ "aa"=>"bb", "cc"=>"dd"
+(1 row)
+
+select 'aa=>null'::hstore;
+ hstore
+------------
+ "aa"=>NULL
+(1 row)
+
+select 'aa=>NuLl'::hstore;
+ hstore
+------------
+ "aa"=>NULL
+(1 row)
+
+select 'aa=>"NuLl"'::hstore;
+ hstore
+--------------
+ "aa"=>"NuLl"
+(1 row)
+
+select e'\\=a=>q=w'::hstore;
+ hstore
+-------------
+ "=a"=>"q=w"
+(1 row)
+
+select e'"=a"=>q\\=w'::hstore;
+ hstore
+-------------
+ "=a"=>"q=w"
+(1 row)
+
+select e'"\\"a"=>q>w'::hstore;
+ hstore
+--------------
+ "\"a"=>"q>w"
+(1 row)
+
+select e'\\"a=>q"w'::hstore;
+ hstore
+---------------
+ "\"a"=>"q\"w"
+(1 row)
+
+select ''::hstore;
+ hstore
+--------
+
+(1 row)
+
+select ' '::hstore;
+ hstore
+--------
+
+(1 row)
+
+-- -> operator
+select 'aa=>b, c=>d , b=>16'::hstore->'c';
+ ?column?
+----------
+ d
+(1 row)
+
+select 'aa=>b, c=>d , b=>16'::hstore->'b';
+ ?column?
+----------
+ 16
+(1 row)
+
+select 'aa=>b, c=>d , b=>16'::hstore->'aa';
+ ?column?
+----------
+ b
+(1 row)
+
+select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
+ ?column?
+----------
+ t
+(1 row)
+
+select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
+ ?column?
+----------
+ t
+(1 row)
+
+select ('aa=>"NULL", c=>d , b=>16'::hstore->'aa') is null;
+ ?column?
+----------
+ f
+(1 row)
+
+-- -> array operator
+select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['aa','c'];
+ ?column?
+------------
+ {"NULL",d}
+(1 row)
+
+select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['c','aa'];
+ ?column?
+------------
+ {d,"NULL"}
+(1 row)
+
+select 'aa=>NULL, c=>d , b=>16'::hstore -> ARRAY['aa','c',null];
+ ?column?
+---------------
+ {NULL,d,NULL}
+(1 row)
+
+select 'aa=>1, c=>3, b=>2, d=>4'::hstore -> ARRAY[['b','d'],['aa','c']];
+ ?column?
+---------------
+ {{2,4},{1,3}}
+(1 row)
+
+-- exists/defined
+select exist('a=>NULL, b=>qq', 'a');
+ exist
+-------
+ t
+(1 row)
+
+select exist('a=>NULL, b=>qq', 'b');
+ exist
+-------
+ t
+(1 row)
+
+select exist('a=>NULL, b=>qq', 'c');
+ exist
+-------
+ f
+(1 row)
+
+select exist('a=>"NULL", b=>qq', 'a');
+ exist
+-------
+ t
+(1 row)
+
+select defined('a=>NULL, b=>qq', 'a');
+ defined
+---------
+ f
+(1 row)
+
+select defined('a=>NULL, b=>qq', 'b');
+ defined
+---------
+ t
+(1 row)
+
+select defined('a=>NULL, b=>qq', 'c');
+ defined
+---------
+ f
+(1 row)
+
+select defined('a=>"NULL", b=>qq', 'a');
+ defined
+---------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ? 'a';
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ? 'b';
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ? 'c';
+ ?column?
+----------
+ f
+(1 row)
+
+select hstore 'a=>"NULL", b=>qq' ? 'a';
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['a','b'];
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['b','a'];
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','a'];
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','d'];
+ ?column?
+----------
+ f
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?| '{}'::text[];
+ ?column?
+----------
+ f
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['a','b'];
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['b','a'];
+ ?column?
+----------
+ t
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','a'];
+ ?column?
+----------
+ f
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','d'];
+ ?column?
+----------
+ f
+(1 row)
+
+select hstore 'a=>NULL, b=>qq' ?& '{}'::text[];
+ ?column?
+----------
+ t
+(1 row)
+
+-- delete
+select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
+ delete
+--------------------
+ "b"=>"2", "c"=>"3"
+(1 row)
+
+select delete('a=>null , b=>2, c=>3'::hstore, 'a');
+ delete
+--------------------
+ "b"=>"2", "c"=>"3"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
+ delete
+--------------------
+ "a"=>"1", "c"=>"3"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
+ delete
+--------------------
+ "a"=>"1", "b"=>"2"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
+ delete
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - 'a'::text;
+ ?column?
+--------------------
+ "b"=>"2", "c"=>"3"
+(1 row)
+
+select 'a=>null , b=>2, c=>3'::hstore - 'a'::text;
+ ?column?
+--------------------
+ "b"=>"2", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - 'b'::text;
+ ?column?
+--------------------
+ "a"=>"1", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - 'c'::text;
+ ?column?
+--------------------
+ "a"=>"1", "b"=>"2"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - 'd'::text;
+ ?column?
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b'::text)
+ = pg_column_size('a=>1, b=>2'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- delete (array)
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','e']);
+ delete
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','b']);
+ delete
+--------------------
+ "a"=>"1", "c"=>"3"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['a','c']);
+ delete
+----------
+ "b"=>"2"
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY[['b'],['c'],['a']]);
+ delete
+--------
+
+(1 row)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, '{}'::text[]);
+ delete
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','e'];
+ ?column?
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','b'];
+ ?column?
+--------------------
+ "a"=>"1", "c"=>"3"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'];
+ ?column?
+----------
+ "b"=>"2"
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY[['b'],['c'],['a']];
+ ?column?
+----------
+
+(1 row)
+
+select 'a=>1 , b=>2, c=>3'::hstore - '{}'::text[];
+ ?column?
+------------------------------
+ "a"=>"1", "b"=>"2", "c"=>"3"
+(1 row)
+
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'])
+ = pg_column_size('b=>2'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - '{}'::text[])
+ = pg_column_size('a=>1, b=>2, c=>3'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- delete (hstore)
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>4, b=>2'::hstore);
+ delete
+---------------------
+ "c"=>"3", "aa"=>"1"
+(1 row)
+
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>NULL, c=>3'::hstore);
+ delete
+---------------------
+ "b"=>"2", "aa"=>"1"
+(1 row)
+
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>1, b=>2, c=>3'::hstore);
+ delete
+--------
+
+(1 row)
+
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'b=>2'::hstore);
+ delete
+---------------------
+ "c"=>"3", "aa"=>"1"
+(1 row)
+
+select delete('aa=>1 , b=>2, c=>3'::hstore, ''::hstore);
+ delete
+-------------------------------
+ "b"=>"2", "c"=>"3", "aa"=>"1"
+(1 row)
+
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>4, b=>2'::hstore;
+ ?column?
+---------------------
+ "c"=>"3", "aa"=>"1"
+(1 row)
+
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>NULL, c=>3'::hstore;
+ ?column?
+---------------------
+ "b"=>"2", "aa"=>"1"
+(1 row)
+
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>1, b=>2, c=>3'::hstore;
+ ?column?
+----------
+
+(1 row)
+
+select 'aa=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore;
+ ?column?
+---------------------
+ "c"=>"3", "aa"=>"1"
+(1 row)
+
+select 'aa=>1 , b=>2, c=>3'::hstore - ''::hstore;
+ ?column?
+-------------------------------
+ "b"=>"2", "c"=>"3", "aa"=>"1"
+(1 row)
+
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore)
+ = pg_column_size('a=>1, c=>3'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ''::hstore)
+ = pg_column_size('a=>1, b=>2, c=>3'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- ||
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
+ ?column?
+-------------------------------------------
+ "b"=>"g", "aa"=>"1", "cq"=>"l", "fg"=>"f"
+(1 row)
+
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
+ ?column?
+-------------------------------------------
+ "b"=>"2", "aa"=>"1", "aq"=>"l", "cq"=>"3"
+(1 row)
+
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
+ ?column?
+--------------------------------
+ "b"=>"2", "aa"=>"l", "cq"=>"3"
+(1 row)
+
+select 'aa=>1 , b=>2, cq=>3'::hstore || '';
+ ?column?
+--------------------------------
+ "b"=>"2", "aa"=>"1", "cq"=>"3"
+(1 row)
+
+select ''::hstore || 'cq=>l, b=>g, fg=>f';
+ ?column?
+--------------------------------
+ "b"=>"g", "cq"=>"l", "fg"=>"f"
+(1 row)
+
+select pg_column_size(''::hstore || ''::hstore) = pg_column_size(''::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size('aa=>1'::hstore || 'b=>2'::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size('aa=>1, b=>2'::hstore || ''::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size(''::hstore || 'aa=>1, b=>2'::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- hstore(text,text)
+select 'a=>g, b=>c'::hstore || hstore('asd', 'gf');
+ ?column?
+---------------------------------
+ "a"=>"g", "b"=>"c", "asd"=>"gf"
+(1 row)
+
+select 'a=>g, b=>c'::hstore || hstore('b', 'gf');
+ ?column?
+---------------------
+ "a"=>"g", "b"=>"gf"
+(1 row)
+
+select 'a=>g, b=>c'::hstore || hstore('b', 'NULL');
+ ?column?
+-----------------------
+ "a"=>"g", "b"=>"NULL"
+(1 row)
+
+select 'a=>g, b=>c'::hstore || hstore('b', NULL);
+ ?column?
+---------------------
+ "a"=>"g", "b"=>NULL
+(1 row)
+
+select ('a=>g, b=>c'::hstore || hstore(NULL, 'b')) is null;
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size(hstore('b', 'gf'))
+ = pg_column_size('b=>gf'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size('a=>g, b=>c'::hstore || hstore('b', 'gf'))
+ = pg_column_size('a=>g, b=>gf'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- slice()
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['g','h','i']);
+ slice
+-------
+
+(1 row)
+
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']);
+ slice
+--------------------
+ "b"=>"2", "c"=>"3"
+(1 row)
+
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['aa','b']);
+ slice
+---------------------
+ "b"=>"2", "aa"=>"1"
+(1 row)
+
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']);
+ slice
+-------------------------------
+ "b"=>"2", "c"=>"3", "aa"=>"1"
+(1 row)
+
+select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']))
+ = pg_column_size('b=>2, c=>3'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']))
+ = pg_column_size('aa=>1, b=>2, c=>3'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- array input
+select '{}'::text[]::hstore;
+ hstore
+--------
+
+(1 row)
+
+select ARRAY['a','g','b','h','asd']::hstore;
+ERROR: array must have even number of elements
+select ARRAY['a','g','b','h','asd','i']::hstore;
+ array
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select ARRAY[['a','g'],['b','h'],['asd','i']]::hstore;
+ array
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select ARRAY[['a','g','b'],['h','asd','i']]::hstore;
+ERROR: array must have two columns
+select ARRAY[[['a','g'],['b','h'],['asd','i']]]::hstore;
+ERROR: wrong number of array subscripts
+select hstore('{}'::text[]);
+ hstore
+--------
+
+(1 row)
+
+select hstore(ARRAY['a','g','b','h','asd']);
+ERROR: array must have even number of elements
+select hstore(ARRAY['a','g','b','h','asd','i']);
+ hstore
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select hstore(ARRAY[['a','g'],['b','h'],['asd','i']]);
+ hstore
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select hstore(ARRAY[['a','g','b'],['h','asd','i']]);
+ERROR: array must have two columns
+select hstore(ARRAY[[['a','g'],['b','h'],['asd','i']]]);
+ERROR: wrong number of array subscripts
+select hstore('[0:5]={a,g,b,h,asd,i}'::text[]);
+ hstore
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select hstore('[0:2][1:2]={{a,g},{b,h},{asd,i}}'::text[]);
+ hstore
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+-- pairs of arrays
+select hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']);
+ hstore
+--------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>"i"
+(1 row)
+
+select hstore(ARRAY['a','b','asd'], ARRAY['g','h',NULL]);
+ hstore
+---------------------------------
+ "a"=>"g", "b"=>"h", "asd"=>NULL
+(1 row)
+
+select hstore(ARRAY['z','y','x'], ARRAY['1','2','3']);
+ hstore
+------------------------------
+ "x"=>"3", "y"=>"2", "z"=>"1"
+(1 row)
+
+select hstore(ARRAY['aaa','bb','c','d'], ARRAY[null::text,null,null,null]);
+ hstore
+-----------------------------------------------
+ "c"=>NULL, "d"=>NULL, "bb"=>NULL, "aaa"=>NULL
+(1 row)
+
+select hstore(ARRAY['aaa','bb','c','d'], null);
+ hstore
+-----------------------------------------------
+ "c"=>NULL, "d"=>NULL, "bb"=>NULL, "aaa"=>NULL
+(1 row)
+
+select quote_literal(hstore('{}'::text[], '{}'::text[]));
+ quote_literal
+---------------
+ ''
+(1 row)
+
+select quote_literal(hstore('{}'::text[], null));
+ quote_literal
+---------------
+ ''
+(1 row)
+
+select hstore(ARRAY['a'], '{}'::text[]); -- error
+ERROR: arrays must have same bounds
+select hstore('{}'::text[], ARRAY['a']); -- error
+ERROR: arrays must have same bounds
+select pg_column_size(hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']))
+ = pg_column_size('a=>g, b=>h, asd=>i'::hstore);
+ ?column?
+----------
+ t
+(1 row)
+
+-- records
+select hstore(v) from (values (1, 'foo', 1.2, 3::float8)) v(a,b,c,d);
+ hstore
+--------------------------------------------
+ "a"=>"1", "b"=>"foo", "c"=>"1.2", "d"=>"3"
+(1 row)
+
+create domain hstestdom1 as integer not null default 0;
+create table testhstore0 (a integer, b text, c numeric, d float8);
+create table testhstore1 (a integer, b text, c numeric, d float8, e hstestdom1);
+insert into testhstore0 values (1, 'foo', 1.2, 3::float8);
+insert into testhstore1 values (1, 'foo', 1.2, 3::float8);
+select hstore(v) from testhstore1 v;
+ hstore
+------------------------------------------------------
+ "a"=>"1", "b"=>"foo", "c"=>"1.2", "d"=>"3", "e"=>"0"
+(1 row)
+
+select hstore(null::testhstore0);
+ hstore
+--------------------------------------------
+ "a"=>NULL, "b"=>NULL, "c"=>NULL, "d"=>NULL
+(1 row)
+
+select hstore(null::testhstore1);
+ hstore
+-------------------------------------------------------
+ "a"=>NULL, "b"=>NULL, "c"=>NULL, "d"=>NULL, "e"=>NULL
+(1 row)
+
+select pg_column_size(hstore(v))
+ = pg_column_size('a=>1, b=>"foo", c=>"1.2", d=>"3", e=>"0"'::hstore)
+ from testhstore1 v;
+ ?column?
+----------
+ t
+(1 row)
+
+select populate_record(v, hstore('c', '3.45')) from testhstore1 v;
+ populate_record
+------------------
+ (1,foo,3.45,3,0)
+(1 row)
+
+select populate_record(v, hstore('d', '3.45')) from testhstore1 v;
+ populate_record
+--------------------
+ (1,foo,1.2,3.45,0)
+(1 row)
+
+select populate_record(v, hstore('e', '123')) from testhstore1 v;
+ populate_record
+-------------------
+ (1,foo,1.2,3,123)
+(1 row)
+
+select populate_record(v, hstore('e', null)) from testhstore1 v;
+ERROR: domain hstestdom1 does not allow null values
+select populate_record(v, hstore('c', null)) from testhstore1 v;
+ populate_record
+-----------------
+ (1,foo,,3,0)
+(1 row)
+
+select populate_record(v, hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
+ populate_record
+-------------------
+ (123,foo,1.2,3,0)
+(1 row)
+
+select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore0 v;
+ populate_record
+-----------------
+ (1,foo,1.2,3)
+(1 row)
+
+select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore1 v;
+ERROR: domain hstestdom1 does not allow null values
+select populate_record(v, '') from testhstore0 v;
+ populate_record
+-----------------
+ (1,foo,1.2,3)
+(1 row)
+
+select populate_record(v, '') from testhstore1 v;
+ populate_record
+-----------------
+ (1,foo,1.2,3,0)
+(1 row)
+
+select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('a', '123'));
+ERROR: domain hstestdom1 does not allow null values
+select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('e', '123'));
+ populate_record
+-----------------
+ (,,3.45,,123)
+(1 row)
+
+select populate_record(null::testhstore0, '');
+ populate_record
+-----------------
+ (,,,)
+(1 row)
+
+select populate_record(null::testhstore1, '');
+ERROR: domain hstestdom1 does not allow null values
+select v #= hstore('c', '3.45') from testhstore1 v;
+ ?column?
+------------------
+ (1,foo,3.45,3,0)
+(1 row)
+
+select v #= hstore('d', '3.45') from testhstore1 v;
+ ?column?
+--------------------
+ (1,foo,1.2,3.45,0)
+(1 row)
+
+select v #= hstore('e', '123') from testhstore1 v;
+ ?column?
+-------------------
+ (1,foo,1.2,3,123)
+(1 row)
+
+select v #= hstore('c', null) from testhstore1 v;
+ ?column?
+--------------
+ (1,foo,,3,0)
+(1 row)
+
+select v #= hstore('e', null) from testhstore0 v;
+ ?column?
+---------------
+ (1,foo,1.2,3)
+(1 row)
+
+select v #= hstore('e', null) from testhstore1 v;
+ERROR: domain hstestdom1 does not allow null values
+select v #= (hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
+ ?column?
+-------------------
+ (123,foo,1.2,3,0)
+(1 row)
+
+select v #= (hstore('b', 'foo') || hstore('e', '123')) from testhstore1 v;
+ ?column?
+-------------------
+ (1,foo,1.2,3,123)
+(1 row)
+
+select v #= hstore '' from testhstore0 v;
+ ?column?
+---------------
+ (1,foo,1.2,3)
+(1 row)
+
+select v #= hstore '' from testhstore1 v;
+ ?column?
+-----------------
+ (1,foo,1.2,3,0)
+(1 row)
+
+select null::testhstore1 #= (hstore('c', '3.45') || hstore('a', '123'));
+ERROR: domain hstestdom1 does not allow null values
+select null::testhstore1 #= (hstore('c', '3.45') || hstore('e', '123'));
+ ?column?
+---------------
+ (,,3.45,,123)
+(1 row)
+
+select null::testhstore0 #= hstore '';
+ ?column?
+----------
+ (,,,)
+(1 row)
+
+select null::testhstore1 #= hstore '';
+ERROR: domain hstestdom1 does not allow null values
+select v #= h from testhstore1 v, (values (hstore 'a=>123',1),('b=>foo,c=>3.21',2),('a=>null',3),('e=>123',4),('f=>blah',5)) x(h,i) order by i;
+ ?column?
+-------------------
+ (123,foo,1.2,3,0)
+ (1,foo,3.21,3,0)
+ (,foo,1.2,3,0)
+ (1,foo,1.2,3,123)
+ (1,foo,1.2,3,0)
+(5 rows)
+
+-- keys/values
+select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+ akeys
+--------------
+ {b,aa,cq,fg}
+(1 row)
+
+select akeys('""=>1');
+ akeys
+-------
+ {""}
+(1 row)
+
+select akeys('');
+ akeys
+-------
+ {}
+(1 row)
+
+select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+ avals
+-----------
+ {g,1,l,f}
+(1 row)
+
+select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
+ avals
+--------------
+ {g,1,l,NULL}
+(1 row)
+
+select avals('""=>1');
+ avals
+-------
+ {1}
+(1 row)
+
+select avals('');
+ avals
+-------
+ {}
+(1 row)
+
+select hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
+ hstore_to_array
+-------------------------
+ {b,g,aa,1,cq,l,fg,NULL}
+(1 row)
+
+select %% 'aa=>1, cq=>l, b=>g, fg=>NULL';
+ ?column?
+-------------------------
+ {b,g,aa,1,cq,l,fg,NULL}
+(1 row)
+
+select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
+ hstore_to_matrix
+---------------------------------
+ {{b,g},{aa,1},{cq,l},{fg,NULL}}
+(1 row)
+
+select %# 'aa=>1, cq=>l, b=>g, fg=>NULL';
+ ?column?
+---------------------------------
+ {{b,g},{aa,1},{cq,l},{fg,NULL}}
+(1 row)
+
+select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+ skeys
+-------
+ b
+ aa
+ cq
+ fg
+(4 rows)
+
+select * from skeys('""=>1');
+ skeys
+-------
+
+(1 row)
+
+select * from skeys('');
+ skeys
+-------
+(0 rows)
+
+select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+ svals
+-------
+ g
+ 1
+ l
+ f
+(4 rows)
+
+select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
+ svals | ?column?
+-------+----------
+ g | f
+ 1 | f
+ l | f
+ | t
+(4 rows)
+
+select * from svals('""=>1');
+ svals
+-------
+ 1
+(1 row)
+
+select * from svals('');
+ svals
+-------
+(0 rows)
+
+select * from each('aaa=>bq, b=>NULL, ""=>1 ');
+ key | value
+-----+-------
+ | 1
+ b |
+ aaa | bq
+(3 rows)
+
+-- @>
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
+ ?column?
+----------
+ t
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>NULL';
+ ?column?
+----------
+ t
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, g=>NULL';
+ ?column?
+----------
+ f
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'g=>NULL';
+ ?column?
+----------
+ f
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
+ ?column?
+----------
+ f
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
+ ?column?
+----------
+ t
+(1 row)
+
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>q';
+ ?column?
+----------
+ f
+(1 row)
+
+CREATE TABLE testhstore (h hstore);
+\copy testhstore from 'data/hstore.data'
+select count(*) from testhstore where h @> 'wait=>NULL';
+ count
+-------
+ 1
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC';
+ count
+-------
+ 15
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+ count
+-------
+ 2
+(1 row)
+
+select count(*) from testhstore where h ? 'public';
+ count
+-------
+ 194
+(1 row)
+
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+ count
+-------
+ 337
+(1 row)
+
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+ count
+-------
+ 42
+(1 row)
+
+create index hidx on testhstore using gist(h);
+set enable_seqscan=off;
+select count(*) from testhstore where h @> 'wait=>NULL';
+ count
+-------
+ 1
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC';
+ count
+-------
+ 15
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+ count
+-------
+ 2
+(1 row)
+
+select count(*) from testhstore where h ? 'public';
+ count
+-------
+ 194
+(1 row)
+
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+ count
+-------
+ 337
+(1 row)
+
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+ count
+-------
+ 42
+(1 row)
+
+drop index hidx;
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=0));
+ERROR: value 0 out of bounds for option "siglen"
+DETAIL: Valid values are between "1" and "2024".
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=2025));
+ERROR: value 2025 out of bounds for option "siglen"
+DETAIL: Valid values are between "1" and "2024".
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=2024));
+set enable_seqscan=off;
+select count(*) from testhstore where h @> 'wait=>NULL';
+ count
+-------
+ 1
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC';
+ count
+-------
+ 15
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+ count
+-------
+ 2
+(1 row)
+
+select count(*) from testhstore where h ? 'public';
+ count
+-------
+ 194
+(1 row)
+
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+ count
+-------
+ 337
+(1 row)
+
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+ count
+-------
+ 42
+(1 row)
+
+drop index hidx;
+create index hidx on testhstore using gin (h);
+set enable_seqscan=off;
+select count(*) from testhstore where h @> 'wait=>NULL';
+ count
+-------
+ 1
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC';
+ count
+-------
+ 15
+(1 row)
+
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+ count
+-------
+ 2
+(1 row)
+
+select count(*) from testhstore where h ? 'public';
+ count
+-------
+ 194
+(1 row)
+
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+ count
+-------
+ 337
+(1 row)
+
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+ count
+-------
+ 42
+(1 row)
+
+select count(*) from (select (each(h)).key from testhstore) as wow ;
+ count
+-------
+ 4781
+(1 row)
+
+select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
+ key | count
+-----------+-------
+ line | 884
+ query | 207
+ pos | 203
+ node | 202
+ space | 197
+ status | 195
+ public | 194
+ title | 190
+ wait | 190
+ org | 189
+ user | 189
+ coauthors | 188
+ disabled | 185
+ indexed | 184
+ cleaned | 180
+ bad | 179
+ date | 179
+ world | 176
+ state | 172
+ subtitle | 169
+ auth | 168
+ abstract | 161
+(22 rows)
+
+-- sort/hash
+select count(distinct h) from testhstore;
+ count
+-------
+ 885
+(1 row)
+
+set enable_hashagg = false;
+select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
+ count
+-------
+ 885
+(1 row)
+
+set enable_hashagg = true;
+set enable_sort = false;
+select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
+ count
+-------
+ 885
+(1 row)
+
+select distinct * from (values (hstore '' || ''),('')) v(h);
+ h
+---
+
+(1 row)
+
+set enable_sort = true;
+-- btree
+drop index hidx;
+create index hidx on testhstore using btree (h);
+set enable_seqscan=off;
+select count(*) from testhstore where h #># 'p=>1';
+ count
+-------
+ 125
+(1 row)
+
+select count(*) from testhstore where h = 'pos=>98, line=>371, node=>CBA, indexed=>t';
+ count
+-------
+ 1
+(1 row)
+
+-- json and jsonb
+select hstore_to_json('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
+ hstore_to_json
+-------------------------------------------------------------------------------------------------
+ {"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
+(1 row)
+
+select cast( hstore '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as json);
+ json
+-------------------------------------------------------------------------------------------------
+ {"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
+(1 row)
+
+select hstore_to_json_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
+ hstore_to_json_loose
+-------------------------------------------------------------------------------------------------------------
+ {"b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4, "h": "2016-01-01", "a key": 1}
+(1 row)
+
+select hstore_to_jsonb('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
+ hstore_to_jsonb
+-------------------------------------------------------------------------------------------------
+ {"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
+(1 row)
+
+select cast( hstore '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as jsonb);
+ jsonb
+-------------------------------------------------------------------------------------------------
+ {"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}
+(1 row)
+
+select hstore_to_jsonb_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
+ hstore_to_jsonb_loose
+----------------------------------------------------------------------------------------------------------
+ {"b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 23450, "h": "2016-01-01", "a key": 1}
+(1 row)
+
+create table test_json_agg (f1 text, f2 hstore);
+insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
+ ('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
+select json_agg(q) from test_json_agg q;
+ json_agg
+----------------------------------------------------------------------------------------------------------------------------
+ [{"f1":"rec1","f2":{"b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4", "a key": "1"}}, +
+ {"f1":"rec2","f2":{"b": "f", "c": "null", "d": "-12345", "e": "012345.6", "f": "-1.234", "g": "0.345e-4", "a key": "2"}}]
+(1 row)
+
+select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
+ json_agg
+----------------------------------------------------------------------------------------------------------------------
+ [{"f1":"rec1","f2":{"b": true, "c": null, "d": 12345, "e": "012345", "f": 1.234, "g": 2.345e+4, "a key": 1}}, +
+ {"f1":"rec2","f2":{"b": false, "c": "null", "d": -12345, "e": "012345.6", "f": -1.234, "g": 0.345e-4, "a key": 2}}]
+(1 row)
+
+-- Test subscripting
+insert into test_json_agg default values;
+select f2['d'], f2['x'] is null as x_isnull from test_json_agg;
+ f2 | x_isnull
+--------+----------
+ 12345 | t
+ -12345 | t
+ | t
+(3 rows)
+
+select f2['d']['e'] from test_json_agg; -- error
+ERROR: hstore allows only one subscript
+select f2['d':'e'] from test_json_agg; -- error
+ERROR: hstore allows only one subscript
+update test_json_agg set f2['d'] = f2['e'], f2['x'] = 'xyzzy';
+select f2 from test_json_agg;
+ f2
+---------------------------------------------------------------------------------------------------------------------
+ "b"=>"t", "c"=>NULL, "d"=>"012345", "e"=>"012345", "f"=>"1.234", "g"=>"2.345e+4", "x"=>"xyzzy", "a key"=>"1"
+ "b"=>"f", "c"=>"null", "d"=>"012345.6", "e"=>"012345.6", "f"=>"-1.234", "g"=>"0.345e-4", "x"=>"xyzzy", "a key"=>"2"
+ "d"=>NULL, "x"=>"xyzzy"
+(3 rows)
+
+-- Test subscripting in plpgsql
+do $$ declare h hstore;
+begin h['a'] := 'b'; raise notice 'h = %, h[a] = %', h, h['a']; end $$;
+NOTICE: h = "a"=>"b", h[a] = b
+-- Check the hstore_hash() and hstore_hash_extended() function explicitly.
+SELECT v as value, hstore_hash(v)::bit(32) as standard,
+ hstore_hash_extended(v, 0)::bit(32) as extended0,
+ hstore_hash_extended(v, 1)::bit(32) as extended1
+FROM (VALUES (NULL::hstore), (''), ('"a key" =>1'), ('c => null'),
+ ('e => 012345'), ('g => 2.345e+4')) x(v)
+WHERE hstore_hash(v)::bit(32) != hstore_hash_extended(v, 0)::bit(32)
+ OR hstore_hash(v)::bit(32) = hstore_hash_extended(v, 1)::bit(32);
+ value | standard | extended0 | extended1
+-------+----------+-----------+-----------
+(0 rows)
+
diff --git a/contrib/hstore/hstore--1.1--1.2.sql b/contrib/hstore/hstore--1.1--1.2.sql
new file mode 100644
index 0000000..cc69fc7
--- /dev/null
+++ b/contrib/hstore/hstore--1.1--1.2.sql
@@ -0,0 +1,53 @@
+/* contrib/hstore/hstore--1.1--1.2.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.2'" to load this file. \quit
+
+
+-- A version of 1.1 was shipped with these objects mistakenly in 9.3.0.
+-- Therefore we only add them if we detect that they aren't already there and
+-- dependent on the extension.
+
+DO LANGUAGE plpgsql
+$$
+DECLARE
+ my_schema pg_catalog.text := pg_catalog.quote_ident(pg_catalog.current_schema());
+ old_path pg_catalog.text := pg_catalog.current_setting('search_path');
+BEGIN
+-- for safety, transiently set search_path to just pg_catalog+pg_temp
+PERFORM pg_catalog.set_config('search_path', 'pg_catalog, pg_temp', true);
+
+ PERFORM 1
+ FROM pg_proc p
+ JOIN pg_depend d
+ ON p.proname = 'hstore_to_json_loose'
+ AND d.classid = 'pg_proc'::regclass
+ AND d.objid = p.oid
+ AND d.refclassid = 'pg_extension'::regclass
+ JOIN pg_extension x
+ ON d.refobjid = x.oid
+ AND x.extname = 'hstore';
+
+ IF NOT FOUND
+ THEN
+ PERFORM pg_catalog.set_config('search_path', old_path, true);
+
+ CREATE FUNCTION hstore_to_json(hstore)
+ RETURNS json
+ AS 'MODULE_PATHNAME', 'hstore_to_json'
+ LANGUAGE C IMMUTABLE STRICT;
+
+ CREATE CAST (hstore AS json)
+ WITH FUNCTION hstore_to_json(hstore);
+
+ CREATE FUNCTION hstore_to_json_loose(hstore)
+ RETURNS json
+ AS 'MODULE_PATHNAME', 'hstore_to_json_loose'
+ LANGUAGE C IMMUTABLE STRICT;
+
+ END IF;
+
+PERFORM pg_catalog.set_config('search_path', old_path, true);
+END;
+
+$$;
diff --git a/contrib/hstore/hstore--1.2--1.3.sql b/contrib/hstore/hstore--1.2--1.3.sql
new file mode 100644
index 0000000..0a70560
--- /dev/null
+++ b/contrib/hstore/hstore--1.2--1.3.sql
@@ -0,0 +1,17 @@
+/* contrib/hstore/hstore--1.2--1.3.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.3'" to load this file. \quit
+
+CREATE FUNCTION hstore_to_jsonb(hstore)
+RETURNS jsonb
+AS 'MODULE_PATHNAME', 'hstore_to_jsonb'
+LANGUAGE C IMMUTABLE STRICT;
+
+CREATE CAST (hstore AS jsonb)
+ WITH FUNCTION hstore_to_jsonb(hstore);
+
+CREATE FUNCTION hstore_to_jsonb_loose(hstore)
+RETURNS jsonb
+AS 'MODULE_PATHNAME', 'hstore_to_jsonb_loose'
+LANGUAGE C IMMUTABLE STRICT;
diff --git a/contrib/hstore/hstore--1.3--1.4.sql b/contrib/hstore/hstore--1.3--1.4.sql
new file mode 100644
index 0000000..53f26f9
--- /dev/null
+++ b/contrib/hstore/hstore--1.3--1.4.sql
@@ -0,0 +1,99 @@
+/* contrib/hstore/hstore--1.3--1.4.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.4'" to load this file. \quit
+
+-- Update procedure signatures the hard way.
+-- We use to_regprocedure() so that query doesn't fail if run against 9.6beta1 definitions,
+-- wherein the signatures have been updated already. In that case to_regprocedure() will
+-- return NULL and no updates will happen.
+DO LANGUAGE plpgsql
+$$
+DECLARE
+ my_schema pg_catalog.text := pg_catalog.quote_ident(pg_catalog.current_schema());
+ old_path pg_catalog.text := pg_catalog.current_setting('search_path');
+BEGIN
+-- for safety, transiently set search_path to just pg_catalog+pg_temp
+PERFORM pg_catalog.set_config('search_path', 'pg_catalog, pg_temp', true);
+
+UPDATE pg_catalog.pg_proc SET
+ proargtypes = pg_catalog.array_to_string(newtypes::pg_catalog.oid[], ' ')::pg_catalog.oidvector,
+ pronargs = pg_catalog.array_length(newtypes, 1)
+FROM (VALUES
+(NULL::pg_catalog.text, NULL::pg_catalog.text[]), -- establish column types
+('ghstore_same(internal,internal,internal)', '{SCH.ghstore,SCH.ghstore,internal}'),
+('ghstore_consistent(internal,internal,int4,oid,internal)', '{internal,SCH.hstore,int2,oid,internal}'),
+('gin_extract_hstore(internal,internal)', '{SCH.hstore,internal}'),
+('gin_extract_hstore_query(internal,internal,int2,internal,internal)', '{SCH.hstore,internal,int2,internal,internal}'),
+('gin_consistent_hstore(internal,int2,internal,int4,internal,internal)', '{internal,int2,SCH.hstore,int4,internal,internal}')
+) AS update_data (oldproc, newtypestext),
+LATERAL (
+ SELECT array_agg(replace(typ, 'SCH', my_schema)::regtype) as newtypes FROM unnest(newtypestext) typ
+) ls
+WHERE oid = to_regprocedure(my_schema || '.' || replace(oldproc, 'SCH', my_schema));
+
+UPDATE pg_catalog.pg_proc SET
+ prorettype = (my_schema || '.ghstore')::pg_catalog.regtype
+WHERE oid = pg_catalog.to_regprocedure((my_schema || '.ghstore_union(internal,internal)'));
+
+PERFORM pg_catalog.set_config('search_path', old_path, true);
+END
+$$;
+
+ALTER FUNCTION hstore_in(cstring) PARALLEL SAFE;
+ALTER FUNCTION hstore_out(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_recv(internal) PARALLEL SAFE;
+ALTER FUNCTION hstore_send(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_version_diag(hstore) PARALLEL SAFE;
+ALTER FUNCTION fetchval(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION slice_array(hstore, text[]) PARALLEL SAFE;
+ALTER FUNCTION slice(hstore, text[]) PARALLEL SAFE;
+ALTER FUNCTION isexists(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION exist(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION exists_any(hstore, text[]) PARALLEL SAFE;
+ALTER FUNCTION exists_all(hstore, text[]) PARALLEL SAFE;
+ALTER FUNCTION isdefined(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION defined(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION delete(hstore, text) PARALLEL SAFE;
+ALTER FUNCTION delete(hstore, text[]) PARALLEL SAFE;
+ALTER FUNCTION delete(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hs_concat(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hs_contains(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hs_contained(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION tconvert(text, text) PARALLEL SAFE;
+ALTER FUNCTION hstore(text, text) PARALLEL SAFE;
+ALTER FUNCTION hstore(text[], text[]) PARALLEL SAFE;
+ALTER FUNCTION hstore(text[]) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_json(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_json_loose(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_jsonb(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_jsonb_loose(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore(record) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_array(hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_to_matrix(hstore) PARALLEL SAFE;
+ALTER FUNCTION akeys(hstore) PARALLEL SAFE;
+ALTER FUNCTION avals(hstore) PARALLEL SAFE;
+ALTER FUNCTION skeys(hstore) PARALLEL SAFE;
+ALTER FUNCTION svals(hstore) PARALLEL SAFE;
+ALTER FUNCTION each(hstore) PARALLEL SAFE;
+ALTER FUNCTION populate_record(anyelement, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_eq(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_ne(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_gt(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_ge(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_lt(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_le(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_cmp(hstore, hstore) PARALLEL SAFE;
+ALTER FUNCTION hstore_hash(hstore) PARALLEL SAFE;
+ALTER FUNCTION ghstore_in(cstring) PARALLEL SAFE;
+ALTER FUNCTION ghstore_out(ghstore) PARALLEL SAFE;
+ALTER FUNCTION ghstore_compress(internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_decompress(internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_penalty(internal, internal, internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_picksplit(internal, internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_union(internal, internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_same(ghstore, ghstore, internal) PARALLEL SAFE;
+ALTER FUNCTION ghstore_consistent(internal, hstore, smallint, oid, internal) PARALLEL SAFE;
+ALTER FUNCTION gin_extract_hstore(hstore, internal) PARALLEL SAFE;
+ALTER FUNCTION gin_extract_hstore_query(hstore, internal, int2, internal, internal) PARALLEL SAFE;
+ALTER FUNCTION gin_consistent_hstore(internal, int2, hstore, int4, internal, internal) PARALLEL SAFE;
diff --git a/contrib/hstore/hstore--1.4--1.5.sql b/contrib/hstore/hstore--1.4--1.5.sql
new file mode 100644
index 0000000..92c1832
--- /dev/null
+++ b/contrib/hstore/hstore--1.4--1.5.sql
@@ -0,0 +1,14 @@
+/* contrib/hstore/hstore--1.4--1.5.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.5'" to load this file. \quit
+
+ALTER OPERATOR #<=# (hstore, hstore) SET (
+ RESTRICT = scalarlesel,
+ JOIN = scalarlejoinsel
+);
+
+ALTER OPERATOR #>=# (hstore, hstore) SET (
+ RESTRICT = scalargesel,
+ JOIN = scalargejoinsel
+);
diff --git a/contrib/hstore/hstore--1.4.sql b/contrib/hstore/hstore--1.4.sql
new file mode 100644
index 0000000..4294d14
--- /dev/null
+++ b/contrib/hstore/hstore--1.4.sql
@@ -0,0 +1,550 @@
+/* contrib/hstore/hstore--1.4.sql */
+
+-- complain if script is sourced in psql, rather than via CREATE EXTENSION
+\echo Use "CREATE EXTENSION hstore" to load this file. \quit
+
+CREATE TYPE hstore;
+
+CREATE FUNCTION hstore_in(cstring)
+RETURNS hstore
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_out(hstore)
+RETURNS cstring
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_recv(internal)
+RETURNS hstore
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_send(hstore)
+RETURNS bytea
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE TYPE hstore (
+ INTERNALLENGTH = -1,
+ INPUT = hstore_in,
+ OUTPUT = hstore_out,
+ RECEIVE = hstore_recv,
+ SEND = hstore_send,
+ STORAGE = extended
+);
+
+CREATE FUNCTION hstore_version_diag(hstore)
+RETURNS integer
+AS 'MODULE_PATHNAME','hstore_version_diag'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION fetchval(hstore,text)
+RETURNS text
+AS 'MODULE_PATHNAME','hstore_fetchval'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR -> (
+ LEFTARG = hstore,
+ RIGHTARG = text,
+ PROCEDURE = fetchval
+);
+
+CREATE FUNCTION slice_array(hstore,text[])
+RETURNS text[]
+AS 'MODULE_PATHNAME','hstore_slice_to_array'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR -> (
+ LEFTARG = hstore,
+ RIGHTARG = text[],
+ PROCEDURE = slice_array
+);
+
+CREATE FUNCTION slice(hstore,text[])
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_slice_to_hstore'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION isexists(hstore,text)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_exists'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION exist(hstore,text)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_exists'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR ? (
+ LEFTARG = hstore,
+ RIGHTARG = text,
+ PROCEDURE = exist,
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE FUNCTION exists_any(hstore,text[])
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_exists_any'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR ?| (
+ LEFTARG = hstore,
+ RIGHTARG = text[],
+ PROCEDURE = exists_any,
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE FUNCTION exists_all(hstore,text[])
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_exists_all'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR ?& (
+ LEFTARG = hstore,
+ RIGHTARG = text[],
+ PROCEDURE = exists_all,
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE FUNCTION isdefined(hstore,text)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_defined'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION defined(hstore,text)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_defined'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION delete(hstore,text)
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_delete'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION delete(hstore,text[])
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_delete_array'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION delete(hstore,hstore)
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_delete_hstore'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR - (
+ LEFTARG = hstore,
+ RIGHTARG = text,
+ PROCEDURE = delete
+);
+
+CREATE OPERATOR - (
+ LEFTARG = hstore,
+ RIGHTARG = text[],
+ PROCEDURE = delete
+);
+
+CREATE OPERATOR - (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = delete
+);
+
+CREATE FUNCTION hs_concat(hstore,hstore)
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_concat'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR || (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hs_concat
+);
+
+CREATE FUNCTION hs_contains(hstore,hstore)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_contains'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hs_contained(hstore,hstore)
+RETURNS bool
+AS 'MODULE_PATHNAME','hstore_contained'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR @> (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hs_contains,
+ COMMUTATOR = '<@',
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE OPERATOR <@ (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hs_contained,
+ COMMUTATOR = '@>',
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+-- obsolete:
+CREATE OPERATOR @ (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hs_contains,
+ COMMUTATOR = '~',
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE OPERATOR ~ (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hs_contained,
+ COMMUTATOR = '@',
+ RESTRICT = contsel,
+ JOIN = contjoinsel
+);
+
+CREATE FUNCTION tconvert(text,text)
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_from_text'
+LANGUAGE C IMMUTABLE PARALLEL SAFE; -- not STRICT; needs to allow (key,NULL)
+
+CREATE FUNCTION hstore(text,text)
+RETURNS hstore
+AS 'MODULE_PATHNAME','hstore_from_text'
+LANGUAGE C IMMUTABLE PARALLEL SAFE; -- not STRICT; needs to allow (key,NULL)
+
+CREATE FUNCTION hstore(text[],text[])
+RETURNS hstore
+AS 'MODULE_PATHNAME', 'hstore_from_arrays'
+LANGUAGE C IMMUTABLE PARALLEL SAFE; -- not STRICT; allows (keys,null)
+
+CREATE FUNCTION hstore(text[])
+RETURNS hstore
+AS 'MODULE_PATHNAME', 'hstore_from_array'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE CAST (text[] AS hstore)
+ WITH FUNCTION hstore(text[]);
+
+CREATE FUNCTION hstore_to_json(hstore)
+RETURNS json
+AS 'MODULE_PATHNAME', 'hstore_to_json'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE CAST (hstore AS json)
+ WITH FUNCTION hstore_to_json(hstore);
+
+CREATE FUNCTION hstore_to_json_loose(hstore)
+RETURNS json
+AS 'MODULE_PATHNAME', 'hstore_to_json_loose'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION hstore_to_jsonb(hstore)
+RETURNS jsonb
+AS 'MODULE_PATHNAME', 'hstore_to_jsonb'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE CAST (hstore AS jsonb)
+ WITH FUNCTION hstore_to_jsonb(hstore);
+
+CREATE FUNCTION hstore_to_jsonb_loose(hstore)
+RETURNS jsonb
+AS 'MODULE_PATHNAME', 'hstore_to_jsonb_loose'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION hstore(record)
+RETURNS hstore
+AS 'MODULE_PATHNAME', 'hstore_from_record'
+LANGUAGE C IMMUTABLE PARALLEL SAFE; -- not STRICT; allows (null::recordtype)
+
+CREATE FUNCTION hstore_to_array(hstore)
+RETURNS text[]
+AS 'MODULE_PATHNAME','hstore_to_array'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR %% (
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_to_array
+);
+
+CREATE FUNCTION hstore_to_matrix(hstore)
+RETURNS text[]
+AS 'MODULE_PATHNAME','hstore_to_matrix'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR %# (
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_to_matrix
+);
+
+CREATE FUNCTION akeys(hstore)
+RETURNS text[]
+AS 'MODULE_PATHNAME','hstore_akeys'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION avals(hstore)
+RETURNS text[]
+AS 'MODULE_PATHNAME','hstore_avals'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION skeys(hstore)
+RETURNS setof text
+AS 'MODULE_PATHNAME','hstore_skeys'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION svals(hstore)
+RETURNS setof text
+AS 'MODULE_PATHNAME','hstore_svals'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION each(IN hs hstore,
+ OUT key text,
+ OUT value text)
+RETURNS SETOF record
+AS 'MODULE_PATHNAME','hstore_each'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION populate_record(anyelement,hstore)
+RETURNS anyelement
+AS 'MODULE_PATHNAME', 'hstore_populate_record'
+LANGUAGE C IMMUTABLE PARALLEL SAFE; -- not STRICT; allows (null::rectype,hstore)
+
+CREATE OPERATOR #= (
+ LEFTARG = anyelement,
+ RIGHTARG = hstore,
+ PROCEDURE = populate_record
+);
+
+-- btree support
+
+CREATE FUNCTION hstore_eq(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_eq'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_ne(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_ne'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_gt(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_gt'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_ge(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_ge'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_lt(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_lt'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_le(hstore,hstore)
+RETURNS boolean
+AS 'MODULE_PATHNAME','hstore_le'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION hstore_cmp(hstore,hstore)
+RETURNS integer
+AS 'MODULE_PATHNAME','hstore_cmp'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR = (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_eq,
+ COMMUTATOR = =,
+ NEGATOR = <>,
+ RESTRICT = eqsel,
+ JOIN = eqjoinsel,
+ MERGES,
+ HASHES
+);
+CREATE OPERATOR <> (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_ne,
+ COMMUTATOR = <>,
+ NEGATOR = =,
+ RESTRICT = neqsel,
+ JOIN = neqjoinsel
+);
+
+-- the comparison operators have funky names (and are undocumented)
+-- in an attempt to discourage anyone from actually using them. they
+-- only exist to support the btree opclass
+
+CREATE OPERATOR #<# (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_lt,
+ COMMUTATOR = #>#,
+ NEGATOR = #>=#,
+ RESTRICT = scalarltsel,
+ JOIN = scalarltjoinsel
+);
+CREATE OPERATOR #<=# (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_le,
+ COMMUTATOR = #>=#,
+ NEGATOR = #>#,
+ RESTRICT = scalarltsel,
+ JOIN = scalarltjoinsel
+);
+CREATE OPERATOR #># (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_gt,
+ COMMUTATOR = #<#,
+ NEGATOR = #<=#,
+ RESTRICT = scalargtsel,
+ JOIN = scalargtjoinsel
+);
+CREATE OPERATOR #>=# (
+ LEFTARG = hstore,
+ RIGHTARG = hstore,
+ PROCEDURE = hstore_ge,
+ COMMUTATOR = #<=#,
+ NEGATOR = #<#,
+ RESTRICT = scalargtsel,
+ JOIN = scalargtjoinsel
+);
+
+CREATE OPERATOR CLASS btree_hstore_ops
+DEFAULT FOR TYPE hstore USING btree
+AS
+ OPERATOR 1 #<# ,
+ OPERATOR 2 #<=# ,
+ OPERATOR 3 = ,
+ OPERATOR 4 #>=# ,
+ OPERATOR 5 #># ,
+ FUNCTION 1 hstore_cmp(hstore,hstore);
+
+-- hash support
+
+CREATE FUNCTION hstore_hash(hstore)
+RETURNS integer
+AS 'MODULE_PATHNAME','hstore_hash'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE OPERATOR CLASS hash_hstore_ops
+DEFAULT FOR TYPE hstore USING hash
+AS
+ OPERATOR 1 = ,
+ FUNCTION 1 hstore_hash(hstore);
+
+-- GiST support
+
+CREATE TYPE ghstore;
+
+CREATE FUNCTION ghstore_in(cstring)
+RETURNS ghstore
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_out(ghstore)
+RETURNS cstring
+AS 'MODULE_PATHNAME'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+CREATE TYPE ghstore (
+ INTERNALLENGTH = -1,
+ INPUT = ghstore_in,
+ OUTPUT = ghstore_out
+);
+
+CREATE FUNCTION ghstore_compress(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_decompress(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_penalty(internal,internal,internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_picksplit(internal, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_union(internal, internal)
+RETURNS ghstore
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_same(ghstore, ghstore, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION ghstore_consistent(internal,hstore,smallint,oid,internal)
+RETURNS bool
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE OPERATOR CLASS gist_hstore_ops
+DEFAULT FOR TYPE hstore USING gist
+AS
+ OPERATOR 7 @> ,
+ OPERATOR 9 ?(hstore,text) ,
+ OPERATOR 10 ?|(hstore,text[]) ,
+ OPERATOR 11 ?&(hstore,text[]) ,
+ --OPERATOR 8 <@ ,
+ OPERATOR 13 @ ,
+ --OPERATOR 14 ~ ,
+ FUNCTION 1 ghstore_consistent (internal, hstore, smallint, oid, internal),
+ FUNCTION 2 ghstore_union (internal, internal),
+ FUNCTION 3 ghstore_compress (internal),
+ FUNCTION 4 ghstore_decompress (internal),
+ FUNCTION 5 ghstore_penalty (internal, internal, internal),
+ FUNCTION 6 ghstore_picksplit (internal, internal),
+ FUNCTION 7 ghstore_same (ghstore, ghstore, internal),
+ STORAGE ghstore;
+
+-- GIN support
+
+CREATE FUNCTION gin_extract_hstore(hstore, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION gin_extract_hstore_query(hstore, internal, int2, internal, internal)
+RETURNS internal
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE FUNCTION gin_consistent_hstore(internal, int2, hstore, int4, internal, internal)
+RETURNS bool
+AS 'MODULE_PATHNAME'
+LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;
+
+CREATE OPERATOR CLASS gin_hstore_ops
+DEFAULT FOR TYPE hstore USING gin
+AS
+ OPERATOR 7 @>,
+ OPERATOR 9 ?(hstore,text),
+ OPERATOR 10 ?|(hstore,text[]),
+ OPERATOR 11 ?&(hstore,text[]),
+ FUNCTION 1 bttextcmp(text,text),
+ FUNCTION 2 gin_extract_hstore(hstore, internal),
+ FUNCTION 3 gin_extract_hstore_query(hstore, internal, int2, internal, internal),
+ FUNCTION 4 gin_consistent_hstore(internal, int2, hstore, int4, internal, internal),
+ STORAGE text;
diff --git a/contrib/hstore/hstore--1.5--1.6.sql b/contrib/hstore/hstore--1.5--1.6.sql
new file mode 100644
index 0000000..c5a2bae
--- /dev/null
+++ b/contrib/hstore/hstore--1.5--1.6.sql
@@ -0,0 +1,12 @@
+/* contrib/hstore/hstore--1.5--1.6.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.6'" to load this file. \quit
+
+CREATE FUNCTION hstore_hash_extended(hstore, int8)
+RETURNS int8
+AS 'MODULE_PATHNAME','hstore_hash_extended'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+ALTER OPERATOR FAMILY hash_hstore_ops USING hash ADD
+ FUNCTION 2 hstore_hash_extended(hstore, int8);
diff --git a/contrib/hstore/hstore--1.6--1.7.sql b/contrib/hstore/hstore--1.6--1.7.sql
new file mode 100644
index 0000000..3e5cb67
--- /dev/null
+++ b/contrib/hstore/hstore--1.6--1.7.sql
@@ -0,0 +1,27 @@
+/* contrib/hstore/hstore--1.6--1.7.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.7'" to load this file. \quit
+
+CREATE FUNCTION ghstore_options(internal)
+RETURNS void
+AS 'MODULE_PATHNAME', 'ghstore_options'
+LANGUAGE C IMMUTABLE PARALLEL SAFE;
+
+ALTER OPERATOR FAMILY gist_hstore_ops USING gist
+ADD FUNCTION 10 (hstore) ghstore_options (internal);
+
+ALTER OPERATOR ? (hstore, text)
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR ?| (hstore, text[])
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR ?& (hstore, text[])
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR @> (hstore, hstore)
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR <@ (hstore, hstore)
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR @ (hstore, hstore)
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
+ALTER OPERATOR ~ (hstore, hstore)
+ SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
diff --git a/contrib/hstore/hstore--1.7--1.8.sql b/contrib/hstore/hstore--1.7--1.8.sql
new file mode 100644
index 0000000..fb450a9
--- /dev/null
+++ b/contrib/hstore/hstore--1.7--1.8.sql
@@ -0,0 +1,17 @@
+/* contrib/hstore/hstore--1.7--1.8.sql */
+
+-- complain if script is sourced in psql, rather than via ALTER EXTENSION
+\echo Use "ALTER EXTENSION hstore UPDATE TO '1.8'" to load this file. \quit
+
+CREATE FUNCTION hstore_subscript_handler(internal)
+RETURNS internal
+AS 'MODULE_PATHNAME', 'hstore_subscript_handler'
+LANGUAGE C STRICT IMMUTABLE PARALLEL SAFE;
+
+ALTER TYPE hstore SET (
+ SUBSCRIPT = hstore_subscript_handler
+);
+
+-- Remove @ and ~
+DROP OPERATOR @ (hstore, hstore);
+DROP OPERATOR ~ (hstore, hstore);
diff --git a/contrib/hstore/hstore.control b/contrib/hstore/hstore.control
new file mode 100644
index 0000000..89e3c74
--- /dev/null
+++ b/contrib/hstore/hstore.control
@@ -0,0 +1,6 @@
+# hstore extension
+comment = 'data type for storing sets of (key, value) pairs'
+default_version = '1.8'
+module_pathname = '$libdir/hstore'
+relocatable = true
+trusted = true
diff --git a/contrib/hstore/hstore.h b/contrib/hstore/hstore.h
new file mode 100644
index 0000000..bf4a565
--- /dev/null
+++ b/contrib/hstore/hstore.h
@@ -0,0 +1,205 @@
+/*
+ * contrib/hstore/hstore.h
+ */
+#ifndef __HSTORE_H__
+#define __HSTORE_H__
+
+#include "fmgr.h"
+#include "utils/array.h"
+
+
+/*
+ * HEntry: there is one of these for each key _and_ value in an hstore
+ *
+ * the position offset points to the _end_ so that we can get the length
+ * by subtraction from the previous entry. the ISFIRST flag lets us tell
+ * whether there is a previous entry.
+ */
+typedef struct
+{
+ uint32 entry;
+} HEntry;
+
+#define HENTRY_ISFIRST 0x80000000
+#define HENTRY_ISNULL 0x40000000
+#define HENTRY_POSMASK 0x3FFFFFFF
+
+/* note possible multiple evaluations, also access to prior array element */
+#define HSE_ISFIRST(he_) (((he_).entry & HENTRY_ISFIRST) != 0)
+#define HSE_ISNULL(he_) (((he_).entry & HENTRY_ISNULL) != 0)
+#define HSE_ENDPOS(he_) ((he_).entry & HENTRY_POSMASK)
+#define HSE_OFF(he_) (HSE_ISFIRST(he_) ? 0 : HSE_ENDPOS((&(he_))[-1]))
+#define HSE_LEN(he_) (HSE_ISFIRST(he_) \
+ ? HSE_ENDPOS(he_) \
+ : HSE_ENDPOS(he_) - HSE_ENDPOS((&(he_))[-1]))
+
+/*
+ * determined by the size of "endpos" (ie HENTRY_POSMASK), though this is a
+ * bit academic since currently varlenas (and hence both the input and the
+ * whole hstore) have the same limit
+ */
+#define HSTORE_MAX_KEY_LEN 0x3FFFFFFF
+#define HSTORE_MAX_VALUE_LEN 0x3FFFFFFF
+
+typedef struct
+{
+ int32 vl_len_; /* varlena header (do not touch directly!) */
+ uint32 size_; /* flags and number of items in hstore */
+ /* array of HEntry follows */
+} HStore;
+
+/*
+ * It's not possible to get more than 2^28 items into an hstore, so we reserve
+ * the top few bits of the size field. See hstore_compat.c for one reason
+ * why. Some bits are left for future use here. MaxAllocSize makes the
+ * practical count limit slightly more than 2^28 / 3, or INT_MAX / 24, the
+ * limit for an hstore full of 4-byte keys and null values. Therefore, we
+ * don't explicitly check the format-imposed limit.
+ */
+#define HS_FLAG_NEWVERSION 0x80000000
+
+#define HS_COUNT(hsp_) ((hsp_)->size_ & 0x0FFFFFFF)
+#define HS_SETCOUNT(hsp_,c_) ((hsp_)->size_ = (c_) | HS_FLAG_NEWVERSION)
+
+
+/*
+ * "x" comes from an existing HS_COUNT() (as discussed, <= INT_MAX/24) or a
+ * Pairs array length (due to MaxAllocSize, <= INT_MAX/40). "lenstr" is no
+ * more than INT_MAX, that extreme case arising in hstore_from_arrays().
+ * Therefore, this calculation is limited to about INT_MAX / 5 + INT_MAX.
+ */
+#define HSHRDSIZE (sizeof(HStore))
+#define CALCDATASIZE(x, lenstr) ( (x) * 2 * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
+
+/* note multiple evaluations of x */
+#define ARRPTR(x) ( (HEntry*) ( (HStore*)(x) + 1 ) )
+#define STRPTR(x) ( (char*)(ARRPTR(x) + HS_COUNT((HStore*)(x)) * 2) )
+
+/* note multiple/non evaluations */
+#define HSTORE_KEY(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)]))
+#define HSTORE_VAL(arr_,str_,i_) ((str_) + HSE_OFF((arr_)[2*(i_)+1]))
+#define HSTORE_KEYLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)]))
+#define HSTORE_VALLEN(arr_,i_) (HSE_LEN((arr_)[2*(i_)+1]))
+#define HSTORE_VALISNULL(arr_,i_) (HSE_ISNULL((arr_)[2*(i_)+1]))
+
+/*
+ * currently, these following macros are the _only_ places that rely
+ * on internal knowledge of HEntry. Everything else should be using
+ * the above macros. Exception: the in-place upgrade in hstore_compat.c
+ * messes with entries directly.
+ */
+
+/*
+ * copy one key/value pair (which must be contiguous starting at
+ * sptr_) into an under-construction hstore; dent_ is an HEntry*,
+ * dbuf_ is the destination's string buffer, dptr_ is the current
+ * position in the destination. lots of modification and multiple
+ * evaluation here.
+ */
+#define HS_COPYITEM(dent_,dbuf_,dptr_,sptr_,klen_,vlen_,vnull_) \
+ do { \
+ memcpy((dptr_), (sptr_), (klen_)+(vlen_)); \
+ (dptr_) += (klen_)+(vlen_); \
+ (dent_)++->entry = ((dptr_) - (dbuf_) - (vlen_)) & HENTRY_POSMASK; \
+ (dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK) \
+ | ((vnull_) ? HENTRY_ISNULL : 0)); \
+ } while(0)
+
+/*
+ * add one key/item pair, from a Pairs structure, into an
+ * under-construction hstore
+ */
+#define HS_ADDITEM(dent_,dbuf_,dptr_,pair_) \
+ do { \
+ memcpy((dptr_), (pair_).key, (pair_).keylen); \
+ (dptr_) += (pair_).keylen; \
+ (dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK; \
+ if ((pair_).isnull) \
+ (dent_)++->entry = ((((dptr_) - (dbuf_)) & HENTRY_POSMASK) \
+ | HENTRY_ISNULL); \
+ else \
+ { \
+ memcpy((dptr_), (pair_).val, (pair_).vallen); \
+ (dptr_) += (pair_).vallen; \
+ (dent_)++->entry = ((dptr_) - (dbuf_)) & HENTRY_POSMASK; \
+ } \
+ } while (0)
+
+/* finalize a newly-constructed hstore */
+#define HS_FINALIZE(hsp_,count_,buf_,ptr_) \
+ do { \
+ int buflen = (ptr_) - (buf_); \
+ if ((count_)) \
+ ARRPTR(hsp_)[0].entry |= HENTRY_ISFIRST; \
+ if ((count_) != HS_COUNT((hsp_))) \
+ { \
+ HS_SETCOUNT((hsp_),(count_)); \
+ memmove(STRPTR(hsp_), (buf_), buflen); \
+ } \
+ SET_VARSIZE((hsp_), CALCDATASIZE((count_), buflen)); \
+ } while (0)
+
+/* ensure the varlena size of an existing hstore is correct */
+#define HS_FIXSIZE(hsp_,count_) \
+ do { \
+ int bl = (count_) ? HSE_ENDPOS(ARRPTR(hsp_)[2*(count_)-1]) : 0; \
+ SET_VARSIZE((hsp_), CALCDATASIZE((count_),bl)); \
+ } while (0)
+
+/* DatumGetHStoreP includes support for reading old-format hstore values */
+extern HStore *hstoreUpgrade(Datum orig);
+
+#define DatumGetHStoreP(d) hstoreUpgrade(d)
+
+#define PG_GETARG_HSTORE_P(x) DatumGetHStoreP(PG_GETARG_DATUM(x))
+
+
+/*
+ * Pairs is a "decompressed" representation of one key/value pair.
+ * The two strings are not necessarily null-terminated.
+ */
+typedef struct
+{
+ char *key;
+ char *val;
+ size_t keylen;
+ size_t vallen;
+ bool isnull; /* value is null? */
+ bool needfree; /* need to pfree the value? */
+} Pairs;
+
+extern int hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen);
+extern HStore *hstorePairs(Pairs *pairs, int32 pcount, int32 buflen);
+
+extern size_t hstoreCheckKeyLen(size_t len);
+extern size_t hstoreCheckValLen(size_t len);
+
+extern int hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen);
+extern Pairs *hstoreArrayToPairs(ArrayType *a, int *npairs);
+
+#define HStoreContainsStrategyNumber 7
+#define HStoreExistsStrategyNumber 9
+#define HStoreExistsAnyStrategyNumber 10
+#define HStoreExistsAllStrategyNumber 11
+#define HStoreOldContainsStrategyNumber 13 /* backwards compatibility */
+
+/*
+ * defining HSTORE_POLLUTE_NAMESPACE=0 will prevent use of old function names;
+ * for now, we default to on for the benefit of people restoring old dumps
+ */
+#ifndef HSTORE_POLLUTE_NAMESPACE
+#define HSTORE_POLLUTE_NAMESPACE 1
+#endif
+
+#if HSTORE_POLLUTE_NAMESPACE
+#define HSTORE_POLLUTE(newname_,oldname_) \
+ PG_FUNCTION_INFO_V1(oldname_); \
+ Datum newname_(PG_FUNCTION_ARGS); \
+ Datum oldname_(PG_FUNCTION_ARGS) { return newname_(fcinfo); } \
+ extern int no_such_variable
+#else
+#define HSTORE_POLLUTE(newname_,oldname_) \
+ extern int no_such_variable
+#endif
+
+#endif /* __HSTORE_H__ */
diff --git a/contrib/hstore/hstore_compat.c b/contrib/hstore/hstore_compat.c
new file mode 100644
index 0000000..d75e9cb
--- /dev/null
+++ b/contrib/hstore/hstore_compat.c
@@ -0,0 +1,359 @@
+/*
+ * contrib/hstore/hstore_compat.c
+ *
+ * Notes on old/new hstore format disambiguation.
+ *
+ * There are three formats to consider:
+ * 1) old contrib/hstore (referred to as hstore-old)
+ * 2) prerelease pgfoundry hstore
+ * 3) new contrib/hstore
+ *
+ * (2) and (3) are identical except for the HS_FLAG_NEWVERSION
+ * bit, which is set in (3) but not (2).
+ *
+ * Values that are already in format (3), or which are
+ * unambiguously in format (2), are handled by the first
+ * "return immediately" test in hstoreUpgrade().
+ *
+ * To stress a point: we ONLY get here with possibly-ambiguous
+ * values if we're doing some sort of in-place migration from an
+ * old prerelease pgfoundry hstore-new; and we explicitly don't
+ * support that without fixing up any potentially padded values
+ * first. Most of the code here is serious overkill, but the
+ * performance penalty isn't serious (especially compared to the
+ * palloc() that we have to do anyway) and the belt-and-braces
+ * validity checks provide some reassurance. (If for some reason
+ * we get a value that would have worked on the old code, but
+ * which would be botched by the conversion code, the validity
+ * checks will fail it first so we get an error rather than bad
+ * data.)
+ *
+ * Note also that empty hstores are the same in (2) and (3), so
+ * there are some special-case paths for them.
+ *
+ * We tell the difference between formats (2) and (3) as follows (but
+ * note that there are some edge cases where we can't tell; see
+ * comments in hstoreUpgrade):
+ *
+ * First, since there must be at least one entry, we look at
+ * how the bits line up. The new format looks like:
+ *
+ * 10kkkkkkkkkkkkkkkkkkkkkkkkkkkkkk (k..k = keylen)
+ * 0nvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv (v..v = keylen+vallen)
+ *
+ * The old format looks like one of these, depending on endianness
+ * and bitfield layout: (k..k = keylen, v..v = vallen, p..p = pos,
+ * n = isnull)
+ *
+ * kkkkkkkkkkkkkkkkvvvvvvvvvvvvvvvv
+ * nppppppppppppppppppppppppppppppp
+ *
+ * kkkkkkkkkkkkkkkkvvvvvvvvvvvvvvvv
+ * pppppppppppppppppppppppppppppppn
+ *
+ * vvvvvvvvvvvvvvvvkkkkkkkkkkkkkkkk
+ * nppppppppppppppppppppppppppppppp
+ *
+ * vvvvvvvvvvvvvvvvkkkkkkkkkkkkkkkk
+ * pppppppppppppppppppppppppppppppn (usual i386 format)
+ *
+ * If the entry is in old format, for the first entry "pos" must be 0.
+ * We can obviously see that either keylen or vallen must be >32768
+ * for there to be any ambiguity (which is why lengths less than that
+ * are fasttracked in hstore.h) Since "pos"==0, the "v" field in the
+ * new-format interpretation can only be 0 or 1, which constrains all
+ * but three bits of the old-format's k and v fields. But in addition
+ * to all of this, the data length implied by the keylen and vallen
+ * must fit in the varlena size. So the only ambiguous edge case for
+ * hstores with only one entry occurs between a new-format entry with
+ * an excess (~32k) of padding, and an old-format entry. But we know
+ * which format to use in that case based on how we were compiled, so
+ * no actual data corruption can occur.
+ *
+ * If there is more than one entry, the requirement that keys do not
+ * decrease in length, and that positions increase contiguously, and
+ * that the end of the data not be beyond the end of the varlena
+ * itself, disambiguates in almost all other cases. There is a small
+ * set of ambiguous cases which could occur if the old-format value
+ * has a large excess of padding and just the right pattern of key
+ * sizes, but these are also handled based on how we were compiled.
+ *
+ * The otherwise undocumented function hstore_version_diag is provided
+ * for testing purposes.
+ */
+#include "postgres.h"
+
+
+#include "hstore.h"
+
+/*
+ * This is the structure used for entries in the old contrib/hstore
+ * implementation. Notice that this is the same size as the new entry
+ * (two 32-bit words per key/value pair) and that the header is the
+ * same, so the old and new versions of ARRPTR, STRPTR, CALCDATASIZE
+ * etc. are compatible.
+ *
+ * If the above statement isn't true on some bizarre platform, we're
+ * a bit hosed (see StaticAssertStmt in hstoreValidOldFormat).
+ */
+typedef struct
+{
+ uint16 keylen;
+ uint16 vallen;
+ uint32
+ valisnull:1,
+ pos:31;
+} HOldEntry;
+
+static int hstoreValidNewFormat(HStore *hs);
+static int hstoreValidOldFormat(HStore *hs);
+
+
+/*
+ * Validity test for a new-format hstore.
+ * 0 = not valid
+ * 1 = valid but with "slop" in the length
+ * 2 = exactly valid
+ */
+static int
+hstoreValidNewFormat(HStore *hs)
+{
+ int count = HS_COUNT(hs);
+ HEntry *entries = ARRPTR(hs);
+ int buflen = (count) ? HSE_ENDPOS(entries[2 * (count) - 1]) : 0;
+ int vsize = CALCDATASIZE(count, buflen);
+ int i;
+
+ if (hs->size_ & HS_FLAG_NEWVERSION)
+ return 2;
+
+ if (count == 0)
+ return 2;
+
+ if (!HSE_ISFIRST(entries[0]))
+ return 0;
+
+ if (vsize > VARSIZE(hs))
+ return 0;
+
+ /* entry position must be nondecreasing */
+
+ for (i = 1; i < 2 * count; ++i)
+ {
+ if (HSE_ISFIRST(entries[i])
+ || (HSE_ENDPOS(entries[i]) < HSE_ENDPOS(entries[i - 1])))
+ return 0;
+ }
+
+ /* key length must be nondecreasing and keys must not be null */
+
+ for (i = 1; i < count; ++i)
+ {
+ if (HSTORE_KEYLEN(entries, i) < HSTORE_KEYLEN(entries, i - 1))
+ return 0;
+ if (HSE_ISNULL(entries[2 * i]))
+ return 0;
+ }
+
+ if (vsize != VARSIZE(hs))
+ return 1;
+
+ return 2;
+}
+
+/*
+ * Validity test for an old-format hstore.
+ * 0 = not valid
+ * 1 = valid but with "slop" in the length
+ * 2 = exactly valid
+ */
+static int
+hstoreValidOldFormat(HStore *hs)
+{
+ int count = hs->size_;
+ HOldEntry *entries = (HOldEntry *) ARRPTR(hs);
+ int vsize;
+ int lastpos = 0;
+ int i;
+
+ if (hs->size_ & HS_FLAG_NEWVERSION)
+ return 0;
+
+ /* New format uses an HEntry for key and another for value */
+ StaticAssertStmt(sizeof(HOldEntry) == 2 * sizeof(HEntry),
+ "old hstore format is not upward-compatible");
+
+ if (count == 0)
+ return 2;
+
+ if (count > 0xFFFFFFF)
+ return 0;
+
+ if (CALCDATASIZE(count, 0) > VARSIZE(hs))
+ return 0;
+
+ if (entries[0].pos != 0)
+ return 0;
+
+ /* key length must be nondecreasing */
+
+ for (i = 1; i < count; ++i)
+ {
+ if (entries[i].keylen < entries[i - 1].keylen)
+ return 0;
+ }
+
+ /*
+ * entry position must be strictly increasing, except for the first entry
+ * (which can be ""=>"" and thus zero-length); and all entries must be
+ * properly contiguous
+ */
+
+ for (i = 0; i < count; ++i)
+ {
+ if (entries[i].pos != lastpos)
+ return 0;
+ lastpos += (entries[i].keylen
+ + ((entries[i].valisnull) ? 0 : entries[i].vallen));
+ }
+
+ vsize = CALCDATASIZE(count, lastpos);
+
+ if (vsize > VARSIZE(hs))
+ return 0;
+
+ if (vsize != VARSIZE(hs))
+ return 1;
+
+ return 2;
+}
+
+
+/*
+ * hstoreUpgrade: PG_DETOAST_DATUM plus support for conversion of old hstores
+ */
+HStore *
+hstoreUpgrade(Datum orig)
+{
+ HStore *hs = (HStore *) PG_DETOAST_DATUM(orig);
+ int valid_new;
+ int valid_old;
+
+ /* Return immediately if no conversion needed */
+ if (hs->size_ & HS_FLAG_NEWVERSION)
+ return hs;
+
+ /* Do we have a writable copy? If not, make one. */
+ if ((void *) hs == (void *) DatumGetPointer(orig))
+ hs = (HStore *) PG_DETOAST_DATUM_COPY(orig);
+
+ if (hs->size_ == 0 ||
+ (VARSIZE(hs) < 32768 && HSE_ISFIRST((ARRPTR(hs)[0]))))
+ {
+ HS_SETCOUNT(hs, HS_COUNT(hs));
+ HS_FIXSIZE(hs, HS_COUNT(hs));
+ return hs;
+ }
+
+ valid_new = hstoreValidNewFormat(hs);
+ valid_old = hstoreValidOldFormat(hs);
+
+ if (!valid_old || hs->size_ == 0)
+ {
+ if (valid_new)
+ {
+ /*
+ * force the "new version" flag and the correct varlena length.
+ */
+ HS_SETCOUNT(hs, HS_COUNT(hs));
+ HS_FIXSIZE(hs, HS_COUNT(hs));
+ return hs;
+ }
+ else
+ {
+ elog(ERROR, "invalid hstore value found");
+ }
+ }
+
+ /*
+ * this is the tricky edge case. It is only possible in some quite extreme
+ * cases (the hstore must have had a lot of wasted padding space at the
+ * end). But the only way a "new" hstore value could get here is if we're
+ * upgrading in place from a pre-release version of hstore-new (NOT
+ * contrib/hstore), so we work off the following assumptions: 1. If you're
+ * moving from old contrib/hstore to hstore-new, you're required to fix up
+ * any potential conflicts first, e.g. by running ALTER TABLE ... USING
+ * col::text::hstore; on all hstore columns before upgrading. 2. If you're
+ * moving from old contrib/hstore to new contrib/hstore, then "new" values
+ * are impossible here 3. If you're moving from pre-release hstore-new to
+ * hstore-new, then "old" values are impossible here 4. If you're moving
+ * from pre-release hstore-new to new contrib/hstore, you're not doing so
+ * as an in-place upgrade, so there is no issue So the upshot of all this
+ * is that we can treat all the edge cases as "new" if we're being built
+ * as hstore-new, and "old" if we're being built as contrib/hstore.
+ *
+ * XXX the WARNING can probably be downgraded to DEBUG1 once this has been
+ * beta-tested. But for now, it would be very useful to know if anyone can
+ * actually reach this case in a non-contrived setting.
+ */
+
+ if (valid_new)
+ {
+#ifdef HSTORE_IS_HSTORE_NEW
+ elog(WARNING, "ambiguous hstore value resolved as hstore-new");
+
+ /*
+ * force the "new version" flag and the correct varlena length.
+ */
+ HS_SETCOUNT(hs, HS_COUNT(hs));
+ HS_FIXSIZE(hs, HS_COUNT(hs));
+ return hs;
+#else
+ elog(WARNING, "ambiguous hstore value resolved as hstore-old");
+#endif
+ }
+
+ /*
+ * must have an old-style value. Overwrite it in place as a new-style one.
+ */
+ {
+ int count = hs->size_;
+ HEntry *new_entries = ARRPTR(hs);
+ HOldEntry *old_entries = (HOldEntry *) ARRPTR(hs);
+ int i;
+
+ for (i = 0; i < count; ++i)
+ {
+ uint32 pos = old_entries[i].pos;
+ uint32 keylen = old_entries[i].keylen;
+ uint32 vallen = old_entries[i].vallen;
+ bool isnull = old_entries[i].valisnull;
+
+ if (isnull)
+ vallen = 0;
+
+ new_entries[2 * i].entry = (pos + keylen) & HENTRY_POSMASK;
+ new_entries[2 * i + 1].entry = (((pos + keylen + vallen) & HENTRY_POSMASK)
+ | ((isnull) ? HENTRY_ISNULL : 0));
+ }
+
+ if (count)
+ new_entries[0].entry |= HENTRY_ISFIRST;
+ HS_SETCOUNT(hs, count);
+ HS_FIXSIZE(hs, count);
+ }
+
+ return hs;
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_version_diag);
+Datum
+hstore_version_diag(PG_FUNCTION_ARGS)
+{
+ HStore *hs = (HStore *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ int valid_new = hstoreValidNewFormat(hs);
+ int valid_old = hstoreValidOldFormat(hs);
+
+ PG_RETURN_INT32(valid_old * 10 + valid_new);
+}
diff --git a/contrib/hstore/hstore_gin.c b/contrib/hstore/hstore_gin.c
new file mode 100644
index 0000000..9085302
--- /dev/null
+++ b/contrib/hstore/hstore_gin.c
@@ -0,0 +1,212 @@
+/*
+ * contrib/hstore/hstore_gin.c
+ */
+#include "postgres.h"
+
+#include "access/gin.h"
+#include "access/stratnum.h"
+#include "catalog/pg_type.h"
+
+#include "hstore.h"
+
+
+/*
+ * When using a GIN index for hstore, we choose to index both keys and values.
+ * The storage format is "text" values, with K, V, or N prepended to the string
+ * to indicate key, value, or null values. (As of 9.1 it might be better to
+ * store null values as nulls, but we'll keep it this way for on-disk
+ * compatibility.)
+ */
+#define KEYFLAG 'K'
+#define VALFLAG 'V'
+#define NULLFLAG 'N'
+
+PG_FUNCTION_INFO_V1(gin_extract_hstore);
+
+/* Build an indexable text value */
+static text *
+makeitem(char *str, int len, char flag)
+{
+ text *item;
+
+ item = (text *) palloc(VARHDRSZ + len + 1);
+ SET_VARSIZE(item, VARHDRSZ + len + 1);
+
+ *VARDATA(item) = flag;
+
+ if (str && len > 0)
+ memcpy(VARDATA(item) + 1, str, len);
+
+ return item;
+}
+
+Datum
+gin_extract_hstore(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
+ Datum *entries = NULL;
+ HEntry *hsent = ARRPTR(hs);
+ char *ptr = STRPTR(hs);
+ int count = HS_COUNT(hs);
+ int i;
+
+ *nentries = 2 * count;
+ if (count)
+ entries = (Datum *) palloc(sizeof(Datum) * 2 * count);
+
+ for (i = 0; i < count; ++i)
+ {
+ text *item;
+
+ item = makeitem(HSTORE_KEY(hsent, ptr, i),
+ HSTORE_KEYLEN(hsent, i),
+ KEYFLAG);
+ entries[2 * i] = PointerGetDatum(item);
+
+ if (HSTORE_VALISNULL(hsent, i))
+ item = makeitem(NULL, 0, NULLFLAG);
+ else
+ item = makeitem(HSTORE_VAL(hsent, ptr, i),
+ HSTORE_VALLEN(hsent, i),
+ VALFLAG);
+ entries[2 * i + 1] = PointerGetDatum(item);
+ }
+
+ PG_RETURN_POINTER(entries);
+}
+
+PG_FUNCTION_INFO_V1(gin_extract_hstore_query);
+
+Datum
+gin_extract_hstore_query(PG_FUNCTION_ARGS)
+{
+ int32 *nentries = (int32 *) PG_GETARG_POINTER(1);
+ StrategyNumber strategy = PG_GETARG_UINT16(2);
+ int32 *searchMode = (int32 *) PG_GETARG_POINTER(6);
+ Datum *entries;
+
+ if (strategy == HStoreContainsStrategyNumber)
+ {
+ /* Query is an hstore, so just apply gin_extract_hstore... */
+ entries = (Datum *)
+ DatumGetPointer(DirectFunctionCall2(gin_extract_hstore,
+ PG_GETARG_DATUM(0),
+ PointerGetDatum(nentries)));
+ /* ... except that "contains {}" requires a full index scan */
+ if (entries == NULL)
+ *searchMode = GIN_SEARCH_MODE_ALL;
+ }
+ else if (strategy == HStoreExistsStrategyNumber)
+ {
+ text *query = PG_GETARG_TEXT_PP(0);
+ text *item;
+
+ *nentries = 1;
+ entries = (Datum *) palloc(sizeof(Datum));
+ item = makeitem(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query), KEYFLAG);
+ entries[0] = PointerGetDatum(item);
+ }
+ else if (strategy == HStoreExistsAnyStrategyNumber ||
+ strategy == HStoreExistsAllStrategyNumber)
+ {
+ ArrayType *query = PG_GETARG_ARRAYTYPE_P(0);
+ Datum *key_datums;
+ bool *key_nulls;
+ int key_count;
+ int i,
+ j;
+ text *item;
+
+ deconstruct_array(query,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ entries = (Datum *) palloc(sizeof(Datum) * key_count);
+
+ for (i = 0, j = 0; i < key_count; ++i)
+ {
+ /* Nulls in the array are ignored, cf hstoreArrayToPairs */
+ if (key_nulls[i])
+ continue;
+ item = makeitem(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ, KEYFLAG);
+ entries[j++] = PointerGetDatum(item);
+ }
+
+ *nentries = j;
+ /* ExistsAll with no keys should match everything */
+ if (j == 0 && strategy == HStoreExistsAllStrategyNumber)
+ *searchMode = GIN_SEARCH_MODE_ALL;
+ }
+ else
+ {
+ elog(ERROR, "unrecognized strategy number: %d", strategy);
+ entries = NULL; /* keep compiler quiet */
+ }
+
+ PG_RETURN_POINTER(entries);
+}
+
+PG_FUNCTION_INFO_V1(gin_consistent_hstore);
+
+Datum
+gin_consistent_hstore(PG_FUNCTION_ARGS)
+{
+ bool *check = (bool *) PG_GETARG_POINTER(0);
+ StrategyNumber strategy = PG_GETARG_UINT16(1);
+
+ /* HStore *query = PG_GETARG_HSTORE_P(2); */
+ int32 nkeys = PG_GETARG_INT32(3);
+
+ /* Pointer *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
+ bool *recheck = (bool *) PG_GETARG_POINTER(5);
+ bool res = true;
+ int32 i;
+
+ if (strategy == HStoreContainsStrategyNumber)
+ {
+ /*
+ * Index doesn't have information about correspondence of keys and
+ * values, so we need recheck. However, if not all the keys are
+ * present, we can fail at once.
+ */
+ *recheck = true;
+ for (i = 0; i < nkeys; i++)
+ {
+ if (!check[i])
+ {
+ res = false;
+ break;
+ }
+ }
+ }
+ else if (strategy == HStoreExistsStrategyNumber)
+ {
+ /* Existence of key is guaranteed in default search mode */
+ *recheck = false;
+ res = true;
+ }
+ else if (strategy == HStoreExistsAnyStrategyNumber)
+ {
+ /* Existence of key is guaranteed in default search mode */
+ *recheck = false;
+ res = true;
+ }
+ else if (strategy == HStoreExistsAllStrategyNumber)
+ {
+ /* Testing for all the keys being present gives an exact result */
+ *recheck = false;
+ for (i = 0; i < nkeys; i++)
+ {
+ if (!check[i])
+ {
+ res = false;
+ break;
+ }
+ }
+ }
+ else
+ elog(ERROR, "unrecognized strategy number: %d", strategy);
+
+ PG_RETURN_BOOL(res);
+}
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
new file mode 100644
index 0000000..102c9ce
--- /dev/null
+++ b/contrib/hstore/hstore_gist.c
@@ -0,0 +1,621 @@
+/*
+ * contrib/hstore/hstore_gist.c
+ */
+#include "postgres.h"
+
+#include "access/gist.h"
+#include "access/reloptions.h"
+#include "access/stratnum.h"
+#include "catalog/pg_type.h"
+#include "hstore.h"
+#include "utils/pg_crc.h"
+
+/* gist_hstore_ops opclass options */
+typedef struct
+{
+ int32 vl_len_; /* varlena header (do not touch directly!) */
+ int siglen; /* signature length in bytes */
+} GistHstoreOptions;
+
+/* bigint defines */
+#define BITBYTE 8
+#define SIGLEN_DEFAULT (sizeof(int32) * 4)
+#define SIGLEN_MAX GISTMaxIndexKeySize
+#define SIGLENBIT(siglen) ((siglen) * BITBYTE)
+#define GET_SIGLEN() (PG_HAS_OPCLASS_OPTIONS() ? \
+ ((GistHstoreOptions *) PG_GET_OPCLASS_OPTIONS())->siglen : \
+ SIGLEN_DEFAULT)
+
+
+typedef char *BITVECP;
+
+#define LOOPBYTE(siglen) \
+ for (i = 0; i < (siglen); i++)
+
+#define LOOPBIT(siglen) \
+ for (i = 0; i < SIGLENBIT(siglen); i++)
+
+/* beware of multiple evaluation of arguments to these macros! */
+#define GETBYTE(x,i) ( *( (BITVECP)(x) + (int)( (i) / BITBYTE ) ) )
+#define GETBITBYTE(x,i) ( (*((char*)(x)) >> (i)) & 0x01 )
+#define CLRBIT(x,i) GETBYTE(x,i) &= ~( 0x01 << ( (i) % BITBYTE ) )
+#define SETBIT(x,i) GETBYTE(x,i) |= ( 0x01 << ( (i) % BITBYTE ) )
+#define GETBIT(x,i) ( (GETBYTE(x,i) >> ( (i) % BITBYTE )) & 0x01 )
+#define HASHVAL(val, siglen) (((unsigned int)(val)) % SIGLENBIT(siglen))
+#define HASH(sign, val, siglen) SETBIT((sign), HASHVAL(val, siglen))
+
+typedef struct
+{
+ int32 vl_len_; /* varlena header (do not touch directly!) */
+ int32 flag;
+ char data[FLEXIBLE_ARRAY_MEMBER];
+} GISTTYPE;
+
+#define ALLISTRUE 0x04
+
+#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
+
+#define GTHDRSIZE (VARHDRSZ + sizeof(int32))
+#define CALCGTSIZE(flag, siglen) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : (siglen)) )
+
+#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
+
+#define SUMBIT(val) ( \
+ GETBITBYTE((val),0) + \
+ GETBITBYTE((val),1) + \
+ GETBITBYTE((val),2) + \
+ GETBITBYTE((val),3) + \
+ GETBITBYTE((val),4) + \
+ GETBITBYTE((val),5) + \
+ GETBITBYTE((val),6) + \
+ GETBITBYTE((val),7) \
+)
+
+#define GETENTRY(vec,pos) ((GISTTYPE *) DatumGetPointer((vec)->vector[(pos)].key))
+
+#define WISH_F(a,b,c) (double)( -(double)(((a)-(b))*((a)-(b))*((a)-(b)))*(c) )
+
+/* shorthand for calculating CRC-32 of a single chunk of data. */
+static pg_crc32
+crc32_sz(char *buf, int size)
+{
+ pg_crc32 crc;
+
+ INIT_TRADITIONAL_CRC32(crc);
+ COMP_TRADITIONAL_CRC32(crc, buf, size);
+ FIN_TRADITIONAL_CRC32(crc);
+
+ return crc;
+}
+
+
+PG_FUNCTION_INFO_V1(ghstore_in);
+PG_FUNCTION_INFO_V1(ghstore_out);
+
+
+Datum
+ghstore_in(PG_FUNCTION_ARGS)
+{
+ elog(ERROR, "Not implemented");
+ PG_RETURN_DATUM(0);
+}
+
+Datum
+ghstore_out(PG_FUNCTION_ARGS)
+{
+ elog(ERROR, "Not implemented");
+ PG_RETURN_DATUM(0);
+}
+
+static GISTTYPE *
+ghstore_alloc(bool allistrue, int siglen, BITVECP sign)
+{
+ int flag = allistrue ? ALLISTRUE : 0;
+ int size = CALCGTSIZE(flag, siglen);
+ GISTTYPE *res = palloc(size);
+
+ SET_VARSIZE(res, size);
+ res->flag = flag;
+
+ if (!allistrue)
+ {
+ if (sign)
+ memcpy(GETSIGN(res), sign, siglen);
+ else
+ memset(GETSIGN(res), 0, siglen);
+ }
+
+ return res;
+}
+
+PG_FUNCTION_INFO_V1(ghstore_consistent);
+PG_FUNCTION_INFO_V1(ghstore_compress);
+PG_FUNCTION_INFO_V1(ghstore_decompress);
+PG_FUNCTION_INFO_V1(ghstore_penalty);
+PG_FUNCTION_INFO_V1(ghstore_picksplit);
+PG_FUNCTION_INFO_V1(ghstore_union);
+PG_FUNCTION_INFO_V1(ghstore_same);
+PG_FUNCTION_INFO_V1(ghstore_options);
+
+Datum
+ghstore_compress(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
+ int siglen = GET_SIGLEN();
+ GISTENTRY *retval = entry;
+
+ if (entry->leafkey)
+ {
+ GISTTYPE *res = ghstore_alloc(false, siglen, NULL);
+ HStore *val = DatumGetHStoreP(entry->key);
+ HEntry *hsent = ARRPTR(val);
+ char *ptr = STRPTR(val);
+ int count = HS_COUNT(val);
+ int i;
+
+ for (i = 0; i < count; ++i)
+ {
+ int h;
+
+ h = crc32_sz((char *) HSTORE_KEY(hsent, ptr, i),
+ HSTORE_KEYLEN(hsent, i));
+ HASH(GETSIGN(res), h, siglen);
+ if (!HSTORE_VALISNULL(hsent, i))
+ {
+ h = crc32_sz((char *) HSTORE_VAL(hsent, ptr, i),
+ HSTORE_VALLEN(hsent, i));
+ HASH(GETSIGN(res), h, siglen);
+ }
+ }
+
+ retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
+ gistentryinit(*retval, PointerGetDatum(res),
+ entry->rel, entry->page,
+ entry->offset,
+ false);
+ }
+ else if (!ISALLTRUE(DatumGetPointer(entry->key)))
+ {
+ int32 i;
+ GISTTYPE *res;
+ BITVECP sign = GETSIGN(DatumGetPointer(entry->key));
+
+ LOOPBYTE(siglen)
+ {
+ if ((sign[i] & 0xff) != 0xff)
+ PG_RETURN_POINTER(retval);
+ }
+
+ res = ghstore_alloc(true, siglen, NULL);
+
+ retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
+ gistentryinit(*retval, PointerGetDatum(res),
+ entry->rel, entry->page,
+ entry->offset,
+ false);
+ }
+
+ PG_RETURN_POINTER(retval);
+}
+
+/*
+ * Since type ghstore isn't toastable (and doesn't need to be),
+ * this function can be a no-op.
+ */
+Datum
+ghstore_decompress(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_POINTER(PG_GETARG_POINTER(0));
+}
+
+Datum
+ghstore_same(PG_FUNCTION_ARGS)
+{
+ GISTTYPE *a = (GISTTYPE *) PG_GETARG_POINTER(0);
+ GISTTYPE *b = (GISTTYPE *) PG_GETARG_POINTER(1);
+ bool *result = (bool *) PG_GETARG_POINTER(2);
+ int siglen = GET_SIGLEN();
+
+
+ if (ISALLTRUE(a) && ISALLTRUE(b))
+ *result = true;
+ else if (ISALLTRUE(a))
+ *result = false;
+ else if (ISALLTRUE(b))
+ *result = false;
+ else
+ {
+ int32 i;
+ BITVECP sa = GETSIGN(a),
+ sb = GETSIGN(b);
+
+ *result = true;
+ LOOPBYTE(siglen)
+ {
+ if (sa[i] != sb[i])
+ {
+ *result = false;
+ break;
+ }
+ }
+ }
+ PG_RETURN_POINTER(result);
+}
+
+static int32
+sizebitvec(BITVECP sign, int siglen)
+{
+ int32 size = 0,
+ i;
+
+ LOOPBYTE(siglen)
+ {
+ size += SUMBIT(sign);
+ sign = (BITVECP) (((char *) sign) + 1);
+ }
+ return size;
+}
+
+static int
+hemdistsign(BITVECP a, BITVECP b, int siglen)
+{
+ int i,
+ dist = 0;
+
+ LOOPBIT(siglen)
+ {
+ if (GETBIT(a, i) != GETBIT(b, i))
+ dist++;
+ }
+ return dist;
+}
+
+static int
+hemdist(GISTTYPE *a, GISTTYPE *b, int siglen)
+{
+ if (ISALLTRUE(a))
+ {
+ if (ISALLTRUE(b))
+ return 0;
+ else
+ return SIGLENBIT(siglen) - sizebitvec(GETSIGN(b), siglen);
+ }
+ else if (ISALLTRUE(b))
+ return SIGLENBIT(siglen) - sizebitvec(GETSIGN(a), siglen);
+
+ return hemdistsign(GETSIGN(a), GETSIGN(b), siglen);
+}
+
+static int32
+unionkey(BITVECP sbase, GISTTYPE *add, int siglen)
+{
+ int32 i;
+ BITVECP sadd = GETSIGN(add);
+
+ if (ISALLTRUE(add))
+ return 1;
+ LOOPBYTE(siglen)
+ sbase[i] |= sadd[i];
+ return 0;
+}
+
+Datum
+ghstore_union(PG_FUNCTION_ARGS)
+{
+ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
+ int32 len = entryvec->n;
+
+ int *size = (int *) PG_GETARG_POINTER(1);
+ int siglen = GET_SIGLEN();
+ int32 i;
+ GISTTYPE *result = ghstore_alloc(false, siglen, NULL);
+ BITVECP base = GETSIGN(result);
+
+ for (i = 0; i < len; i++)
+ {
+ if (unionkey(base, GETENTRY(entryvec, i), siglen))
+ {
+ result->flag |= ALLISTRUE;
+ SET_VARSIZE(result, CALCGTSIZE(ALLISTRUE, siglen));
+ break;
+ }
+ }
+
+ *size = VARSIZE(result);
+
+ PG_RETURN_POINTER(result);
+}
+
+Datum
+ghstore_penalty(PG_FUNCTION_ARGS)
+{
+ GISTENTRY *origentry = (GISTENTRY *) PG_GETARG_POINTER(0); /* always ISSIGNKEY */
+ GISTENTRY *newentry = (GISTENTRY *) PG_GETARG_POINTER(1);
+ float *penalty = (float *) PG_GETARG_POINTER(2);
+ int siglen = GET_SIGLEN();
+ GISTTYPE *origval = (GISTTYPE *) DatumGetPointer(origentry->key);
+ GISTTYPE *newval = (GISTTYPE *) DatumGetPointer(newentry->key);
+
+ *penalty = hemdist(origval, newval, siglen);
+ PG_RETURN_POINTER(penalty);
+}
+
+
+typedef struct
+{
+ OffsetNumber pos;
+ int32 cost;
+} SPLITCOST;
+
+static int
+comparecost(const void *a, const void *b)
+{
+ return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost;
+}
+
+
+Datum
+ghstore_picksplit(PG_FUNCTION_ARGS)
+{
+ GistEntryVector *entryvec = (GistEntryVector *) PG_GETARG_POINTER(0);
+ OffsetNumber maxoff = entryvec->n - 2;
+
+ GIST_SPLITVEC *v = (GIST_SPLITVEC *) PG_GETARG_POINTER(1);
+ int siglen = GET_SIGLEN();
+ OffsetNumber k,
+ j;
+ GISTTYPE *datum_l,
+ *datum_r;
+ BITVECP union_l,
+ union_r;
+ int32 size_alpha,
+ size_beta;
+ int32 size_waste,
+ waste = -1;
+ int32 nbytes;
+ OffsetNumber seed_1 = 0,
+ seed_2 = 0;
+ OffsetNumber *left,
+ *right;
+ BITVECP ptr;
+ int i;
+ SPLITCOST *costvector;
+ GISTTYPE *_k,
+ *_j;
+
+ nbytes = (maxoff + 2) * sizeof(OffsetNumber);
+ v->spl_left = (OffsetNumber *) palloc(nbytes);
+ v->spl_right = (OffsetNumber *) palloc(nbytes);
+
+ for (k = FirstOffsetNumber; k < maxoff; k = OffsetNumberNext(k))
+ {
+ _k = GETENTRY(entryvec, k);
+ for (j = OffsetNumberNext(k); j <= maxoff; j = OffsetNumberNext(j))
+ {
+ size_waste = hemdist(_k, GETENTRY(entryvec, j), siglen);
+ if (size_waste > waste)
+ {
+ waste = size_waste;
+ seed_1 = k;
+ seed_2 = j;
+ }
+ }
+ }
+
+ left = v->spl_left;
+ v->spl_nleft = 0;
+ right = v->spl_right;
+ v->spl_nright = 0;
+
+ if (seed_1 == 0 || seed_2 == 0)
+ {
+ seed_1 = 1;
+ seed_2 = 2;
+ }
+
+ /* form initial .. */
+ datum_l = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_1)), siglen,
+ GETSIGN(GETENTRY(entryvec, seed_1)));
+ datum_r = ghstore_alloc(ISALLTRUE(GETENTRY(entryvec, seed_2)), siglen,
+ GETSIGN(GETENTRY(entryvec, seed_2)));
+
+ maxoff = OffsetNumberNext(maxoff);
+ /* sort before ... */
+ costvector = (SPLITCOST *) palloc(sizeof(SPLITCOST) * maxoff);
+ for (j = FirstOffsetNumber; j <= maxoff; j = OffsetNumberNext(j))
+ {
+ costvector[j - 1].pos = j;
+ _j = GETENTRY(entryvec, j);
+ size_alpha = hemdist(datum_l, _j, siglen);
+ size_beta = hemdist(datum_r, _j, siglen);
+ costvector[j - 1].cost = abs(size_alpha - size_beta);
+ }
+ qsort((void *) costvector, maxoff, sizeof(SPLITCOST), comparecost);
+
+ union_l = GETSIGN(datum_l);
+ union_r = GETSIGN(datum_r);
+
+ for (k = 0; k < maxoff; k++)
+ {
+ j = costvector[k].pos;
+ if (j == seed_1)
+ {
+ *left++ = j;
+ v->spl_nleft++;
+ continue;
+ }
+ else if (j == seed_2)
+ {
+ *right++ = j;
+ v->spl_nright++;
+ continue;
+ }
+ _j = GETENTRY(entryvec, j);
+ size_alpha = hemdist(datum_l, _j, siglen);
+ size_beta = hemdist(datum_r, _j, siglen);
+
+ if (size_alpha < size_beta + WISH_F(v->spl_nleft, v->spl_nright, 0.0001))
+ {
+ if (ISALLTRUE(datum_l) || ISALLTRUE(_j))
+ {
+ if (!ISALLTRUE(datum_l))
+ MemSet((void *) union_l, 0xff, siglen);
+ }
+ else
+ {
+ ptr = GETSIGN(_j);
+ LOOPBYTE(siglen)
+ union_l[i] |= ptr[i];
+ }
+ *left++ = j;
+ v->spl_nleft++;
+ }
+ else
+ {
+ if (ISALLTRUE(datum_r) || ISALLTRUE(_j))
+ {
+ if (!ISALLTRUE(datum_r))
+ MemSet((void *) union_r, 0xff, siglen);
+ }
+ else
+ {
+ ptr = GETSIGN(_j);
+ LOOPBYTE(siglen)
+ union_r[i] |= ptr[i];
+ }
+ *right++ = j;
+ v->spl_nright++;
+ }
+ }
+
+ *right = *left = FirstOffsetNumber;
+
+ v->spl_ldatum = PointerGetDatum(datum_l);
+ v->spl_rdatum = PointerGetDatum(datum_r);
+
+ PG_RETURN_POINTER(v);
+}
+
+
+Datum
+ghstore_consistent(PG_FUNCTION_ARGS)
+{
+ GISTTYPE *entry = (GISTTYPE *) DatumGetPointer(((GISTENTRY *) PG_GETARG_POINTER(0))->key);
+ StrategyNumber strategy = (StrategyNumber) PG_GETARG_UINT16(2);
+
+ /* Oid subtype = PG_GETARG_OID(3); */
+ bool *recheck = (bool *) PG_GETARG_POINTER(4);
+ int siglen = GET_SIGLEN();
+ bool res = true;
+ BITVECP sign;
+
+ /* All cases served by this function are inexact */
+ *recheck = true;
+
+ if (ISALLTRUE(entry))
+ PG_RETURN_BOOL(true);
+
+ sign = GETSIGN(entry);
+
+ if (strategy == HStoreContainsStrategyNumber ||
+ strategy == HStoreOldContainsStrategyNumber)
+ {
+ HStore *query = PG_GETARG_HSTORE_P(1);
+ HEntry *qe = ARRPTR(query);
+ char *qv = STRPTR(query);
+ int count = HS_COUNT(query);
+ int i;
+
+ for (i = 0; res && i < count; ++i)
+ {
+ int crc = crc32_sz((char *) HSTORE_KEY(qe, qv, i),
+ HSTORE_KEYLEN(qe, i));
+
+ if (GETBIT(sign, HASHVAL(crc, siglen)))
+ {
+ if (!HSTORE_VALISNULL(qe, i))
+ {
+ crc = crc32_sz((char *) HSTORE_VAL(qe, qv, i),
+ HSTORE_VALLEN(qe, i));
+ if (!GETBIT(sign, HASHVAL(crc, siglen)))
+ res = false;
+ }
+ }
+ else
+ res = false;
+ }
+ }
+ else if (strategy == HStoreExistsStrategyNumber)
+ {
+ text *query = PG_GETARG_TEXT_PP(1);
+ int crc = crc32_sz(VARDATA_ANY(query), VARSIZE_ANY_EXHDR(query));
+
+ res = (GETBIT(sign, HASHVAL(crc, siglen))) ? true : false;
+ }
+ else if (strategy == HStoreExistsAllStrategyNumber)
+ {
+ ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
+ Datum *key_datums;
+ bool *key_nulls;
+ int key_count;
+ int i;
+
+ deconstruct_array(query,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ for (i = 0; res && i < key_count; ++i)
+ {
+ int crc;
+
+ if (key_nulls[i])
+ continue;
+ crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
+ if (!(GETBIT(sign, HASHVAL(crc, siglen))))
+ res = false;
+ }
+ }
+ else if (strategy == HStoreExistsAnyStrategyNumber)
+ {
+ ArrayType *query = PG_GETARG_ARRAYTYPE_P(1);
+ Datum *key_datums;
+ bool *key_nulls;
+ int key_count;
+ int i;
+
+ deconstruct_array(query,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ res = false;
+
+ for (i = 0; !res && i < key_count; ++i)
+ {
+ int crc;
+
+ if (key_nulls[i])
+ continue;
+ crc = crc32_sz(VARDATA(key_datums[i]), VARSIZE(key_datums[i]) - VARHDRSZ);
+ if (GETBIT(sign, HASHVAL(crc, siglen)))
+ res = true;
+ }
+ }
+ else
+ elog(ERROR, "Unsupported strategy number: %d", strategy);
+
+ PG_RETURN_BOOL(res);
+}
+
+Datum
+ghstore_options(PG_FUNCTION_ARGS)
+{
+ local_relopts *relopts = (local_relopts *) PG_GETARG_POINTER(0);
+
+ init_local_reloptions(relopts, sizeof(GistHstoreOptions));
+ add_local_int_reloption(relopts, "siglen",
+ "signature length in bytes",
+ SIGLEN_DEFAULT, 1, SIGLEN_MAX,
+ offsetof(GistHstoreOptions, siglen));
+
+ PG_RETURN_VOID();
+}
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
new file mode 100644
index 0000000..b3304ff
--- /dev/null
+++ b/contrib/hstore/hstore_io.c
@@ -0,0 +1,1499 @@
+/*
+ * contrib/hstore/hstore_io.c
+ */
+#include "postgres.h"
+
+#include <ctype.h>
+
+#include "access/htup_details.h"
+#include "catalog/pg_type.h"
+#include "common/jsonapi.h"
+#include "funcapi.h"
+#include "hstore.h"
+#include "lib/stringinfo.h"
+#include "libpq/pqformat.h"
+#include "utils/builtins.h"
+#include "utils/json.h"
+#include "utils/jsonb.h"
+#include "utils/lsyscache.h"
+#include "utils/memutils.h"
+#include "utils/typcache.h"
+
+PG_MODULE_MAGIC;
+
+/* old names for C functions */
+HSTORE_POLLUTE(hstore_from_text, tconvert);
+
+
+typedef struct
+{
+ char *begin;
+ char *ptr;
+ char *cur;
+ char *word;
+ int wordlen;
+
+ Pairs *pairs;
+ int pcur;
+ int plen;
+} HSParser;
+
+#define RESIZEPRSBUF \
+do { \
+ if ( state->cur - state->word + 1 >= state->wordlen ) \
+ { \
+ int32 clen = state->cur - state->word; \
+ state->wordlen *= 2; \
+ state->word = (char*)repalloc( (void*)state->word, state->wordlen ); \
+ state->cur = state->word + clen; \
+ } \
+} while (0)
+
+
+#define GV_WAITVAL 0
+#define GV_INVAL 1
+#define GV_INESCVAL 2
+#define GV_WAITESCIN 3
+#define GV_WAITESCESCIN 4
+
+static bool
+get_val(HSParser *state, bool ignoreeq, bool *escaped)
+{
+ int st = GV_WAITVAL;
+
+ state->wordlen = 32;
+ state->cur = state->word = palloc(state->wordlen);
+ *escaped = false;
+
+ while (1)
+ {
+ if (st == GV_WAITVAL)
+ {
+ if (*(state->ptr) == '"')
+ {
+ *escaped = true;
+ st = GV_INESCVAL;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ return false;
+ }
+ else if (*(state->ptr) == '=' && !ignoreeq)
+ {
+ elog(ERROR, "Syntax error near \"%.*s\" at position %d",
+ pg_mblen(state->ptr), state->ptr,
+ (int32) (state->ptr - state->begin));
+ }
+ else if (*(state->ptr) == '\\')
+ {
+ st = GV_WAITESCIN;
+ }
+ else if (!isspace((unsigned char) *(state->ptr)))
+ {
+ *(state->cur) = *(state->ptr);
+ state->cur++;
+ st = GV_INVAL;
+ }
+ }
+ else if (st == GV_INVAL)
+ {
+ if (*(state->ptr) == '\\')
+ {
+ st = GV_WAITESCIN;
+ }
+ else if (*(state->ptr) == '=' && !ignoreeq)
+ {
+ state->ptr--;
+ return true;
+ }
+ else if (*(state->ptr) == ',' && ignoreeq)
+ {
+ state->ptr--;
+ return true;
+ }
+ else if (isspace((unsigned char) *(state->ptr)))
+ {
+ return true;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ state->ptr--;
+ return true;
+ }
+ else
+ {
+ RESIZEPRSBUF;
+ *(state->cur) = *(state->ptr);
+ state->cur++;
+ }
+ }
+ else if (st == GV_INESCVAL)
+ {
+ if (*(state->ptr) == '\\')
+ {
+ st = GV_WAITESCESCIN;
+ }
+ else if (*(state->ptr) == '"')
+ {
+ return true;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ elog(ERROR, "Unexpected end of string");
+ }
+ else
+ {
+ RESIZEPRSBUF;
+ *(state->cur) = *(state->ptr);
+ state->cur++;
+ }
+ }
+ else if (st == GV_WAITESCIN)
+ {
+ if (*(state->ptr) == '\0')
+ elog(ERROR, "Unexpected end of string");
+ RESIZEPRSBUF;
+ *(state->cur) = *(state->ptr);
+ state->cur++;
+ st = GV_INVAL;
+ }
+ else if (st == GV_WAITESCESCIN)
+ {
+ if (*(state->ptr) == '\0')
+ elog(ERROR, "Unexpected end of string");
+ RESIZEPRSBUF;
+ *(state->cur) = *(state->ptr);
+ state->cur++;
+ st = GV_INESCVAL;
+ }
+ else
+ elog(ERROR, "Unknown state %d at position line %d in file '%s'", st, __LINE__, __FILE__);
+
+ state->ptr++;
+ }
+}
+
+#define WKEY 0
+#define WVAL 1
+#define WEQ 2
+#define WGT 3
+#define WDEL 4
+
+
+static void
+parse_hstore(HSParser *state)
+{
+ int st = WKEY;
+ bool escaped = false;
+
+ state->plen = 16;
+ state->pairs = (Pairs *) palloc(sizeof(Pairs) * state->plen);
+ state->pcur = 0;
+ state->ptr = state->begin;
+ state->word = NULL;
+
+ while (1)
+ {
+ if (st == WKEY)
+ {
+ if (!get_val(state, false, &escaped))
+ return;
+ if (state->pcur >= state->plen)
+ {
+ state->plen *= 2;
+ state->pairs = (Pairs *) repalloc(state->pairs, sizeof(Pairs) * state->plen);
+ }
+ state->pairs[state->pcur].key = state->word;
+ state->pairs[state->pcur].keylen = hstoreCheckKeyLen(state->cur - state->word);
+ state->pairs[state->pcur].val = NULL;
+ state->word = NULL;
+ st = WEQ;
+ }
+ else if (st == WEQ)
+ {
+ if (*(state->ptr) == '=')
+ {
+ st = WGT;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ elog(ERROR, "Unexpected end of string");
+ }
+ else if (!isspace((unsigned char) *(state->ptr)))
+ {
+ elog(ERROR, "Syntax error near \"%.*s\" at position %d",
+ pg_mblen(state->ptr), state->ptr,
+ (int32) (state->ptr - state->begin));
+ }
+ }
+ else if (st == WGT)
+ {
+ if (*(state->ptr) == '>')
+ {
+ st = WVAL;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ elog(ERROR, "Unexpected end of string");
+ }
+ else
+ {
+ elog(ERROR, "Syntax error near \"%.*s\" at position %d",
+ pg_mblen(state->ptr), state->ptr,
+ (int32) (state->ptr - state->begin));
+ }
+ }
+ else if (st == WVAL)
+ {
+ if (!get_val(state, true, &escaped))
+ elog(ERROR, "Unexpected end of string");
+ state->pairs[state->pcur].val = state->word;
+ state->pairs[state->pcur].vallen = hstoreCheckValLen(state->cur - state->word);
+ state->pairs[state->pcur].isnull = false;
+ state->pairs[state->pcur].needfree = true;
+ if (state->cur - state->word == 4 && !escaped)
+ {
+ state->word[4] = '\0';
+ if (0 == pg_strcasecmp(state->word, "null"))
+ state->pairs[state->pcur].isnull = true;
+ }
+ state->word = NULL;
+ state->pcur++;
+ st = WDEL;
+ }
+ else if (st == WDEL)
+ {
+ if (*(state->ptr) == ',')
+ {
+ st = WKEY;
+ }
+ else if (*(state->ptr) == '\0')
+ {
+ return;
+ }
+ else if (!isspace((unsigned char) *(state->ptr)))
+ {
+ elog(ERROR, "Syntax error near \"%.*s\" at position %d",
+ pg_mblen(state->ptr), state->ptr,
+ (int32) (state->ptr - state->begin));
+ }
+ }
+ else
+ elog(ERROR, "Unknown state %d at line %d in file '%s'", st, __LINE__, __FILE__);
+
+ state->ptr++;
+ }
+}
+
+static int
+comparePairs(const void *a, const void *b)
+{
+ const Pairs *pa = a;
+ const Pairs *pb = b;
+
+ if (pa->keylen == pb->keylen)
+ {
+ int res = memcmp(pa->key, pb->key, pa->keylen);
+
+ if (res)
+ return res;
+
+ /* guarantee that needfree will be later */
+ if (pb->needfree == pa->needfree)
+ return 0;
+ else if (pa->needfree)
+ return 1;
+ else
+ return -1;
+ }
+ return (pa->keylen > pb->keylen) ? 1 : -1;
+}
+
+/*
+ * this code still respects pairs.needfree, even though in general
+ * it should never be called in a context where anything needs freeing.
+ * we keep it because (a) those calls are in a rare code path anyway,
+ * and (b) who knows whether they might be needed by some caller.
+ */
+int
+hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
+{
+ Pairs *ptr,
+ *res;
+
+ *buflen = 0;
+ if (l < 2)
+ {
+ if (l == 1)
+ *buflen = a->keylen + ((a->isnull) ? 0 : a->vallen);
+ return l;
+ }
+
+ qsort((void *) a, l, sizeof(Pairs), comparePairs);
+
+ /*
+ * We can't use qunique here because we have some clean-up code to run on
+ * removed elements.
+ */
+ ptr = a + 1;
+ res = a;
+ while (ptr - a < l)
+ {
+ if (ptr->keylen == res->keylen &&
+ memcmp(ptr->key, res->key, res->keylen) == 0)
+ {
+ if (ptr->needfree)
+ {
+ pfree(ptr->key);
+ pfree(ptr->val);
+ }
+ }
+ else
+ {
+ *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
+ res++;
+ if (res != ptr)
+ memcpy(res, ptr, sizeof(Pairs));
+ }
+
+ ptr++;
+ }
+
+ *buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
+ return res + 1 - a;
+}
+
+size_t
+hstoreCheckKeyLen(size_t len)
+{
+ if (len > HSTORE_MAX_KEY_LEN)
+ ereport(ERROR,
+ (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
+ errmsg("string too long for hstore key")));
+ return len;
+}
+
+size_t
+hstoreCheckValLen(size_t len)
+{
+ if (len > HSTORE_MAX_VALUE_LEN)
+ ereport(ERROR,
+ (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
+ errmsg("string too long for hstore value")));
+ return len;
+}
+
+
+HStore *
+hstorePairs(Pairs *pairs, int32 pcount, int32 buflen)
+{
+ HStore *out;
+ HEntry *entry;
+ char *ptr;
+ char *buf;
+ int32 len;
+ int32 i;
+
+ len = CALCDATASIZE(pcount, buflen);
+ out = palloc(len);
+ SET_VARSIZE(out, len);
+ HS_SETCOUNT(out, pcount);
+
+ if (pcount == 0)
+ return out;
+
+ entry = ARRPTR(out);
+ buf = ptr = STRPTR(out);
+
+ for (i = 0; i < pcount; i++)
+ HS_ADDITEM(entry, buf, ptr, pairs[i]);
+
+ HS_FINALIZE(out, pcount, buf, ptr);
+
+ return out;
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_in);
+Datum
+hstore_in(PG_FUNCTION_ARGS)
+{
+ HSParser state;
+ int32 buflen;
+ HStore *out;
+
+ state.begin = PG_GETARG_CSTRING(0);
+
+ parse_hstore(&state);
+
+ state.pcur = hstoreUniquePairs(state.pairs, state.pcur, &buflen);
+
+ out = hstorePairs(state.pairs, state.pcur, buflen);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_recv);
+Datum
+hstore_recv(PG_FUNCTION_ARGS)
+{
+ int32 buflen;
+ HStore *out;
+ Pairs *pairs;
+ int32 i;
+ int32 pcount;
+ StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
+
+ pcount = pq_getmsgint(buf, 4);
+
+ if (pcount == 0)
+ {
+ out = hstorePairs(NULL, 0, 0);
+ PG_RETURN_POINTER(out);
+ }
+
+ if (pcount < 0 || pcount > MaxAllocSize / sizeof(Pairs))
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+ pcount, (int) (MaxAllocSize / sizeof(Pairs)))));
+ pairs = palloc(pcount * sizeof(Pairs));
+
+ for (i = 0; i < pcount; ++i)
+ {
+ int rawlen = pq_getmsgint(buf, 4);
+ int len;
+
+ if (rawlen < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ errmsg("null value not allowed for hstore key")));
+
+ pairs[i].key = pq_getmsgtext(buf, rawlen, &len);
+ pairs[i].keylen = hstoreCheckKeyLen(len);
+ pairs[i].needfree = true;
+
+ rawlen = pq_getmsgint(buf, 4);
+ if (rawlen < 0)
+ {
+ pairs[i].val = NULL;
+ pairs[i].vallen = 0;
+ pairs[i].isnull = true;
+ }
+ else
+ {
+ pairs[i].val = pq_getmsgtext(buf, rawlen, &len);
+ pairs[i].vallen = hstoreCheckValLen(len);
+ pairs[i].isnull = false;
+ }
+ }
+
+ pcount = hstoreUniquePairs(pairs, pcount, &buflen);
+
+ out = hstorePairs(pairs, pcount, buflen);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_from_text);
+Datum
+hstore_from_text(PG_FUNCTION_ARGS)
+{
+ text *key;
+ text *val = NULL;
+ Pairs p;
+ HStore *out;
+
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+
+ p.needfree = false;
+ key = PG_GETARG_TEXT_PP(0);
+ p.key = VARDATA_ANY(key);
+ p.keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(key));
+
+ if (PG_ARGISNULL(1))
+ {
+ p.vallen = 0;
+ p.isnull = true;
+ }
+ else
+ {
+ val = PG_GETARG_TEXT_PP(1);
+ p.val = VARDATA_ANY(val);
+ p.vallen = hstoreCheckValLen(VARSIZE_ANY_EXHDR(val));
+ p.isnull = false;
+ }
+
+ out = hstorePairs(&p, 1, p.keylen + p.vallen);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_from_arrays);
+Datum
+hstore_from_arrays(PG_FUNCTION_ARGS)
+{
+ int32 buflen;
+ HStore *out;
+ Pairs *pairs;
+ Datum *key_datums;
+ bool *key_nulls;
+ int key_count;
+ Datum *value_datums;
+ bool *value_nulls;
+ int value_count;
+ ArrayType *key_array;
+ ArrayType *value_array;
+ int i;
+
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+
+ key_array = PG_GETARG_ARRAYTYPE_P(0);
+
+ Assert(ARR_ELEMTYPE(key_array) == TEXTOID);
+
+ /*
+ * must check >1 rather than != 1 because empty arrays have 0 dimensions,
+ * not 1
+ */
+
+ if (ARR_NDIM(key_array) > 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("wrong number of array subscripts")));
+
+ deconstruct_array(key_array,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ /* see discussion in hstoreArrayToPairs() */
+ if (key_count > MaxAllocSize / sizeof(Pairs))
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+ key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
+ /* value_array might be NULL */
+
+ if (PG_ARGISNULL(1))
+ {
+ value_array = NULL;
+ value_count = key_count;
+ value_datums = NULL;
+ value_nulls = NULL;
+ }
+ else
+ {
+ value_array = PG_GETARG_ARRAYTYPE_P(1);
+
+ Assert(ARR_ELEMTYPE(value_array) == TEXTOID);
+
+ if (ARR_NDIM(value_array) > 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("wrong number of array subscripts")));
+
+ if ((ARR_NDIM(key_array) > 0 || ARR_NDIM(value_array) > 0) &&
+ (ARR_NDIM(key_array) != ARR_NDIM(value_array) ||
+ ARR_DIMS(key_array)[0] != ARR_DIMS(value_array)[0] ||
+ ARR_LBOUND(key_array)[0] != ARR_LBOUND(value_array)[0]))
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("arrays must have same bounds")));
+
+ deconstruct_array(value_array,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &value_datums, &value_nulls, &value_count);
+
+ Assert(key_count == value_count);
+ }
+
+ pairs = palloc(key_count * sizeof(Pairs));
+
+ for (i = 0; i < key_count; ++i)
+ {
+ if (key_nulls[i])
+ ereport(ERROR,
+ (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ errmsg("null value not allowed for hstore key")));
+
+ if (!value_nulls || value_nulls[i])
+ {
+ pairs[i].key = VARDATA(key_datums[i]);
+ pairs[i].val = NULL;
+ pairs[i].keylen =
+ hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
+ pairs[i].vallen = 4;
+ pairs[i].isnull = true;
+ pairs[i].needfree = false;
+ }
+ else
+ {
+ pairs[i].key = VARDATA(key_datums[i]);
+ pairs[i].val = VARDATA(value_datums[i]);
+ pairs[i].keylen =
+ hstoreCheckKeyLen(VARSIZE(key_datums[i]) - VARHDRSZ);
+ pairs[i].vallen =
+ hstoreCheckValLen(VARSIZE(value_datums[i]) - VARHDRSZ);
+ pairs[i].isnull = false;
+ pairs[i].needfree = false;
+ }
+ }
+
+ key_count = hstoreUniquePairs(pairs, key_count, &buflen);
+
+ out = hstorePairs(pairs, key_count, buflen);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_from_array);
+Datum
+hstore_from_array(PG_FUNCTION_ARGS)
+{
+ ArrayType *in_array = PG_GETARG_ARRAYTYPE_P(0);
+ int ndims = ARR_NDIM(in_array);
+ int count;
+ int32 buflen;
+ HStore *out;
+ Pairs *pairs;
+ Datum *in_datums;
+ bool *in_nulls;
+ int in_count;
+ int i;
+
+ Assert(ARR_ELEMTYPE(in_array) == TEXTOID);
+
+ switch (ndims)
+ {
+ case 0:
+ out = hstorePairs(NULL, 0, 0);
+ PG_RETURN_POINTER(out);
+
+ case 1:
+ if ((ARR_DIMS(in_array)[0]) % 2)
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("array must have even number of elements")));
+ break;
+
+ case 2:
+ if ((ARR_DIMS(in_array)[1]) != 2)
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("array must have two columns")));
+ break;
+
+ default:
+ ereport(ERROR,
+ (errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
+ errmsg("wrong number of array subscripts")));
+ }
+
+ deconstruct_array(in_array,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &in_datums, &in_nulls, &in_count);
+
+ count = in_count / 2;
+
+ /* see discussion in hstoreArrayToPairs() */
+ if (count > MaxAllocSize / sizeof(Pairs))
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+ count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
+ pairs = palloc(count * sizeof(Pairs));
+
+ for (i = 0; i < count; ++i)
+ {
+ if (in_nulls[i * 2])
+ ereport(ERROR,
+ (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ errmsg("null value not allowed for hstore key")));
+
+ if (in_nulls[i * 2 + 1])
+ {
+ pairs[i].key = VARDATA(in_datums[i * 2]);
+ pairs[i].val = NULL;
+ pairs[i].keylen =
+ hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
+ pairs[i].vallen = 4;
+ pairs[i].isnull = true;
+ pairs[i].needfree = false;
+ }
+ else
+ {
+ pairs[i].key = VARDATA(in_datums[i * 2]);
+ pairs[i].val = VARDATA(in_datums[i * 2 + 1]);
+ pairs[i].keylen =
+ hstoreCheckKeyLen(VARSIZE(in_datums[i * 2]) - VARHDRSZ);
+ pairs[i].vallen =
+ hstoreCheckValLen(VARSIZE(in_datums[i * 2 + 1]) - VARHDRSZ);
+ pairs[i].isnull = false;
+ pairs[i].needfree = false;
+ }
+ }
+
+ count = hstoreUniquePairs(pairs, count, &buflen);
+
+ out = hstorePairs(pairs, count, buflen);
+
+ PG_RETURN_POINTER(out);
+}
+
+/* most of hstore_from_record is shamelessly swiped from record_out */
+
+/*
+ * structure to cache metadata needed for record I/O
+ */
+typedef struct ColumnIOData
+{
+ Oid column_type;
+ Oid typiofunc;
+ Oid typioparam;
+ FmgrInfo proc;
+} ColumnIOData;
+
+typedef struct RecordIOData
+{
+ Oid record_type;
+ int32 record_typmod;
+ /* this field is used only if target type is domain over composite: */
+ void *domain_info; /* opaque cache for domain checks */
+ int ncolumns;
+ ColumnIOData columns[FLEXIBLE_ARRAY_MEMBER];
+} RecordIOData;
+
+PG_FUNCTION_INFO_V1(hstore_from_record);
+Datum
+hstore_from_record(PG_FUNCTION_ARGS)
+{
+ HeapTupleHeader rec;
+ int32 buflen;
+ HStore *out;
+ Pairs *pairs;
+ Oid tupType;
+ int32 tupTypmod;
+ TupleDesc tupdesc;
+ HeapTupleData tuple;
+ RecordIOData *my_extra;
+ int ncolumns;
+ int i,
+ j;
+ Datum *values;
+ bool *nulls;
+
+ if (PG_ARGISNULL(0))
+ {
+ Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
+
+ /*
+ * We have no tuple to look at, so the only source of type info is the
+ * argtype --- which might be domain over composite, but we don't care
+ * here, since we have no need to be concerned about domain
+ * constraints. The lookup_rowtype_tupdesc_domain call below will
+ * error out if we don't have a known composite type oid here.
+ */
+ tupType = argtype;
+ tupTypmod = -1;
+
+ rec = NULL;
+ }
+ else
+ {
+ rec = PG_GETARG_HEAPTUPLEHEADER(0);
+
+ /*
+ * Extract type info from the tuple itself -- this will work even for
+ * anonymous record types.
+ */
+ tupType = HeapTupleHeaderGetTypeId(rec);
+ tupTypmod = HeapTupleHeaderGetTypMod(rec);
+ }
+
+ tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false);
+ ncolumns = tupdesc->natts;
+
+ /*
+ * We arrange to look up the needed I/O info just once per series of
+ * calls, assuming the record type doesn't change underneath us.
+ */
+ my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
+ if (my_extra == NULL ||
+ my_extra->ncolumns != ncolumns)
+ {
+ fcinfo->flinfo->fn_extra =
+ MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
+ offsetof(RecordIOData, columns) +
+ ncolumns * sizeof(ColumnIOData));
+ my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
+ my_extra->record_type = InvalidOid;
+ my_extra->record_typmod = 0;
+ }
+
+ if (my_extra->record_type != tupType ||
+ my_extra->record_typmod != tupTypmod)
+ {
+ MemSet(my_extra, 0,
+ offsetof(RecordIOData, columns) +
+ ncolumns * sizeof(ColumnIOData));
+ my_extra->record_type = tupType;
+ my_extra->record_typmod = tupTypmod;
+ my_extra->ncolumns = ncolumns;
+ }
+
+ Assert(ncolumns <= MaxTupleAttributeNumber); /* thus, no overflow */
+ pairs = palloc(ncolumns * sizeof(Pairs));
+
+ if (rec)
+ {
+ /* Build a temporary HeapTuple control structure */
+ tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
+ ItemPointerSetInvalid(&(tuple.t_self));
+ tuple.t_tableOid = InvalidOid;
+ tuple.t_data = rec;
+
+ values = (Datum *) palloc(ncolumns * sizeof(Datum));
+ nulls = (bool *) palloc(ncolumns * sizeof(bool));
+
+ /* Break down the tuple into fields */
+ heap_deform_tuple(&tuple, tupdesc, values, nulls);
+ }
+ else
+ {
+ values = NULL;
+ nulls = NULL;
+ }
+
+ for (i = 0, j = 0; i < ncolumns; ++i)
+ {
+ ColumnIOData *column_info = &my_extra->columns[i];
+ Form_pg_attribute att = TupleDescAttr(tupdesc, i);
+ Oid column_type = att->atttypid;
+ char *value;
+
+ /* Ignore dropped columns in datatype */
+ if (att->attisdropped)
+ continue;
+
+ pairs[j].key = NameStr(att->attname);
+ pairs[j].keylen = hstoreCheckKeyLen(strlen(NameStr(att->attname)));
+
+ if (!nulls || nulls[i])
+ {
+ pairs[j].val = NULL;
+ pairs[j].vallen = 4;
+ pairs[j].isnull = true;
+ pairs[j].needfree = false;
+ ++j;
+ continue;
+ }
+
+ /*
+ * Convert the column value to text
+ */
+ if (column_info->column_type != column_type)
+ {
+ bool typIsVarlena;
+
+ getTypeOutputInfo(column_type,
+ &column_info->typiofunc,
+ &typIsVarlena);
+ fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
+ fcinfo->flinfo->fn_mcxt);
+ column_info->column_type = column_type;
+ }
+
+ value = OutputFunctionCall(&column_info->proc, values[i]);
+
+ pairs[j].val = value;
+ pairs[j].vallen = hstoreCheckValLen(strlen(value));
+ pairs[j].isnull = false;
+ pairs[j].needfree = false;
+ ++j;
+ }
+
+ ncolumns = hstoreUniquePairs(pairs, j, &buflen);
+
+ out = hstorePairs(pairs, ncolumns, buflen);
+
+ ReleaseTupleDesc(tupdesc);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_populate_record);
+Datum
+hstore_populate_record(PG_FUNCTION_ARGS)
+{
+ Oid argtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
+ HStore *hs;
+ HEntry *entries;
+ char *ptr;
+ HeapTupleHeader rec;
+ Oid tupType;
+ int32 tupTypmod;
+ TupleDesc tupdesc;
+ HeapTupleData tuple;
+ HeapTuple rettuple;
+ RecordIOData *my_extra;
+ int ncolumns;
+ int i;
+ Datum *values;
+ bool *nulls;
+
+ if (!type_is_rowtype(argtype))
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("first argument must be a rowtype")));
+
+ if (PG_ARGISNULL(0))
+ {
+ if (PG_ARGISNULL(1))
+ PG_RETURN_NULL();
+
+ rec = NULL;
+
+ /*
+ * We have no tuple to look at, so the only source of type info is the
+ * argtype. The lookup_rowtype_tupdesc_domain call below will error
+ * out if we don't have a known composite type oid here.
+ */
+ tupType = argtype;
+ tupTypmod = -1;
+ }
+ else
+ {
+ rec = PG_GETARG_HEAPTUPLEHEADER(0);
+
+ if (PG_ARGISNULL(1))
+ PG_RETURN_POINTER(rec);
+
+ /*
+ * Extract type info from the tuple itself -- this will work even for
+ * anonymous record types.
+ */
+ tupType = HeapTupleHeaderGetTypeId(rec);
+ tupTypmod = HeapTupleHeaderGetTypMod(rec);
+ }
+
+ hs = PG_GETARG_HSTORE_P(1);
+ entries = ARRPTR(hs);
+ ptr = STRPTR(hs);
+
+ /*
+ * if the input hstore is empty, we can only skip the rest if we were
+ * passed in a non-null record, since otherwise there may be issues with
+ * domain nulls.
+ */
+
+ if (HS_COUNT(hs) == 0 && rec)
+ PG_RETURN_POINTER(rec);
+
+ /*
+ * Lookup the input record's tupdesc. For the moment, we don't worry
+ * about whether it is a domain over composite.
+ */
+ tupdesc = lookup_rowtype_tupdesc_domain(tupType, tupTypmod, false);
+ ncolumns = tupdesc->natts;
+
+ if (rec)
+ {
+ /* Build a temporary HeapTuple control structure */
+ tuple.t_len = HeapTupleHeaderGetDatumLength(rec);
+ ItemPointerSetInvalid(&(tuple.t_self));
+ tuple.t_tableOid = InvalidOid;
+ tuple.t_data = rec;
+ }
+
+ /*
+ * We arrange to look up the needed I/O info just once per series of
+ * calls, assuming the record type doesn't change underneath us.
+ */
+ my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
+ if (my_extra == NULL ||
+ my_extra->ncolumns != ncolumns)
+ {
+ fcinfo->flinfo->fn_extra =
+ MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
+ offsetof(RecordIOData, columns) +
+ ncolumns * sizeof(ColumnIOData));
+ my_extra = (RecordIOData *) fcinfo->flinfo->fn_extra;
+ my_extra->record_type = InvalidOid;
+ my_extra->record_typmod = 0;
+ my_extra->domain_info = NULL;
+ }
+
+ if (my_extra->record_type != tupType ||
+ my_extra->record_typmod != tupTypmod)
+ {
+ MemSet(my_extra, 0,
+ offsetof(RecordIOData, columns) +
+ ncolumns * sizeof(ColumnIOData));
+ my_extra->record_type = tupType;
+ my_extra->record_typmod = tupTypmod;
+ my_extra->ncolumns = ncolumns;
+ }
+
+ values = (Datum *) palloc(ncolumns * sizeof(Datum));
+ nulls = (bool *) palloc(ncolumns * sizeof(bool));
+
+ if (rec)
+ {
+ /* Break down the tuple into fields */
+ heap_deform_tuple(&tuple, tupdesc, values, nulls);
+ }
+ else
+ {
+ for (i = 0; i < ncolumns; ++i)
+ {
+ values[i] = (Datum) 0;
+ nulls[i] = true;
+ }
+ }
+
+ for (i = 0; i < ncolumns; ++i)
+ {
+ ColumnIOData *column_info = &my_extra->columns[i];
+ Form_pg_attribute att = TupleDescAttr(tupdesc, i);
+ Oid column_type = att->atttypid;
+ char *value;
+ int idx;
+ int vallen;
+
+ /* Ignore dropped columns in datatype */
+ if (att->attisdropped)
+ {
+ nulls[i] = true;
+ continue;
+ }
+
+ idx = hstoreFindKey(hs, 0,
+ NameStr(att->attname),
+ strlen(NameStr(att->attname)));
+
+ /*
+ * we can't just skip here if the key wasn't found since we might have
+ * a domain to deal with. If we were passed in a non-null record
+ * datum, we assume that the existing values are valid (if they're
+ * not, then it's not our fault), but if we were passed in a null,
+ * then every field which we don't populate needs to be run through
+ * the input function just in case it's a domain type.
+ */
+ if (idx < 0 && rec)
+ continue;
+
+ /*
+ * Prepare to convert the column value from text
+ */
+ if (column_info->column_type != column_type)
+ {
+ getTypeInputInfo(column_type,
+ &column_info->typiofunc,
+ &column_info->typioparam);
+ fmgr_info_cxt(column_info->typiofunc, &column_info->proc,
+ fcinfo->flinfo->fn_mcxt);
+ column_info->column_type = column_type;
+ }
+
+ if (idx < 0 || HSTORE_VALISNULL(entries, idx))
+ {
+ /*
+ * need InputFunctionCall to happen even for nulls, so that domain
+ * checks are done
+ */
+ values[i] = InputFunctionCall(&column_info->proc, NULL,
+ column_info->typioparam,
+ att->atttypmod);
+ nulls[i] = true;
+ }
+ else
+ {
+ vallen = HSTORE_VALLEN(entries, idx);
+ value = palloc(1 + vallen);
+ memcpy(value, HSTORE_VAL(entries, ptr, idx), vallen);
+ value[vallen] = 0;
+
+ values[i] = InputFunctionCall(&column_info->proc, value,
+ column_info->typioparam,
+ att->atttypmod);
+ nulls[i] = false;
+ }
+ }
+
+ rettuple = heap_form_tuple(tupdesc, values, nulls);
+
+ /*
+ * If the target type is domain over composite, all we know at this point
+ * is that we've made a valid value of the base composite type. Must
+ * check domain constraints before deciding we're done.
+ */
+ if (argtype != tupdesc->tdtypeid)
+ domain_check(HeapTupleGetDatum(rettuple), false,
+ argtype,
+ &my_extra->domain_info,
+ fcinfo->flinfo->fn_mcxt);
+
+ ReleaseTupleDesc(tupdesc);
+
+ PG_RETURN_DATUM(HeapTupleGetDatum(rettuple));
+}
+
+
+static char *
+cpw(char *dst, char *src, int len)
+{
+ char *ptr = src;
+
+ while (ptr - src < len)
+ {
+ if (*ptr == '"' || *ptr == '\\')
+ *dst++ = '\\';
+ *dst++ = *ptr++;
+ }
+ return dst;
+}
+
+PG_FUNCTION_INFO_V1(hstore_out);
+Datum
+hstore_out(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int buflen,
+ i;
+ int count = HS_COUNT(in);
+ char *out,
+ *ptr;
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+
+ if (count == 0)
+ PG_RETURN_CSTRING(pstrdup(""));
+
+ buflen = 0;
+
+ /*
+ * this loop overestimates due to pessimistic assumptions about escaping,
+ * so very large hstore values can't be output. this could be fixed, but
+ * many other data types probably have the same issue. This replaced code
+ * that used the original varlena size for calculations, which was wrong
+ * in some subtle ways.
+ */
+
+ for (i = 0; i < count; i++)
+ {
+ /* include "" and => and comma-space */
+ buflen += 6 + 2 * HSTORE_KEYLEN(entries, i);
+ /* include "" only if nonnull */
+ buflen += 2 + (HSTORE_VALISNULL(entries, i)
+ ? 2
+ : 2 * HSTORE_VALLEN(entries, i));
+ }
+
+ out = ptr = palloc(buflen);
+
+ for (i = 0; i < count; i++)
+ {
+ *ptr++ = '"';
+ ptr = cpw(ptr, HSTORE_KEY(entries, base, i), HSTORE_KEYLEN(entries, i));
+ *ptr++ = '"';
+ *ptr++ = '=';
+ *ptr++ = '>';
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ *ptr++ = 'N';
+ *ptr++ = 'U';
+ *ptr++ = 'L';
+ *ptr++ = 'L';
+ }
+ else
+ {
+ *ptr++ = '"';
+ ptr = cpw(ptr, HSTORE_VAL(entries, base, i), HSTORE_VALLEN(entries, i));
+ *ptr++ = '"';
+ }
+
+ if (i + 1 != count)
+ {
+ *ptr++ = ',';
+ *ptr++ = ' ';
+ }
+ }
+ *ptr = '\0';
+
+ PG_RETURN_CSTRING(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_send);
+Datum
+hstore_send(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int i;
+ int count = HS_COUNT(in);
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+ StringInfoData buf;
+
+ pq_begintypsend(&buf);
+
+ pq_sendint32(&buf, count);
+
+ for (i = 0; i < count; i++)
+ {
+ int32 keylen = HSTORE_KEYLEN(entries, i);
+
+ pq_sendint32(&buf, keylen);
+ pq_sendtext(&buf, HSTORE_KEY(entries, base, i), keylen);
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ pq_sendint32(&buf, -1);
+ }
+ else
+ {
+ int32 vallen = HSTORE_VALLEN(entries, i);
+
+ pq_sendint32(&buf, vallen);
+ pq_sendtext(&buf, HSTORE_VAL(entries, base, i), vallen);
+ }
+ }
+
+ PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+}
+
+
+/*
+ * hstore_to_json_loose
+ *
+ * This is a heuristic conversion to json which treats
+ * 't' and 'f' as booleans and strings that look like numbers as numbers,
+ * as long as they don't start with a leading zero followed by another digit
+ * (think zip codes or phone numbers starting with 0).
+ */
+PG_FUNCTION_INFO_V1(hstore_to_json_loose);
+Datum
+hstore_to_json_loose(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int i;
+ int count = HS_COUNT(in);
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+ StringInfoData tmp,
+ dst;
+
+ if (count == 0)
+ PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
+
+ initStringInfo(&tmp);
+ initStringInfo(&dst);
+
+ appendStringInfoChar(&dst, '{');
+
+ for (i = 0; i < count; i++)
+ {
+ resetStringInfo(&tmp);
+ appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
+ HSTORE_KEYLEN(entries, i));
+ escape_json(&dst, tmp.data);
+ appendStringInfoString(&dst, ": ");
+ if (HSTORE_VALISNULL(entries, i))
+ appendStringInfoString(&dst, "null");
+ /* guess that values of 't' or 'f' are booleans */
+ else if (HSTORE_VALLEN(entries, i) == 1 &&
+ *(HSTORE_VAL(entries, base, i)) == 't')
+ appendStringInfoString(&dst, "true");
+ else if (HSTORE_VALLEN(entries, i) == 1 &&
+ *(HSTORE_VAL(entries, base, i)) == 'f')
+ appendStringInfoString(&dst, "false");
+ else
+ {
+ resetStringInfo(&tmp);
+ appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
+ HSTORE_VALLEN(entries, i));
+ if (IsValidJsonNumber(tmp.data, tmp.len))
+ appendBinaryStringInfo(&dst, tmp.data, tmp.len);
+ else
+ escape_json(&dst, tmp.data);
+ }
+
+ if (i + 1 != count)
+ appendStringInfoString(&dst, ", ");
+ }
+ appendStringInfoChar(&dst, '}');
+
+ PG_RETURN_TEXT_P(cstring_to_text(dst.data));
+}
+
+PG_FUNCTION_INFO_V1(hstore_to_json);
+Datum
+hstore_to_json(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int i;
+ int count = HS_COUNT(in);
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+ StringInfoData tmp,
+ dst;
+
+ if (count == 0)
+ PG_RETURN_TEXT_P(cstring_to_text_with_len("{}", 2));
+
+ initStringInfo(&tmp);
+ initStringInfo(&dst);
+
+ appendStringInfoChar(&dst, '{');
+
+ for (i = 0; i < count; i++)
+ {
+ resetStringInfo(&tmp);
+ appendBinaryStringInfo(&tmp, HSTORE_KEY(entries, base, i),
+ HSTORE_KEYLEN(entries, i));
+ escape_json(&dst, tmp.data);
+ appendStringInfoString(&dst, ": ");
+ if (HSTORE_VALISNULL(entries, i))
+ appendStringInfoString(&dst, "null");
+ else
+ {
+ resetStringInfo(&tmp);
+ appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
+ HSTORE_VALLEN(entries, i));
+ escape_json(&dst, tmp.data);
+ }
+
+ if (i + 1 != count)
+ appendStringInfoString(&dst, ", ");
+ }
+ appendStringInfoChar(&dst, '}');
+
+ PG_RETURN_TEXT_P(cstring_to_text(dst.data));
+}
+
+PG_FUNCTION_INFO_V1(hstore_to_jsonb);
+Datum
+hstore_to_jsonb(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int i;
+ int count = HS_COUNT(in);
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+ JsonbParseState *state = NULL;
+ JsonbValue *res;
+
+ (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
+
+ for (i = 0; i < count; i++)
+ {
+ JsonbValue key,
+ val;
+
+ key.type = jbvString;
+ key.val.string.len = HSTORE_KEYLEN(entries, i);
+ key.val.string.val = HSTORE_KEY(entries, base, i);
+
+ (void) pushJsonbValue(&state, WJB_KEY, &key);
+
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ val.type = jbvNull;
+ }
+ else
+ {
+ val.type = jbvString;
+ val.val.string.len = HSTORE_VALLEN(entries, i);
+ val.val.string.val = HSTORE_VAL(entries, base, i);
+ }
+ (void) pushJsonbValue(&state, WJB_VALUE, &val);
+ }
+
+ res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
+
+ PG_RETURN_POINTER(JsonbValueToJsonb(res));
+}
+
+PG_FUNCTION_INFO_V1(hstore_to_jsonb_loose);
+Datum
+hstore_to_jsonb_loose(PG_FUNCTION_ARGS)
+{
+ HStore *in = PG_GETARG_HSTORE_P(0);
+ int i;
+ int count = HS_COUNT(in);
+ char *base = STRPTR(in);
+ HEntry *entries = ARRPTR(in);
+ JsonbParseState *state = NULL;
+ JsonbValue *res;
+ StringInfoData tmp;
+
+ initStringInfo(&tmp);
+
+ (void) pushJsonbValue(&state, WJB_BEGIN_OBJECT, NULL);
+
+ for (i = 0; i < count; i++)
+ {
+ JsonbValue key,
+ val;
+
+ key.type = jbvString;
+ key.val.string.len = HSTORE_KEYLEN(entries, i);
+ key.val.string.val = HSTORE_KEY(entries, base, i);
+
+ (void) pushJsonbValue(&state, WJB_KEY, &key);
+
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ val.type = jbvNull;
+ }
+ /* guess that values of 't' or 'f' are booleans */
+ else if (HSTORE_VALLEN(entries, i) == 1 &&
+ *(HSTORE_VAL(entries, base, i)) == 't')
+ {
+ val.type = jbvBool;
+ val.val.boolean = true;
+ }
+ else if (HSTORE_VALLEN(entries, i) == 1 &&
+ *(HSTORE_VAL(entries, base, i)) == 'f')
+ {
+ val.type = jbvBool;
+ val.val.boolean = false;
+ }
+ else
+ {
+ resetStringInfo(&tmp);
+ appendBinaryStringInfo(&tmp, HSTORE_VAL(entries, base, i),
+ HSTORE_VALLEN(entries, i));
+ if (IsValidJsonNumber(tmp.data, tmp.len))
+ {
+ Datum numd;
+
+ val.type = jbvNumeric;
+ numd = DirectFunctionCall3(numeric_in,
+ CStringGetDatum(tmp.data),
+ ObjectIdGetDatum(InvalidOid),
+ Int32GetDatum(-1));
+ val.val.numeric = DatumGetNumeric(numd);
+ }
+ else
+ {
+ val.type = jbvString;
+ val.val.string.len = HSTORE_VALLEN(entries, i);
+ val.val.string.val = HSTORE_VAL(entries, base, i);
+ }
+ }
+ (void) pushJsonbValue(&state, WJB_VALUE, &val);
+ }
+
+ res = pushJsonbValue(&state, WJB_END_OBJECT, NULL);
+
+ PG_RETURN_POINTER(JsonbValueToJsonb(res));
+}
diff --git a/contrib/hstore/hstore_op.c b/contrib/hstore/hstore_op.c
new file mode 100644
index 0000000..dd79d01
--- /dev/null
+++ b/contrib/hstore/hstore_op.c
@@ -0,0 +1,1278 @@
+/*
+ * contrib/hstore/hstore_op.c
+ */
+#include "postgres.h"
+
+#include "access/htup_details.h"
+#include "catalog/pg_type.h"
+#include "common/hashfn.h"
+#include "funcapi.h"
+#include "hstore.h"
+#include "utils/builtins.h"
+#include "utils/memutils.h"
+
+/* old names for C functions */
+HSTORE_POLLUTE(hstore_fetchval, fetchval);
+HSTORE_POLLUTE(hstore_exists, exists);
+HSTORE_POLLUTE(hstore_defined, defined);
+HSTORE_POLLUTE(hstore_delete, delete);
+HSTORE_POLLUTE(hstore_concat, hs_concat);
+HSTORE_POLLUTE(hstore_contains, hs_contains);
+HSTORE_POLLUTE(hstore_contained, hs_contained);
+HSTORE_POLLUTE(hstore_akeys, akeys);
+HSTORE_POLLUTE(hstore_avals, avals);
+HSTORE_POLLUTE(hstore_skeys, skeys);
+HSTORE_POLLUTE(hstore_svals, svals);
+HSTORE_POLLUTE(hstore_each, each);
+
+
+/*
+ * We're often finding a sequence of keys in ascending order. The
+ * "lowbound" parameter is used to cache lower bounds of searches
+ * between calls, based on this assumption. Pass NULL for it for
+ * one-off or unordered searches.
+ */
+int
+hstoreFindKey(HStore *hs, int *lowbound, char *key, int keylen)
+{
+ HEntry *entries = ARRPTR(hs);
+ int stopLow = lowbound ? *lowbound : 0;
+ int stopHigh = HS_COUNT(hs);
+ int stopMiddle;
+ char *base = STRPTR(hs);
+
+ while (stopLow < stopHigh)
+ {
+ int difference;
+
+ stopMiddle = stopLow + (stopHigh - stopLow) / 2;
+
+ if (HSTORE_KEYLEN(entries, stopMiddle) == keylen)
+ difference = memcmp(HSTORE_KEY(entries, base, stopMiddle), key, keylen);
+ else
+ difference = (HSTORE_KEYLEN(entries, stopMiddle) > keylen) ? 1 : -1;
+
+ if (difference == 0)
+ {
+ if (lowbound)
+ *lowbound = stopMiddle + 1;
+ return stopMiddle;
+ }
+ else if (difference < 0)
+ stopLow = stopMiddle + 1;
+ else
+ stopHigh = stopMiddle;
+ }
+
+ if (lowbound)
+ *lowbound = stopLow;
+ return -1;
+}
+
+Pairs *
+hstoreArrayToPairs(ArrayType *a, int *npairs)
+{
+ Datum *key_datums;
+ bool *key_nulls;
+ int key_count;
+ Pairs *key_pairs;
+ int bufsiz;
+ int i,
+ j;
+
+ deconstruct_array(a,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ if (key_count == 0)
+ {
+ *npairs = 0;
+ return NULL;
+ }
+
+ /*
+ * A text array uses at least eight bytes per element, so any overflow in
+ * "key_count * sizeof(Pairs)" is small enough for palloc() to catch.
+ * However, credible improvements to the array format could invalidate
+ * that assumption. Therefore, use an explicit check rather than relying
+ * on palloc() to complain.
+ */
+ if (key_count > MaxAllocSize / sizeof(Pairs))
+ ereport(ERROR,
+ (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+ errmsg("number of pairs (%d) exceeds the maximum allowed (%d)",
+ key_count, (int) (MaxAllocSize / sizeof(Pairs)))));
+
+ key_pairs = palloc(sizeof(Pairs) * key_count);
+
+ for (i = 0, j = 0; i < key_count; i++)
+ {
+ if (!key_nulls[i])
+ {
+ key_pairs[j].key = VARDATA(key_datums[i]);
+ key_pairs[j].keylen = VARSIZE(key_datums[i]) - VARHDRSZ;
+ key_pairs[j].val = NULL;
+ key_pairs[j].vallen = 0;
+ key_pairs[j].needfree = 0;
+ key_pairs[j].isnull = 1;
+ j++;
+ }
+ }
+
+ *npairs = hstoreUniquePairs(key_pairs, j, &bufsiz);
+
+ return key_pairs;
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_fetchval);
+Datum
+hstore_fetchval(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ text *key = PG_GETARG_TEXT_PP(1);
+ HEntry *entries = ARRPTR(hs);
+ text *out;
+ int idx = hstoreFindKey(hs, NULL,
+ VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
+
+ if (idx < 0 || HSTORE_VALISNULL(entries, idx))
+ PG_RETURN_NULL();
+
+ out = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), idx),
+ HSTORE_VALLEN(entries, idx));
+
+ PG_RETURN_TEXT_P(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_exists);
+Datum
+hstore_exists(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ text *key = PG_GETARG_TEXT_PP(1);
+ int idx = hstoreFindKey(hs, NULL,
+ VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
+
+ PG_RETURN_BOOL(idx >= 0);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_exists_any);
+Datum
+hstore_exists_any(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
+ int nkeys;
+ Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
+ int i;
+ int lowbound = 0;
+ bool res = false;
+
+ /*
+ * we exploit the fact that the pairs list is already sorted into strictly
+ * increasing order to narrow the hstoreFindKey search; each search can
+ * start one entry past the previous "found" entry, or at the lower bound
+ * of the last search.
+ */
+ for (i = 0; i < nkeys; i++)
+ {
+ int idx = hstoreFindKey(hs, &lowbound,
+ key_pairs[i].key, key_pairs[i].keylen);
+
+ if (idx >= 0)
+ {
+ res = true;
+ break;
+ }
+ }
+
+ PG_RETURN_BOOL(res);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_exists_all);
+Datum
+hstore_exists_all(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ ArrayType *keys = PG_GETARG_ARRAYTYPE_P(1);
+ int nkeys;
+ Pairs *key_pairs = hstoreArrayToPairs(keys, &nkeys);
+ int i;
+ int lowbound = 0;
+ bool res = true;
+
+ /*
+ * we exploit the fact that the pairs list is already sorted into strictly
+ * increasing order to narrow the hstoreFindKey search; each search can
+ * start one entry past the previous "found" entry, or at the lower bound
+ * of the last search.
+ */
+ for (i = 0; i < nkeys; i++)
+ {
+ int idx = hstoreFindKey(hs, &lowbound,
+ key_pairs[i].key, key_pairs[i].keylen);
+
+ if (idx < 0)
+ {
+ res = false;
+ break;
+ }
+ }
+
+ PG_RETURN_BOOL(res);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_defined);
+Datum
+hstore_defined(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ text *key = PG_GETARG_TEXT_PP(1);
+ HEntry *entries = ARRPTR(hs);
+ int idx = hstoreFindKey(hs, NULL,
+ VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
+ bool res = (idx >= 0 && !HSTORE_VALISNULL(entries, idx));
+
+ PG_RETURN_BOOL(res);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_delete);
+Datum
+hstore_delete(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ text *key = PG_GETARG_TEXT_PP(1);
+ char *keyptr = VARDATA_ANY(key);
+ int keylen = VARSIZE_ANY_EXHDR(key);
+ HStore *out = palloc(VARSIZE(hs));
+ char *bufs,
+ *bufd,
+ *ptrd;
+ HEntry *es,
+ *ed;
+ int i;
+ int count = HS_COUNT(hs);
+ int outcount = 0;
+
+ SET_VARSIZE(out, VARSIZE(hs));
+ HS_SETCOUNT(out, count); /* temporary! */
+
+ bufs = STRPTR(hs);
+ es = ARRPTR(hs);
+ bufd = ptrd = STRPTR(out);
+ ed = ARRPTR(out);
+
+ for (i = 0; i < count; ++i)
+ {
+ int len = HSTORE_KEYLEN(es, i);
+ char *ptrs = HSTORE_KEY(es, bufs, i);
+
+ if (!(len == keylen && memcmp(ptrs, keyptr, keylen) == 0))
+ {
+ int vallen = HSTORE_VALLEN(es, i);
+
+ HS_COPYITEM(ed, bufd, ptrd, ptrs, len, vallen,
+ HSTORE_VALISNULL(es, i));
+ ++outcount;
+ }
+ }
+
+ HS_FINALIZE(out, outcount, bufd, ptrd);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_delete_array);
+Datum
+hstore_delete_array(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ HStore *out = palloc(VARSIZE(hs));
+ int hs_count = HS_COUNT(hs);
+ char *ps,
+ *bufd,
+ *pd;
+ HEntry *es,
+ *ed;
+ int i,
+ j;
+ int outcount = 0;
+ ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
+ int nkeys;
+ Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
+
+ SET_VARSIZE(out, VARSIZE(hs));
+ HS_SETCOUNT(out, hs_count); /* temporary! */
+
+ ps = STRPTR(hs);
+ es = ARRPTR(hs);
+ bufd = pd = STRPTR(out);
+ ed = ARRPTR(out);
+
+ if (nkeys == 0)
+ {
+ /* return a copy of the input, unchanged */
+ memcpy(out, hs, VARSIZE(hs));
+ HS_FIXSIZE(out, hs_count);
+ HS_SETCOUNT(out, hs_count);
+ PG_RETURN_POINTER(out);
+ }
+
+ /*
+ * this is in effect a merge between hs and key_pairs, both of which are
+ * already sorted by (keylen,key); we take keys from hs only
+ */
+
+ for (i = j = 0; i < hs_count;)
+ {
+ int difference;
+
+ if (j >= nkeys)
+ difference = -1;
+ else
+ {
+ int skeylen = HSTORE_KEYLEN(es, i);
+
+ if (skeylen == key_pairs[j].keylen)
+ difference = memcmp(HSTORE_KEY(es, ps, i),
+ key_pairs[j].key,
+ key_pairs[j].keylen);
+ else
+ difference = (skeylen > key_pairs[j].keylen) ? 1 : -1;
+ }
+
+ if (difference > 0)
+ ++j;
+ else if (difference == 0)
+ ++i, ++j;
+ else
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
+ HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
+ ++outcount;
+ ++i;
+ }
+ }
+
+ HS_FINALIZE(out, outcount, bufd, pd);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_delete_hstore);
+Datum
+hstore_delete_hstore(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ HStore *hs2 = PG_GETARG_HSTORE_P(1);
+ HStore *out = palloc(VARSIZE(hs));
+ int hs_count = HS_COUNT(hs);
+ int hs2_count = HS_COUNT(hs2);
+ char *ps,
+ *ps2,
+ *bufd,
+ *pd;
+ HEntry *es,
+ *es2,
+ *ed;
+ int i,
+ j;
+ int outcount = 0;
+
+ SET_VARSIZE(out, VARSIZE(hs));
+ HS_SETCOUNT(out, hs_count); /* temporary! */
+
+ ps = STRPTR(hs);
+ es = ARRPTR(hs);
+ ps2 = STRPTR(hs2);
+ es2 = ARRPTR(hs2);
+ bufd = pd = STRPTR(out);
+ ed = ARRPTR(out);
+
+ if (hs2_count == 0)
+ {
+ /* return a copy of the input, unchanged */
+ memcpy(out, hs, VARSIZE(hs));
+ HS_FIXSIZE(out, hs_count);
+ HS_SETCOUNT(out, hs_count);
+ PG_RETURN_POINTER(out);
+ }
+
+ /*
+ * this is in effect a merge between hs and hs2, both of which are already
+ * sorted by (keylen,key); we take keys from hs only; for equal keys, we
+ * take the value from hs unless the values are equal
+ */
+
+ for (i = j = 0; i < hs_count;)
+ {
+ int difference;
+
+ if (j >= hs2_count)
+ difference = -1;
+ else
+ {
+ int skeylen = HSTORE_KEYLEN(es, i);
+ int s2keylen = HSTORE_KEYLEN(es2, j);
+
+ if (skeylen == s2keylen)
+ difference = memcmp(HSTORE_KEY(es, ps, i),
+ HSTORE_KEY(es2, ps2, j),
+ skeylen);
+ else
+ difference = (skeylen > s2keylen) ? 1 : -1;
+ }
+
+ if (difference > 0)
+ ++j;
+ else if (difference == 0)
+ {
+ int svallen = HSTORE_VALLEN(es, i);
+ int snullval = HSTORE_VALISNULL(es, i);
+
+ if (snullval != HSTORE_VALISNULL(es2, j) ||
+ (!snullval && (svallen != HSTORE_VALLEN(es2, j) ||
+ memcmp(HSTORE_VAL(es, ps, i),
+ HSTORE_VAL(es2, ps2, j),
+ svallen) != 0)))
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
+ svallen, snullval);
+ ++outcount;
+ }
+ ++i, ++j;
+ }
+ else
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es, ps, i), HSTORE_KEYLEN(es, i),
+ HSTORE_VALLEN(es, i), HSTORE_VALISNULL(es, i));
+ ++outcount;
+ ++i;
+ }
+ }
+
+ HS_FINALIZE(out, outcount, bufd, pd);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_concat);
+Datum
+hstore_concat(PG_FUNCTION_ARGS)
+{
+ HStore *s1 = PG_GETARG_HSTORE_P(0);
+ HStore *s2 = PG_GETARG_HSTORE_P(1);
+ HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
+ char *ps1,
+ *ps2,
+ *bufd,
+ *pd;
+ HEntry *es1,
+ *es2,
+ *ed;
+ int s1idx;
+ int s2idx;
+ int s1count = HS_COUNT(s1);
+ int s2count = HS_COUNT(s2);
+ int outcount = 0;
+
+ SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2) - HSHRDSIZE);
+ HS_SETCOUNT(out, s1count + s2count);
+
+ if (s1count == 0)
+ {
+ /* return a copy of the input, unchanged */
+ memcpy(out, s2, VARSIZE(s2));
+ HS_FIXSIZE(out, s2count);
+ HS_SETCOUNT(out, s2count);
+ PG_RETURN_POINTER(out);
+ }
+
+ if (s2count == 0)
+ {
+ /* return a copy of the input, unchanged */
+ memcpy(out, s1, VARSIZE(s1));
+ HS_FIXSIZE(out, s1count);
+ HS_SETCOUNT(out, s1count);
+ PG_RETURN_POINTER(out);
+ }
+
+ ps1 = STRPTR(s1);
+ ps2 = STRPTR(s2);
+ bufd = pd = STRPTR(out);
+ es1 = ARRPTR(s1);
+ es2 = ARRPTR(s2);
+ ed = ARRPTR(out);
+
+ /*
+ * this is in effect a merge between s1 and s2, both of which are already
+ * sorted by (keylen,key); we take s2 for equal keys
+ */
+
+ for (s1idx = s2idx = 0; s1idx < s1count || s2idx < s2count; ++outcount)
+ {
+ int difference;
+
+ if (s1idx >= s1count)
+ difference = 1;
+ else if (s2idx >= s2count)
+ difference = -1;
+ else
+ {
+ int s1keylen = HSTORE_KEYLEN(es1, s1idx);
+ int s2keylen = HSTORE_KEYLEN(es2, s2idx);
+
+ if (s1keylen == s2keylen)
+ difference = memcmp(HSTORE_KEY(es1, ps1, s1idx),
+ HSTORE_KEY(es2, ps2, s2idx),
+ s1keylen);
+ else
+ difference = (s1keylen > s2keylen) ? 1 : -1;
+ }
+
+ if (difference >= 0)
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es2, ps2, s2idx), HSTORE_KEYLEN(es2, s2idx),
+ HSTORE_VALLEN(es2, s2idx), HSTORE_VALISNULL(es2, s2idx));
+ ++s2idx;
+ if (difference == 0)
+ ++s1idx;
+ }
+ else
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es1, ps1, s1idx), HSTORE_KEYLEN(es1, s1idx),
+ HSTORE_VALLEN(es1, s1idx), HSTORE_VALISNULL(es1, s1idx));
+ ++s1idx;
+ }
+ }
+
+ HS_FINALIZE(out, outcount, bufd, pd);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_slice_to_array);
+Datum
+hstore_slice_to_array(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ HEntry *entries = ARRPTR(hs);
+ char *ptr = STRPTR(hs);
+ ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
+ ArrayType *aout;
+ Datum *key_datums;
+ bool *key_nulls;
+ Datum *out_datums;
+ bool *out_nulls;
+ int key_count;
+ int i;
+
+ deconstruct_array(key_array,
+ TEXTOID, -1, false, TYPALIGN_INT,
+ &key_datums, &key_nulls, &key_count);
+
+ if (key_count == 0)
+ {
+ aout = construct_empty_array(TEXTOID);
+ PG_RETURN_POINTER(aout);
+ }
+
+ out_datums = palloc(sizeof(Datum) * key_count);
+ out_nulls = palloc(sizeof(bool) * key_count);
+
+ for (i = 0; i < key_count; ++i)
+ {
+ text *key = (text *) DatumGetPointer(key_datums[i]);
+ int idx;
+
+ if (key_nulls[i])
+ idx = -1;
+ else
+ idx = hstoreFindKey(hs, NULL, VARDATA(key), VARSIZE(key) - VARHDRSZ);
+
+ if (idx < 0 || HSTORE_VALISNULL(entries, idx))
+ {
+ out_nulls[i] = true;
+ out_datums[i] = (Datum) 0;
+ }
+ else
+ {
+ out_datums[i] =
+ PointerGetDatum(cstring_to_text_with_len(HSTORE_VAL(entries, ptr, idx),
+ HSTORE_VALLEN(entries, idx)));
+ out_nulls[i] = false;
+ }
+ }
+
+ aout = construct_md_array(out_datums, out_nulls,
+ ARR_NDIM(key_array),
+ ARR_DIMS(key_array),
+ ARR_LBOUND(key_array),
+ TEXTOID, -1, false, TYPALIGN_INT);
+
+ PG_RETURN_POINTER(aout);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_slice_to_hstore);
+Datum
+hstore_slice_to_hstore(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ HEntry *entries = ARRPTR(hs);
+ char *ptr = STRPTR(hs);
+ ArrayType *key_array = PG_GETARG_ARRAYTYPE_P(1);
+ HStore *out;
+ int nkeys;
+ Pairs *key_pairs = hstoreArrayToPairs(key_array, &nkeys);
+ Pairs *out_pairs;
+ int bufsiz;
+ int lastidx = 0;
+ int i;
+ int out_count = 0;
+
+ if (nkeys == 0)
+ {
+ out = hstorePairs(NULL, 0, 0);
+ PG_RETURN_POINTER(out);
+ }
+
+ /* hstoreArrayToPairs() checked overflow */
+ out_pairs = palloc(sizeof(Pairs) * nkeys);
+ bufsiz = 0;
+
+ /*
+ * we exploit the fact that the pairs list is already sorted into strictly
+ * increasing order to narrow the hstoreFindKey search; each search can
+ * start one entry past the previous "found" entry, or at the lower bound
+ * of the last search.
+ */
+
+ for (i = 0; i < nkeys; ++i)
+ {
+ int idx = hstoreFindKey(hs, &lastidx,
+ key_pairs[i].key, key_pairs[i].keylen);
+
+ if (idx >= 0)
+ {
+ out_pairs[out_count].key = key_pairs[i].key;
+ bufsiz += (out_pairs[out_count].keylen = key_pairs[i].keylen);
+ out_pairs[out_count].val = HSTORE_VAL(entries, ptr, idx);
+ bufsiz += (out_pairs[out_count].vallen = HSTORE_VALLEN(entries, idx));
+ out_pairs[out_count].isnull = HSTORE_VALISNULL(entries, idx);
+ out_pairs[out_count].needfree = false;
+ ++out_count;
+ }
+ }
+
+ /*
+ * we don't use hstoreUniquePairs here because we know that the pairs list
+ * is already sorted and uniq'ed.
+ */
+
+ out = hstorePairs(out_pairs, out_count, bufsiz);
+
+ PG_RETURN_POINTER(out);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_akeys);
+Datum
+hstore_akeys(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ Datum *d;
+ ArrayType *a;
+ HEntry *entries = ARRPTR(hs);
+ char *base = STRPTR(hs);
+ int count = HS_COUNT(hs);
+ int i;
+
+ if (count == 0)
+ {
+ a = construct_empty_array(TEXTOID);
+ PG_RETURN_POINTER(a);
+ }
+
+ d = (Datum *) palloc(sizeof(Datum) * count);
+
+ for (i = 0; i < count; ++i)
+ {
+ text *t = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
+ HSTORE_KEYLEN(entries, i));
+
+ d[i] = PointerGetDatum(t);
+ }
+
+ a = construct_array(d, count,
+ TEXTOID, -1, false, TYPALIGN_INT);
+
+ PG_RETURN_POINTER(a);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_avals);
+Datum
+hstore_avals(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ Datum *d;
+ bool *nulls;
+ ArrayType *a;
+ HEntry *entries = ARRPTR(hs);
+ char *base = STRPTR(hs);
+ int count = HS_COUNT(hs);
+ int lb = 1;
+ int i;
+
+ if (count == 0)
+ {
+ a = construct_empty_array(TEXTOID);
+ PG_RETURN_POINTER(a);
+ }
+
+ d = (Datum *) palloc(sizeof(Datum) * count);
+ nulls = (bool *) palloc(sizeof(bool) * count);
+
+ for (i = 0; i < count; ++i)
+ {
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ d[i] = (Datum) 0;
+ nulls[i] = true;
+ }
+ else
+ {
+ text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
+ HSTORE_VALLEN(entries, i));
+
+ d[i] = PointerGetDatum(item);
+ nulls[i] = false;
+ }
+ }
+
+ a = construct_md_array(d, nulls, 1, &count, &lb,
+ TEXTOID, -1, false, TYPALIGN_INT);
+
+ PG_RETURN_POINTER(a);
+}
+
+
+static ArrayType *
+hstore_to_array_internal(HStore *hs, int ndims)
+{
+ HEntry *entries = ARRPTR(hs);
+ char *base = STRPTR(hs);
+ int count = HS_COUNT(hs);
+ int out_size[2] = {0, 2};
+ int lb[2] = {1, 1};
+ Datum *out_datums;
+ bool *out_nulls;
+ int i;
+
+ Assert(ndims < 3);
+
+ if (count == 0 || ndims == 0)
+ return construct_empty_array(TEXTOID);
+
+ out_size[0] = count * 2 / ndims;
+ out_datums = palloc(sizeof(Datum) * count * 2);
+ out_nulls = palloc(sizeof(bool) * count * 2);
+
+ for (i = 0; i < count; ++i)
+ {
+ text *key = cstring_to_text_with_len(HSTORE_KEY(entries, base, i),
+ HSTORE_KEYLEN(entries, i));
+
+ out_datums[i * 2] = PointerGetDatum(key);
+ out_nulls[i * 2] = false;
+
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ out_datums[i * 2 + 1] = (Datum) 0;
+ out_nulls[i * 2 + 1] = true;
+ }
+ else
+ {
+ text *item = cstring_to_text_with_len(HSTORE_VAL(entries, base, i),
+ HSTORE_VALLEN(entries, i));
+
+ out_datums[i * 2 + 1] = PointerGetDatum(item);
+ out_nulls[i * 2 + 1] = false;
+ }
+ }
+
+ return construct_md_array(out_datums, out_nulls,
+ ndims, out_size, lb,
+ TEXTOID, -1, false, TYPALIGN_INT);
+}
+
+PG_FUNCTION_INFO_V1(hstore_to_array);
+Datum
+hstore_to_array(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ ArrayType *out = hstore_to_array_internal(hs, 1);
+
+ PG_RETURN_POINTER(out);
+}
+
+PG_FUNCTION_INFO_V1(hstore_to_matrix);
+Datum
+hstore_to_matrix(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ ArrayType *out = hstore_to_array_internal(hs, 2);
+
+ PG_RETURN_POINTER(out);
+}
+
+/*
+ * Common initialization function for the various set-returning
+ * funcs. fcinfo is only passed if the function is to return a
+ * composite; it will be used to look up the return tupledesc.
+ * we stash a copy of the hstore in the multi-call context in
+ * case it was originally toasted. (At least I assume that's why;
+ * there was no explanatory comment in the original code. --AG)
+ */
+
+static void
+setup_firstcall(FuncCallContext *funcctx, HStore *hs,
+ FunctionCallInfo fcinfo)
+{
+ MemoryContext oldcontext;
+ HStore *st;
+
+ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+
+ st = (HStore *) palloc(VARSIZE(hs));
+ memcpy(st, hs, VARSIZE(hs));
+
+ funcctx->user_fctx = (void *) st;
+
+ if (fcinfo)
+ {
+ TupleDesc tupdesc;
+
+ /* Build a tuple descriptor for our result type */
+ if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+ elog(ERROR, "return type must be a row type");
+
+ funcctx->tuple_desc = BlessTupleDesc(tupdesc);
+ }
+
+ MemoryContextSwitchTo(oldcontext);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_skeys);
+Datum
+hstore_skeys(PG_FUNCTION_ARGS)
+{
+ FuncCallContext *funcctx;
+ HStore *hs;
+ int i;
+
+ if (SRF_IS_FIRSTCALL())
+ {
+ hs = PG_GETARG_HSTORE_P(0);
+ funcctx = SRF_FIRSTCALL_INIT();
+ setup_firstcall(funcctx, hs, NULL);
+ }
+
+ funcctx = SRF_PERCALL_SETUP();
+ hs = (HStore *) funcctx->user_fctx;
+ i = funcctx->call_cntr;
+
+ if (i < HS_COUNT(hs))
+ {
+ HEntry *entries = ARRPTR(hs);
+ text *item;
+
+ item = cstring_to_text_with_len(HSTORE_KEY(entries, STRPTR(hs), i),
+ HSTORE_KEYLEN(entries, i));
+
+ SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
+ }
+
+ SRF_RETURN_DONE(funcctx);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_svals);
+Datum
+hstore_svals(PG_FUNCTION_ARGS)
+{
+ FuncCallContext *funcctx;
+ HStore *hs;
+ int i;
+
+ if (SRF_IS_FIRSTCALL())
+ {
+ hs = PG_GETARG_HSTORE_P(0);
+ funcctx = SRF_FIRSTCALL_INIT();
+ setup_firstcall(funcctx, hs, NULL);
+ }
+
+ funcctx = SRF_PERCALL_SETUP();
+ hs = (HStore *) funcctx->user_fctx;
+ i = funcctx->call_cntr;
+
+ if (i < HS_COUNT(hs))
+ {
+ HEntry *entries = ARRPTR(hs);
+
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ ReturnSetInfo *rsi;
+
+ /* ugly ugly ugly. why no macro for this? */
+ (funcctx)->call_cntr++;
+ rsi = (ReturnSetInfo *) fcinfo->resultinfo;
+ rsi->isDone = ExprMultipleResult;
+ PG_RETURN_NULL();
+ }
+ else
+ {
+ text *item;
+
+ item = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), i),
+ HSTORE_VALLEN(entries, i));
+
+ SRF_RETURN_NEXT(funcctx, PointerGetDatum(item));
+ }
+ }
+
+ SRF_RETURN_DONE(funcctx);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_contains);
+Datum
+hstore_contains(PG_FUNCTION_ARGS)
+{
+ HStore *val = PG_GETARG_HSTORE_P(0);
+ HStore *tmpl = PG_GETARG_HSTORE_P(1);
+ bool res = true;
+ HEntry *te = ARRPTR(tmpl);
+ char *tstr = STRPTR(tmpl);
+ HEntry *ve = ARRPTR(val);
+ char *vstr = STRPTR(val);
+ int tcount = HS_COUNT(tmpl);
+ int lastidx = 0;
+ int i;
+
+ /*
+ * we exploit the fact that keys in "tmpl" are in strictly increasing
+ * order to narrow the hstoreFindKey search; each search can start one
+ * entry past the previous "found" entry, or at the lower bound of the
+ * search
+ */
+
+ for (i = 0; res && i < tcount; ++i)
+ {
+ int idx = hstoreFindKey(val, &lastidx,
+ HSTORE_KEY(te, tstr, i),
+ HSTORE_KEYLEN(te, i));
+
+ if (idx >= 0)
+ {
+ bool nullval = HSTORE_VALISNULL(te, i);
+ int vallen = HSTORE_VALLEN(te, i);
+
+ if (nullval != HSTORE_VALISNULL(ve, idx) ||
+ (!nullval && (vallen != HSTORE_VALLEN(ve, idx) ||
+ memcmp(HSTORE_VAL(te, tstr, i),
+ HSTORE_VAL(ve, vstr, idx),
+ vallen) != 0)))
+ res = false;
+ }
+ else
+ res = false;
+ }
+
+ PG_RETURN_BOOL(res);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_contained);
+Datum
+hstore_contained(PG_FUNCTION_ARGS)
+{
+ PG_RETURN_DATUM(DirectFunctionCall2(hstore_contains,
+ PG_GETARG_DATUM(1),
+ PG_GETARG_DATUM(0)
+ ));
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_each);
+Datum
+hstore_each(PG_FUNCTION_ARGS)
+{
+ FuncCallContext *funcctx;
+ HStore *hs;
+ int i;
+
+ if (SRF_IS_FIRSTCALL())
+ {
+ hs = PG_GETARG_HSTORE_P(0);
+ funcctx = SRF_FIRSTCALL_INIT();
+ setup_firstcall(funcctx, hs, fcinfo);
+ }
+
+ funcctx = SRF_PERCALL_SETUP();
+ hs = (HStore *) funcctx->user_fctx;
+ i = funcctx->call_cntr;
+
+ if (i < HS_COUNT(hs))
+ {
+ HEntry *entries = ARRPTR(hs);
+ char *ptr = STRPTR(hs);
+ Datum res,
+ dvalues[2];
+ bool nulls[2] = {false, false};
+ text *item;
+ HeapTuple tuple;
+
+ item = cstring_to_text_with_len(HSTORE_KEY(entries, ptr, i),
+ HSTORE_KEYLEN(entries, i));
+ dvalues[0] = PointerGetDatum(item);
+
+ if (HSTORE_VALISNULL(entries, i))
+ {
+ dvalues[1] = (Datum) 0;
+ nulls[1] = true;
+ }
+ else
+ {
+ item = cstring_to_text_with_len(HSTORE_VAL(entries, ptr, i),
+ HSTORE_VALLEN(entries, i));
+ dvalues[1] = PointerGetDatum(item);
+ }
+
+ tuple = heap_form_tuple(funcctx->tuple_desc, dvalues, nulls);
+ res = HeapTupleGetDatum(tuple);
+
+ SRF_RETURN_NEXT(funcctx, PointerGetDatum(res));
+ }
+
+ SRF_RETURN_DONE(funcctx);
+}
+
+
+/*
+ * btree sort order for hstores isn't intended to be useful; we really only
+ * care about equality versus non-equality. we compare the entire string
+ * buffer first, then the entry pos array.
+ */
+
+PG_FUNCTION_INFO_V1(hstore_cmp);
+Datum
+hstore_cmp(PG_FUNCTION_ARGS)
+{
+ HStore *hs1 = PG_GETARG_HSTORE_P(0);
+ HStore *hs2 = PG_GETARG_HSTORE_P(1);
+ int hcount1 = HS_COUNT(hs1);
+ int hcount2 = HS_COUNT(hs2);
+ int res = 0;
+
+ if (hcount1 == 0 || hcount2 == 0)
+ {
+ /*
+ * if either operand is empty, and the other is nonempty, the nonempty
+ * one is larger. If both are empty they are equal.
+ */
+ if (hcount1 > 0)
+ res = 1;
+ else if (hcount2 > 0)
+ res = -1;
+ }
+ else
+ {
+ /* here we know both operands are nonempty */
+ char *str1 = STRPTR(hs1);
+ char *str2 = STRPTR(hs2);
+ HEntry *ent1 = ARRPTR(hs1);
+ HEntry *ent2 = ARRPTR(hs2);
+ size_t len1 = HSE_ENDPOS(ent1[2 * hcount1 - 1]);
+ size_t len2 = HSE_ENDPOS(ent2[2 * hcount2 - 1]);
+
+ res = memcmp(str1, str2, Min(len1, len2));
+
+ if (res == 0)
+ {
+ if (len1 > len2)
+ res = 1;
+ else if (len1 < len2)
+ res = -1;
+ else if (hcount1 > hcount2)
+ res = 1;
+ else if (hcount2 > hcount1)
+ res = -1;
+ else
+ {
+ int count = hcount1 * 2;
+ int i;
+
+ for (i = 0; i < count; ++i)
+ if (HSE_ENDPOS(ent1[i]) != HSE_ENDPOS(ent2[i]) ||
+ HSE_ISNULL(ent1[i]) != HSE_ISNULL(ent2[i]))
+ break;
+ if (i < count)
+ {
+ if (HSE_ENDPOS(ent1[i]) < HSE_ENDPOS(ent2[i]))
+ res = -1;
+ else if (HSE_ENDPOS(ent1[i]) > HSE_ENDPOS(ent2[i]))
+ res = 1;
+ else if (HSE_ISNULL(ent1[i]))
+ res = 1;
+ else if (HSE_ISNULL(ent2[i]))
+ res = -1;
+ }
+ }
+ }
+ else
+ {
+ res = (res > 0) ? 1 : -1;
+ }
+ }
+
+ /*
+ * this is a btree support function; this is one of the few places where
+ * memory needs to be explicitly freed.
+ */
+ PG_FREE_IF_COPY(hs1, 0);
+ PG_FREE_IF_COPY(hs2, 1);
+ PG_RETURN_INT32(res);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_eq);
+Datum
+hstore_eq(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res == 0);
+}
+
+PG_FUNCTION_INFO_V1(hstore_ne);
+Datum
+hstore_ne(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res != 0);
+}
+
+PG_FUNCTION_INFO_V1(hstore_gt);
+Datum
+hstore_gt(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res > 0);
+}
+
+PG_FUNCTION_INFO_V1(hstore_ge);
+Datum
+hstore_ge(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res >= 0);
+}
+
+PG_FUNCTION_INFO_V1(hstore_lt);
+Datum
+hstore_lt(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res < 0);
+}
+
+PG_FUNCTION_INFO_V1(hstore_le);
+Datum
+hstore_le(PG_FUNCTION_ARGS)
+{
+ int res = DatumGetInt32(DirectFunctionCall2(hstore_cmp,
+ PG_GETARG_DATUM(0),
+ PG_GETARG_DATUM(1)));
+
+ PG_RETURN_BOOL(res <= 0);
+}
+
+
+PG_FUNCTION_INFO_V1(hstore_hash);
+Datum
+hstore_hash(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ Datum hval = hash_any((unsigned char *) VARDATA(hs),
+ VARSIZE(hs) - VARHDRSZ);
+
+ /*
+ * This (along with hstore_hash_extended) is the only place in the code
+ * that cares whether the overall varlena size exactly matches the true
+ * data size; this assertion should be maintained by all the other code,
+ * but we make it explicit here.
+ */
+ Assert(VARSIZE(hs) ==
+ (HS_COUNT(hs) != 0 ?
+ CALCDATASIZE(HS_COUNT(hs),
+ HSE_ENDPOS(ARRPTR(hs)[2 * HS_COUNT(hs) - 1])) :
+ HSHRDSIZE));
+
+ PG_FREE_IF_COPY(hs, 0);
+ PG_RETURN_DATUM(hval);
+}
+
+PG_FUNCTION_INFO_V1(hstore_hash_extended);
+Datum
+hstore_hash_extended(PG_FUNCTION_ARGS)
+{
+ HStore *hs = PG_GETARG_HSTORE_P(0);
+ uint64 seed = PG_GETARG_INT64(1);
+ Datum hval;
+
+ hval = hash_any_extended((unsigned char *) VARDATA(hs),
+ VARSIZE(hs) - VARHDRSZ,
+ seed);
+
+ /* See comment in hstore_hash */
+ Assert(VARSIZE(hs) ==
+ (HS_COUNT(hs) != 0 ?
+ CALCDATASIZE(HS_COUNT(hs),
+ HSE_ENDPOS(ARRPTR(hs)[2 * HS_COUNT(hs) - 1])) :
+ HSHRDSIZE));
+
+ PG_FREE_IF_COPY(hs, 0);
+ PG_RETURN_DATUM(hval);
+}
diff --git a/contrib/hstore/hstore_subs.c b/contrib/hstore/hstore_subs.c
new file mode 100644
index 0000000..ca4c174
--- /dev/null
+++ b/contrib/hstore/hstore_subs.c
@@ -0,0 +1,297 @@
+/*-------------------------------------------------------------------------
+ *
+ * hstore_subs.c
+ * Subscripting support functions for hstore.
+ *
+ * This is a great deal simpler than array_subs.c, because the result of
+ * subscripting an hstore is just a text string (the value for the key).
+ * We do not need to support array slicing notation, nor multiple subscripts.
+ * Less obviously, because the subscript result is never a SQL container
+ * type, there will never be any nested-assignment scenarios, so we do not
+ * need a fetch_old function. In turn, that means we can drop the
+ * check_subscripts function and just let the fetch and assign functions
+ * do everything.
+ *
+ * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ *
+ * IDENTIFICATION
+ * contrib/hstore/hstore_subs.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "executor/execExpr.h"
+#include "hstore.h"
+#include "nodes/nodeFuncs.h"
+#include "nodes/subscripting.h"
+#include "parser/parse_coerce.h"
+#include "parser/parse_expr.h"
+#include "utils/builtins.h"
+
+
+/*
+ * Finish parse analysis of a SubscriptingRef expression for hstore.
+ *
+ * Verify there's just one subscript, coerce it to text,
+ * and set the result type of the SubscriptingRef node.
+ */
+static void
+hstore_subscript_transform(SubscriptingRef *sbsref,
+ List *indirection,
+ ParseState *pstate,
+ bool isSlice,
+ bool isAssignment)
+{
+ A_Indices *ai;
+ Node *subexpr;
+
+ /* We support only single-subscript, non-slice cases */
+ if (isSlice || list_length(indirection) != 1)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("hstore allows only one subscript"),
+ parser_errposition(pstate,
+ exprLocation((Node *) indirection))));
+
+ /* Transform the subscript expression to type text */
+ ai = linitial_node(A_Indices, indirection);
+ Assert(ai->uidx != NULL && ai->lidx == NULL && !ai->is_slice);
+
+ subexpr = transformExpr(pstate, ai->uidx, pstate->p_expr_kind);
+ /* If it's not text already, try to coerce */
+ subexpr = coerce_to_target_type(pstate,
+ subexpr, exprType(subexpr),
+ TEXTOID, -1,
+ COERCION_ASSIGNMENT,
+ COERCE_IMPLICIT_CAST,
+ -1);
+ if (subexpr == NULL)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("hstore subscript must have type text"),
+ parser_errposition(pstate, exprLocation(ai->uidx))));
+
+ /* ... and store the transformed subscript into the SubscriptRef node */
+ sbsref->refupperindexpr = list_make1(subexpr);
+ sbsref->reflowerindexpr = NIL;
+
+ /* Determine the result type of the subscripting operation; always text */
+ sbsref->refrestype = TEXTOID;
+ sbsref->reftypmod = -1;
+}
+
+/*
+ * Evaluate SubscriptingRef fetch for hstore.
+ *
+ * Source container is in step's result variable (it's known not NULL, since
+ * we set fetch_strict to true), and the subscript expression is in the
+ * upperindex[] array.
+ */
+static void
+hstore_subscript_fetch(ExprState *state,
+ ExprEvalStep *op,
+ ExprContext *econtext)
+{
+ SubscriptingRefState *sbsrefstate = op->d.sbsref.state;
+ HStore *hs;
+ text *key;
+ HEntry *entries;
+ int idx;
+ text *out;
+
+ /* Should not get here if source hstore is null */
+ Assert(!(*op->resnull));
+
+ /* Check for null subscript */
+ if (sbsrefstate->upperindexnull[0])
+ {
+ *op->resnull = true;
+ return;
+ }
+
+ /* OK, fetch/detoast the hstore and subscript */
+ hs = DatumGetHStoreP(*op->resvalue);
+ key = DatumGetTextPP(sbsrefstate->upperindex[0]);
+
+ /* The rest is basically the same as hstore_fetchval() */
+ entries = ARRPTR(hs);
+ idx = hstoreFindKey(hs, NULL,
+ VARDATA_ANY(key), VARSIZE_ANY_EXHDR(key));
+
+ if (idx < 0 || HSTORE_VALISNULL(entries, idx))
+ {
+ *op->resnull = true;
+ return;
+ }
+
+ out = cstring_to_text_with_len(HSTORE_VAL(entries, STRPTR(hs), idx),
+ HSTORE_VALLEN(entries, idx));
+
+ *op->resvalue = PointerGetDatum(out);
+}
+
+/*
+ * Evaluate SubscriptingRef assignment for hstore.
+ *
+ * Input container (possibly null) is in result area, replacement value is in
+ * SubscriptingRefState's replacevalue/replacenull.
+ */
+static void
+hstore_subscript_assign(ExprState *state,
+ ExprEvalStep *op,
+ ExprContext *econtext)
+{
+ SubscriptingRefState *sbsrefstate = op->d.sbsref.state;
+ text *key;
+ Pairs p;
+ HStore *out;
+
+ /* Check for null subscript */
+ if (sbsrefstate->upperindexnull[0])
+ ereport(ERROR,
+ (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
+ errmsg("hstore subscript in assignment must not be null")));
+
+ /* OK, fetch/detoast the subscript */
+ key = DatumGetTextPP(sbsrefstate->upperindex[0]);
+
+ /* Create a Pairs entry for subscript + replacement value */
+ p.needfree = false;
+ p.key = VARDATA_ANY(key);
+ p.keylen = hstoreCheckKeyLen(VARSIZE_ANY_EXHDR(key));
+
+ if (sbsrefstate->replacenull)
+ {
+ p.vallen = 0;
+ p.isnull = true;
+ }
+ else
+ {
+ text *val = DatumGetTextPP(sbsrefstate->replacevalue);
+
+ p.val = VARDATA_ANY(val);
+ p.vallen = hstoreCheckValLen(VARSIZE_ANY_EXHDR(val));
+ p.isnull = false;
+ }
+
+ if (*op->resnull)
+ {
+ /* Just build a one-element hstore (cf. hstore_from_text) */
+ out = hstorePairs(&p, 1, p.keylen + p.vallen);
+ }
+ else
+ {
+ /*
+ * Otherwise, merge the new key into the hstore. Based on
+ * hstore_concat.
+ */
+ HStore *hs = DatumGetHStoreP(*op->resvalue);
+ int s1count = HS_COUNT(hs);
+ int outcount = 0;
+ int vsize;
+ char *ps1,
+ *bufd,
+ *pd;
+ HEntry *es1,
+ *ed;
+ int s1idx;
+ int s2idx;
+
+ /* Allocate result without considering possibility of duplicate */
+ vsize = CALCDATASIZE(s1count + 1, VARSIZE(hs) + p.keylen + p.vallen);
+ out = palloc(vsize);
+ SET_VARSIZE(out, vsize);
+ HS_SETCOUNT(out, s1count + 1);
+
+ ps1 = STRPTR(hs);
+ bufd = pd = STRPTR(out);
+ es1 = ARRPTR(hs);
+ ed = ARRPTR(out);
+
+ for (s1idx = s2idx = 0; s1idx < s1count || s2idx < 1; ++outcount)
+ {
+ int difference;
+
+ if (s1idx >= s1count)
+ difference = 1;
+ else if (s2idx >= 1)
+ difference = -1;
+ else
+ {
+ int s1keylen = HSTORE_KEYLEN(es1, s1idx);
+ int s2keylen = p.keylen;
+
+ if (s1keylen == s2keylen)
+ difference = memcmp(HSTORE_KEY(es1, ps1, s1idx),
+ p.key,
+ s1keylen);
+ else
+ difference = (s1keylen > s2keylen) ? 1 : -1;
+ }
+
+ if (difference >= 0)
+ {
+ HS_ADDITEM(ed, bufd, pd, p);
+ ++s2idx;
+ if (difference == 0)
+ ++s1idx;
+ }
+ else
+ {
+ HS_COPYITEM(ed, bufd, pd,
+ HSTORE_KEY(es1, ps1, s1idx),
+ HSTORE_KEYLEN(es1, s1idx),
+ HSTORE_VALLEN(es1, s1idx),
+ HSTORE_VALISNULL(es1, s1idx));
+ ++s1idx;
+ }
+ }
+
+ HS_FINALIZE(out, outcount, bufd, pd);
+ }
+
+ *op->resvalue = PointerGetDatum(out);
+ *op->resnull = false;
+}
+
+/*
+ * Set up execution state for an hstore subscript operation.
+ */
+static void
+hstore_exec_setup(const SubscriptingRef *sbsref,
+ SubscriptingRefState *sbsrefstate,
+ SubscriptExecSteps *methods)
+{
+ /* Assert we are dealing with one subscript */
+ Assert(sbsrefstate->numlower == 0);
+ Assert(sbsrefstate->numupper == 1);
+ /* We can't check upperprovided[0] here, but it must be true */
+
+ /* Pass back pointers to appropriate step execution functions */
+ methods->sbs_check_subscripts = NULL;
+ methods->sbs_fetch = hstore_subscript_fetch;
+ methods->sbs_assign = hstore_subscript_assign;
+ methods->sbs_fetch_old = NULL;
+}
+
+/*
+ * hstore_subscript_handler
+ * Subscripting handler for hstore.
+ */
+PG_FUNCTION_INFO_V1(hstore_subscript_handler);
+Datum
+hstore_subscript_handler(PG_FUNCTION_ARGS)
+{
+ static const SubscriptRoutines sbsroutines = {
+ .transform = hstore_subscript_transform,
+ .exec_setup = hstore_exec_setup,
+ .fetch_strict = true, /* fetch returns NULL for NULL inputs */
+ .fetch_leakproof = true, /* fetch returns NULL for bad subscript */
+ .store_leakproof = false /* ... but assignment throws error */
+ };
+
+ PG_RETURN_POINTER(&sbsroutines);
+}
diff --git a/contrib/hstore/sql/hstore.sql b/contrib/hstore/sql/hstore.sql
new file mode 100644
index 0000000..a59db66
--- /dev/null
+++ b/contrib/hstore/sql/hstore.sql
@@ -0,0 +1,386 @@
+CREATE EXTENSION hstore;
+
+-- Check whether any of our opclasses fail amvalidate
+SELECT amname, opcname
+FROM pg_opclass opc LEFT JOIN pg_am am ON am.oid = opcmethod
+WHERE opc.oid >= 16384 AND NOT amvalidate(opc.oid);
+
+set escape_string_warning=off;
+
+--hstore;
+
+select ''::hstore;
+select 'a=>b'::hstore;
+select ' a=>b'::hstore;
+select 'a =>b'::hstore;
+select 'a=>b '::hstore;
+select 'a=> b'::hstore;
+select '"a"=>"b"'::hstore;
+select ' "a"=>"b"'::hstore;
+select '"a" =>"b"'::hstore;
+select '"a"=>"b" '::hstore;
+select '"a"=> "b"'::hstore;
+select 'aa=>bb'::hstore;
+select ' aa=>bb'::hstore;
+select 'aa =>bb'::hstore;
+select 'aa=>bb '::hstore;
+select 'aa=> bb'::hstore;
+select '"aa"=>"bb"'::hstore;
+select ' "aa"=>"bb"'::hstore;
+select '"aa" =>"bb"'::hstore;
+select '"aa"=>"bb" '::hstore;
+select '"aa"=> "bb"'::hstore;
+
+select 'aa=>bb, cc=>dd'::hstore;
+select 'aa=>bb , cc=>dd'::hstore;
+select 'aa=>bb ,cc=>dd'::hstore;
+select 'aa=>bb, "cc"=>dd'::hstore;
+select 'aa=>bb , "cc"=>dd'::hstore;
+select 'aa=>bb ,"cc"=>dd'::hstore;
+select 'aa=>"bb", cc=>dd'::hstore;
+select 'aa=>"bb" , cc=>dd'::hstore;
+select 'aa=>"bb" ,cc=>dd'::hstore;
+
+select 'aa=>null'::hstore;
+select 'aa=>NuLl'::hstore;
+select 'aa=>"NuLl"'::hstore;
+
+select e'\\=a=>q=w'::hstore;
+select e'"=a"=>q\\=w'::hstore;
+select e'"\\"a"=>q>w'::hstore;
+select e'\\"a=>q"w'::hstore;
+
+select ''::hstore;
+select ' '::hstore;
+
+-- -> operator
+
+select 'aa=>b, c=>d , b=>16'::hstore->'c';
+select 'aa=>b, c=>d , b=>16'::hstore->'b';
+select 'aa=>b, c=>d , b=>16'::hstore->'aa';
+select ('aa=>b, c=>d , b=>16'::hstore->'gg') is null;
+select ('aa=>NULL, c=>d , b=>16'::hstore->'aa') is null;
+select ('aa=>"NULL", c=>d , b=>16'::hstore->'aa') is null;
+
+-- -> array operator
+
+select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['aa','c'];
+select 'aa=>"NULL", c=>d , b=>16'::hstore -> ARRAY['c','aa'];
+select 'aa=>NULL, c=>d , b=>16'::hstore -> ARRAY['aa','c',null];
+select 'aa=>1, c=>3, b=>2, d=>4'::hstore -> ARRAY[['b','d'],['aa','c']];
+
+-- exists/defined
+
+select exist('a=>NULL, b=>qq', 'a');
+select exist('a=>NULL, b=>qq', 'b');
+select exist('a=>NULL, b=>qq', 'c');
+select exist('a=>"NULL", b=>qq', 'a');
+select defined('a=>NULL, b=>qq', 'a');
+select defined('a=>NULL, b=>qq', 'b');
+select defined('a=>NULL, b=>qq', 'c');
+select defined('a=>"NULL", b=>qq', 'a');
+select hstore 'a=>NULL, b=>qq' ? 'a';
+select hstore 'a=>NULL, b=>qq' ? 'b';
+select hstore 'a=>NULL, b=>qq' ? 'c';
+select hstore 'a=>"NULL", b=>qq' ? 'a';
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['a','b'];
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['b','a'];
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','a'];
+select hstore 'a=>NULL, b=>qq' ?| ARRAY['c','d'];
+select hstore 'a=>NULL, b=>qq' ?| '{}'::text[];
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['a','b'];
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['b','a'];
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','a'];
+select hstore 'a=>NULL, b=>qq' ?& ARRAY['c','d'];
+select hstore 'a=>NULL, b=>qq' ?& '{}'::text[];
+
+-- delete
+
+select delete('a=>1 , b=>2, c=>3'::hstore, 'a');
+select delete('a=>null , b=>2, c=>3'::hstore, 'a');
+select delete('a=>1 , b=>2, c=>3'::hstore, 'b');
+select delete('a=>1 , b=>2, c=>3'::hstore, 'c');
+select delete('a=>1 , b=>2, c=>3'::hstore, 'd');
+select 'a=>1 , b=>2, c=>3'::hstore - 'a'::text;
+select 'a=>null , b=>2, c=>3'::hstore - 'a'::text;
+select 'a=>1 , b=>2, c=>3'::hstore - 'b'::text;
+select 'a=>1 , b=>2, c=>3'::hstore - 'c'::text;
+select 'a=>1 , b=>2, c=>3'::hstore - 'd'::text;
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b'::text)
+ = pg_column_size('a=>1, b=>2'::hstore);
+
+-- delete (array)
+
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','e']);
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['d','b']);
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY['a','c']);
+select delete('a=>1 , b=>2, c=>3'::hstore, ARRAY[['b'],['c'],['a']]);
+select delete('a=>1 , b=>2, c=>3'::hstore, '{}'::text[]);
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','e'];
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['d','b'];
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'];
+select 'a=>1 , b=>2, c=>3'::hstore - ARRAY[['b'],['c'],['a']];
+select 'a=>1 , b=>2, c=>3'::hstore - '{}'::text[];
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ARRAY['a','c'])
+ = pg_column_size('b=>2'::hstore);
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - '{}'::text[])
+ = pg_column_size('a=>1, b=>2, c=>3'::hstore);
+
+-- delete (hstore)
+
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>4, b=>2'::hstore);
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>NULL, c=>3'::hstore);
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'aa=>1, b=>2, c=>3'::hstore);
+select delete('aa=>1 , b=>2, c=>3'::hstore, 'b=>2'::hstore);
+select delete('aa=>1 , b=>2, c=>3'::hstore, ''::hstore);
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>4, b=>2'::hstore;
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>NULL, c=>3'::hstore;
+select 'aa=>1 , b=>2, c=>3'::hstore - 'aa=>1, b=>2, c=>3'::hstore;
+select 'aa=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore;
+select 'aa=>1 , b=>2, c=>3'::hstore - ''::hstore;
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - 'b=>2'::hstore)
+ = pg_column_size('a=>1, c=>3'::hstore);
+select pg_column_size('a=>1 , b=>2, c=>3'::hstore - ''::hstore)
+ = pg_column_size('a=>1, b=>2, c=>3'::hstore);
+
+-- ||
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f';
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'aq=>l';
+select 'aa=>1 , b=>2, cq=>3'::hstore || 'aa=>l';
+select 'aa=>1 , b=>2, cq=>3'::hstore || '';
+select ''::hstore || 'cq=>l, b=>g, fg=>f';
+select pg_column_size(''::hstore || ''::hstore) = pg_column_size(''::hstore);
+select pg_column_size('aa=>1'::hstore || 'b=>2'::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+select pg_column_size('aa=>1, b=>2'::hstore || ''::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+select pg_column_size(''::hstore || 'aa=>1, b=>2'::hstore)
+ = pg_column_size('aa=>1, b=>2'::hstore);
+
+-- hstore(text,text)
+select 'a=>g, b=>c'::hstore || hstore('asd', 'gf');
+select 'a=>g, b=>c'::hstore || hstore('b', 'gf');
+select 'a=>g, b=>c'::hstore || hstore('b', 'NULL');
+select 'a=>g, b=>c'::hstore || hstore('b', NULL);
+select ('a=>g, b=>c'::hstore || hstore(NULL, 'b')) is null;
+select pg_column_size(hstore('b', 'gf'))
+ = pg_column_size('b=>gf'::hstore);
+select pg_column_size('a=>g, b=>c'::hstore || hstore('b', 'gf'))
+ = pg_column_size('a=>g, b=>gf'::hstore);
+
+-- slice()
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['g','h','i']);
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']);
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['aa','b']);
+select slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']);
+select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b']))
+ = pg_column_size('b=>2, c=>3'::hstore);
+select pg_column_size(slice(hstore 'aa=>1, b=>2, c=>3', ARRAY['c','b','aa']))
+ = pg_column_size('aa=>1, b=>2, c=>3'::hstore);
+
+-- array input
+select '{}'::text[]::hstore;
+select ARRAY['a','g','b','h','asd']::hstore;
+select ARRAY['a','g','b','h','asd','i']::hstore;
+select ARRAY[['a','g'],['b','h'],['asd','i']]::hstore;
+select ARRAY[['a','g','b'],['h','asd','i']]::hstore;
+select ARRAY[[['a','g'],['b','h'],['asd','i']]]::hstore;
+select hstore('{}'::text[]);
+select hstore(ARRAY['a','g','b','h','asd']);
+select hstore(ARRAY['a','g','b','h','asd','i']);
+select hstore(ARRAY[['a','g'],['b','h'],['asd','i']]);
+select hstore(ARRAY[['a','g','b'],['h','asd','i']]);
+select hstore(ARRAY[[['a','g'],['b','h'],['asd','i']]]);
+select hstore('[0:5]={a,g,b,h,asd,i}'::text[]);
+select hstore('[0:2][1:2]={{a,g},{b,h},{asd,i}}'::text[]);
+
+-- pairs of arrays
+select hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']);
+select hstore(ARRAY['a','b','asd'], ARRAY['g','h',NULL]);
+select hstore(ARRAY['z','y','x'], ARRAY['1','2','3']);
+select hstore(ARRAY['aaa','bb','c','d'], ARRAY[null::text,null,null,null]);
+select hstore(ARRAY['aaa','bb','c','d'], null);
+select quote_literal(hstore('{}'::text[], '{}'::text[]));
+select quote_literal(hstore('{}'::text[], null));
+select hstore(ARRAY['a'], '{}'::text[]); -- error
+select hstore('{}'::text[], ARRAY['a']); -- error
+select pg_column_size(hstore(ARRAY['a','b','asd'], ARRAY['g','h','i']))
+ = pg_column_size('a=>g, b=>h, asd=>i'::hstore);
+
+-- records
+select hstore(v) from (values (1, 'foo', 1.2, 3::float8)) v(a,b,c,d);
+create domain hstestdom1 as integer not null default 0;
+create table testhstore0 (a integer, b text, c numeric, d float8);
+create table testhstore1 (a integer, b text, c numeric, d float8, e hstestdom1);
+insert into testhstore0 values (1, 'foo', 1.2, 3::float8);
+insert into testhstore1 values (1, 'foo', 1.2, 3::float8);
+select hstore(v) from testhstore1 v;
+select hstore(null::testhstore0);
+select hstore(null::testhstore1);
+select pg_column_size(hstore(v))
+ = pg_column_size('a=>1, b=>"foo", c=>"1.2", d=>"3", e=>"0"'::hstore)
+ from testhstore1 v;
+select populate_record(v, hstore('c', '3.45')) from testhstore1 v;
+select populate_record(v, hstore('d', '3.45')) from testhstore1 v;
+select populate_record(v, hstore('e', '123')) from testhstore1 v;
+select populate_record(v, hstore('e', null)) from testhstore1 v;
+select populate_record(v, hstore('c', null)) from testhstore1 v;
+select populate_record(v, hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
+select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore0 v;
+select populate_record(v, hstore('b', 'foo') || hstore('e', null)) from testhstore1 v;
+select populate_record(v, '') from testhstore0 v;
+select populate_record(v, '') from testhstore1 v;
+select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('a', '123'));
+select populate_record(null::testhstore1, hstore('c', '3.45') || hstore('e', '123'));
+select populate_record(null::testhstore0, '');
+select populate_record(null::testhstore1, '');
+select v #= hstore('c', '3.45') from testhstore1 v;
+select v #= hstore('d', '3.45') from testhstore1 v;
+select v #= hstore('e', '123') from testhstore1 v;
+select v #= hstore('c', null) from testhstore1 v;
+select v #= hstore('e', null) from testhstore0 v;
+select v #= hstore('e', null) from testhstore1 v;
+select v #= (hstore('b', 'foo') || hstore('a', '123')) from testhstore1 v;
+select v #= (hstore('b', 'foo') || hstore('e', '123')) from testhstore1 v;
+select v #= hstore '' from testhstore0 v;
+select v #= hstore '' from testhstore1 v;
+select null::testhstore1 #= (hstore('c', '3.45') || hstore('a', '123'));
+select null::testhstore1 #= (hstore('c', '3.45') || hstore('e', '123'));
+select null::testhstore0 #= hstore '';
+select null::testhstore1 #= hstore '';
+select v #= h from testhstore1 v, (values (hstore 'a=>123',1),('b=>foo,c=>3.21',2),('a=>null',3),('e=>123',4),('f=>blah',5)) x(h,i) order by i;
+
+-- keys/values
+select akeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+select akeys('""=>1');
+select akeys('');
+select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+select avals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
+select avals('""=>1');
+select avals('');
+
+select hstore_to_array('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
+select %% 'aa=>1, cq=>l, b=>g, fg=>NULL';
+
+select hstore_to_matrix('aa=>1, cq=>l, b=>g, fg=>NULL'::hstore);
+select %# 'aa=>1, cq=>l, b=>g, fg=>NULL';
+
+select * from skeys('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+select * from skeys('""=>1');
+select * from skeys('');
+select * from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>f');
+select *, svals is null from svals('aa=>1 , b=>2, cq=>3'::hstore || 'cq=>l, b=>g, fg=>NULL');
+select * from svals('""=>1');
+select * from svals('');
+
+select * from each('aaa=>bq, b=>NULL, ""=>1 ');
+
+-- @>
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>NULL';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, g=>NULL';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'g=>NULL';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>c';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b';
+select 'a=>b, b=>1, c=>NULL'::hstore @> 'a=>b, c=>q';
+
+CREATE TABLE testhstore (h hstore);
+\copy testhstore from 'data/hstore.data'
+
+select count(*) from testhstore where h @> 'wait=>NULL';
+select count(*) from testhstore where h @> 'wait=>CC';
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+select count(*) from testhstore where h ? 'public';
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+
+create index hidx on testhstore using gist(h);
+set enable_seqscan=off;
+
+select count(*) from testhstore where h @> 'wait=>NULL';
+select count(*) from testhstore where h @> 'wait=>CC';
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+select count(*) from testhstore where h ? 'public';
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+
+drop index hidx;
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=0));
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=2025));
+create index hidx on testhstore using gist(h gist_hstore_ops(siglen=2024));
+set enable_seqscan=off;
+
+select count(*) from testhstore where h @> 'wait=>NULL';
+select count(*) from testhstore where h @> 'wait=>CC';
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+select count(*) from testhstore where h ? 'public';
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+
+drop index hidx;
+create index hidx on testhstore using gin (h);
+set enable_seqscan=off;
+
+select count(*) from testhstore where h @> 'wait=>NULL';
+select count(*) from testhstore where h @> 'wait=>CC';
+select count(*) from testhstore where h @> 'wait=>CC, public=>t';
+select count(*) from testhstore where h ? 'public';
+select count(*) from testhstore where h ?| ARRAY['public','disabled'];
+select count(*) from testhstore where h ?& ARRAY['public','disabled'];
+
+select count(*) from (select (each(h)).key from testhstore) as wow ;
+select key, count(*) from (select (each(h)).key from testhstore) as wow group by key order by count desc, key;
+
+-- sort/hash
+select count(distinct h) from testhstore;
+set enable_hashagg = false;
+select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
+set enable_hashagg = true;
+set enable_sort = false;
+select count(*) from (select h from (select * from testhstore union all select * from testhstore) hs group by h) hs2;
+select distinct * from (values (hstore '' || ''),('')) v(h);
+set enable_sort = true;
+
+-- btree
+drop index hidx;
+create index hidx on testhstore using btree (h);
+set enable_seqscan=off;
+
+select count(*) from testhstore where h #># 'p=>1';
+select count(*) from testhstore where h = 'pos=>98, line=>371, node=>CBA, indexed=>t';
+
+-- json and jsonb
+select hstore_to_json('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
+select cast( hstore '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as json);
+select hstore_to_json_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
+
+select hstore_to_jsonb('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4');
+select cast( hstore '"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4' as jsonb);
+select hstore_to_jsonb_loose('"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4, h=> "2016-01-01"');
+
+create table test_json_agg (f1 text, f2 hstore);
+insert into test_json_agg values ('rec1','"a key" =>1, b => t, c => null, d=> 12345, e => 012345, f=> 1.234, g=> 2.345e+4'),
+ ('rec2','"a key" =>2, b => f, c => "null", d=> -12345, e => 012345.6, f=> -1.234, g=> 0.345e-4');
+select json_agg(q) from test_json_agg q;
+select json_agg(q) from (select f1, hstore_to_json_loose(f2) as f2 from test_json_agg) q;
+
+-- Test subscripting
+insert into test_json_agg default values;
+select f2['d'], f2['x'] is null as x_isnull from test_json_agg;
+select f2['d']['e'] from test_json_agg; -- error
+select f2['d':'e'] from test_json_agg; -- error
+update test_json_agg set f2['d'] = f2['e'], f2['x'] = 'xyzzy';
+select f2 from test_json_agg;
+
+-- Test subscripting in plpgsql
+do $$ declare h hstore;
+begin h['a'] := 'b'; raise notice 'h = %, h[a] = %', h, h['a']; end $$;
+
+-- Check the hstore_hash() and hstore_hash_extended() function explicitly.
+SELECT v as value, hstore_hash(v)::bit(32) as standard,
+ hstore_hash_extended(v, 0)::bit(32) as extended0,
+ hstore_hash_extended(v, 1)::bit(32) as extended1
+FROM (VALUES (NULL::hstore), (''), ('"a key" =>1'), ('c => null'),
+ ('e => 012345'), ('g => 2.345e+4')) x(v)
+WHERE hstore_hash(v)::bit(32) != hstore_hash_extended(v, 0)::bit(32)
+ OR hstore_hash(v)::bit(32) = hstore_hash_extended(v, 1)::bit(32);