1997-04-27 20:13:54 +02:00
|
|
|
--
|
|
|
|
-- ARRAYS
|
|
|
|
--
|
2000-01-05 18:32:29 +01:00
|
|
|
|
2001-05-21 18:54:46 +02:00
|
|
|
CREATE TABLE arrtest (
|
|
|
|
a int2[],
|
|
|
|
b int4[][][],
|
|
|
|
c name[],
|
2010-11-23 21:27:50 +01:00
|
|
|
d text[][],
|
2001-05-21 18:54:46 +02:00
|
|
|
e float8[],
|
|
|
|
f char(5)[],
|
|
|
|
g varchar(5)[]
|
|
|
|
);
|
|
|
|
|
|
|
|
--
|
2004-06-09 21:08:20 +02:00
|
|
|
-- only the 'e' array is 0-based, the others are 1-based.
|
2001-05-21 18:54:46 +02:00
|
|
|
--
|
|
|
|
|
2004-06-09 21:08:20 +02:00
|
|
|
INSERT INTO arrtest (a[1:5], b[1:1][1:2][1:2], c, d, f, g)
|
2002-08-27 22:29:11 +02:00
|
|
|
VALUES ('{1,2,3,4,5}', '{{{0,0},{1,2}}}', '{}', '{}', '{}', '{}');
|
2001-05-21 18:54:46 +02:00
|
|
|
|
|
|
|
UPDATE arrtest SET e[0] = '1.1';
|
|
|
|
|
|
|
|
UPDATE arrtest SET e[1] = '2.2';
|
|
|
|
|
|
|
|
INSERT INTO arrtest (f)
|
|
|
|
VALUES ('{"too long"}');
|
|
|
|
|
2004-06-09 21:08:20 +02:00
|
|
|
INSERT INTO arrtest (a, b[1:2][1:2], c, d, e, f, g)
|
2010-11-23 21:27:50 +01:00
|
|
|
VALUES ('{11,12,23}', '{{3,4},{4,5}}', '{"foobar"}',
|
2001-05-21 18:54:46 +02:00
|
|
|
'{{"elt1", "elt2"}}', '{"3.4", "6.7"}',
|
|
|
|
'{"abc","abcde"}', '{"abc","abcde"}');
|
|
|
|
|
2004-06-09 21:08:20 +02:00
|
|
|
INSERT INTO arrtest (a, b[1:2], c, d[1:2])
|
2001-05-21 18:54:46 +02:00
|
|
|
VALUES ('{}', '{3,4}', '{foo,bar}', '{bar,foo}');
|
|
|
|
|
|
|
|
|
1997-04-27 20:13:54 +02:00
|
|
|
SELECT * FROM arrtest;
|
|
|
|
|
|
|
|
SELECT arrtest.a[1],
|
|
|
|
arrtest.b[1][1][1],
|
|
|
|
arrtest.c[1],
|
2010-11-23 21:27:50 +01:00
|
|
|
arrtest.d[1][1],
|
1997-04-27 20:13:54 +02:00
|
|
|
arrtest.e[0]
|
|
|
|
FROM arrtest;
|
2000-01-15 20:11:40 +01:00
|
|
|
|
|
|
|
SELECT a[1], b[1][1][1], c[1], d[1][1], e[0]
|
|
|
|
FROM arrtest;
|
|
|
|
|
|
|
|
SELECT a[1:3],
|
|
|
|
b[1:1][1:2][1:2],
|
2010-11-23 21:27:50 +01:00
|
|
|
c[1:2],
|
2000-01-15 20:11:40 +01:00
|
|
|
d[1:1][1:2]
|
1997-04-27 20:13:54 +02:00
|
|
|
FROM arrtest;
|
|
|
|
|
2008-11-04 15:49:12 +01:00
|
|
|
SELECT array_ndims(a) AS a,array_ndims(b) AS b,array_ndims(c) AS c
|
|
|
|
FROM arrtest;
|
|
|
|
|
2000-07-23 03:36:05 +02:00
|
|
|
SELECT array_dims(a) AS a,array_dims(b) AS b,array_dims(c) AS c
|
|
|
|
FROM arrtest;
|
1997-04-27 20:13:54 +02:00
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
-- returns nothing
|
1997-04-27 20:13:54 +02:00
|
|
|
SELECT *
|
|
|
|
FROM arrtest
|
2010-11-23 21:27:50 +01:00
|
|
|
WHERE a[1] < 5 and
|
2000-01-15 20:11:40 +01:00
|
|
|
c = '{"foobar"}'::_name;
|
|
|
|
|
|
|
|
UPDATE arrtest
|
2000-07-23 03:36:05 +02:00
|
|
|
SET a[1:2] = '{16,25}'
|
|
|
|
WHERE NOT a = '{}'::_int2;
|
|
|
|
|
|
|
|
UPDATE arrtest
|
|
|
|
SET b[1:1][1:1][1:2] = '{113, 117}',
|
|
|
|
b[1:1][1:2][2:2] = '{142, 147}'
|
|
|
|
WHERE array_dims(b) = '[1:1][1:2][1:2]';
|
|
|
|
|
|
|
|
UPDATE arrtest
|
|
|
|
SET c[2:2] = '{"new_word"}'
|
|
|
|
WHERE array_dims(c) is not null;
|
|
|
|
|
|
|
|
SELECT a,b,c FROM arrtest;
|
2000-01-15 20:11:40 +01:00
|
|
|
|
|
|
|
SELECT a[1:3],
|
|
|
|
b[1:1][1:2][1:2],
|
2010-11-23 21:27:50 +01:00
|
|
|
c[1:2],
|
2000-01-15 20:11:40 +01:00
|
|
|
d[1:1][2:2]
|
1997-04-27 20:13:54 +02:00
|
|
|
FROM arrtest;
|
2003-04-09 01:20:04 +02:00
|
|
|
|
2015-12-23 03:05:16 +01:00
|
|
|
SELECT b[1:1][2][2],
|
|
|
|
d[1:1][2]
|
|
|
|
FROM arrtest;
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
INSERT INTO arrtest(a) VALUES('{1,null,3}');
|
|
|
|
SELECT a FROM arrtest;
|
|
|
|
UPDATE arrtest SET a[4] = NULL WHERE a[2] IS NULL;
|
|
|
|
SELECT a FROM arrtest WHERE a[2] IS NULL;
|
|
|
|
DELETE FROM arrtest WHERE a[2] IS NULL AND b IS NULL;
|
|
|
|
SELECT a,b,c FROM arrtest;
|
|
|
|
|
2015-12-23 03:05:16 +01:00
|
|
|
-- test mixed slice/scalar subscripting
|
|
|
|
select '{{1,2,3},{4,5,6},{7,8,9}}'::int[];
|
|
|
|
select ('{{1,2,3},{4,5,6},{7,8,9}}'::int[])[1:2][2];
|
|
|
|
select '[0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}'::int[];
|
|
|
|
select ('[0:2][0:2]={{1,2,3},{4,5,6},{7,8,9}}'::int[])[1:2][2];
|
|
|
|
|
|
|
|
-- test slices with empty lower and/or upper index
|
|
|
|
CREATE TEMP TABLE arrtest_s (
|
|
|
|
a int2[],
|
|
|
|
b int2[][]
|
|
|
|
);
|
|
|
|
INSERT INTO arrtest_s VALUES ('{1,2,3,4,5}', '{{1,2,3}, {4,5,6}, {7,8,9}}');
|
|
|
|
INSERT INTO arrtest_s VALUES ('[0:4]={1,2,3,4,5}', '[0:2][0:2]={{1,2,3}, {4,5,6}, {7,8,9}}');
|
|
|
|
|
|
|
|
SELECT * FROM arrtest_s;
|
|
|
|
SELECT a[:3], b[:2][:2] FROM arrtest_s;
|
|
|
|
SELECT a[2:], b[2:][2:] FROM arrtest_s;
|
|
|
|
SELECT a[:], b[:] FROM arrtest_s;
|
|
|
|
|
|
|
|
-- updates
|
|
|
|
UPDATE arrtest_s SET a[:3] = '{11, 12, 13}', b[:2][:2] = '{{11,12}, {14,15}}'
|
|
|
|
WHERE array_lower(a,1) = 1;
|
|
|
|
SELECT * FROM arrtest_s;
|
|
|
|
UPDATE arrtest_s SET a[3:] = '{23, 24, 25}', b[2:][2:] = '{{25,26}, {28,29}}';
|
|
|
|
SELECT * FROM arrtest_s;
|
|
|
|
UPDATE arrtest_s SET a[:] = '{11, 12, 13, 14, 15}';
|
|
|
|
SELECT * FROM arrtest_s;
|
|
|
|
UPDATE arrtest_s SET a[:] = '{23, 24, 25}'; -- fail, too small
|
|
|
|
INSERT INTO arrtest_s VALUES(NULL, NULL);
|
|
|
|
UPDATE arrtest_s SET a[:] = '{11, 12, 13, 14, 15}'; -- fail, no good with null
|
|
|
|
|
|
|
|
-- check with fixed-length-array type, such as point
|
|
|
|
SELECT f1[0:1] FROM POINT_TBL;
|
|
|
|
SELECT f1[0:] FROM POINT_TBL;
|
|
|
|
SELECT f1[:1] FROM POINT_TBL;
|
|
|
|
SELECT f1[:] FROM POINT_TBL;
|
|
|
|
|
2006-09-29 23:22:21 +02:00
|
|
|
--
|
|
|
|
-- test array extension
|
|
|
|
--
|
|
|
|
CREATE TEMP TABLE arrtest1 (i int[], t text[]);
|
|
|
|
insert into arrtest1 values(array[1,2,null,4], array['one','two',null,'four']);
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[2] = 22, t[2] = 'twenty-two';
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[5] = 5, t[5] = 'five';
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[8] = 8, t[8] = 'eight';
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[0] = 0, t[0] = 'zero';
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[-3] = -3, t[-3] = 'minus-three';
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[0:2] = array[10,11,12], t[0:2] = array['ten','eleven','twelve'];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[8:10] = array[18,null,20], t[8:10] = array['p18',null,'p20'];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[11:12] = array[null,22], t[11:12] = array[null,'p22'];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[15:16] = array[null,26], t[15:16] = array[null,'p26'];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[-5:-3] = array[-15,-14,-13], t[-5:-3] = array['m15','m14','m13'];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[-7:-6] = array[-17,null], t[-7:-6] = array['m17',null];
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[-12:-10] = array[-22,null,-20], t[-12:-10] = array['m22',null,'m20'];
|
|
|
|
select * from arrtest1;
|
|
|
|
delete from arrtest1;
|
|
|
|
insert into arrtest1 values(array[1,2,null,4], array['one','two',null,'four']);
|
|
|
|
select * from arrtest1;
|
|
|
|
update arrtest1 set i[0:5] = array[0,1,2,null,4,5], t[0:5] = array['z','p1','p2',null,'p4','p5'];
|
|
|
|
select * from arrtest1;
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
--
|
|
|
|
-- array expressions and operators
|
|
|
|
--
|
|
|
|
|
|
|
|
-- table creation and INSERTs
|
|
|
|
CREATE TEMP TABLE arrtest2 (i integer ARRAY[4], f float8[], n numeric[], t text[], d timestamp[]);
|
|
|
|
INSERT INTO arrtest2 VALUES(
|
|
|
|
ARRAY[[[113,142],[1,147]]],
|
|
|
|
ARRAY[1.1,1.2,1.3]::float8[],
|
|
|
|
ARRAY[1.1,1.2,1.3],
|
|
|
|
ARRAY[[['aaa','aab'],['aba','abb'],['aca','acb']],[['baa','bab'],['bba','bbb'],['bca','bcb']]],
|
|
|
|
ARRAY['19620326','19931223','19970117']::timestamp[]
|
|
|
|
);
|
|
|
|
|
|
|
|
-- some more test data
|
|
|
|
CREATE TEMP TABLE arrtest_f (f0 int, f1 text, f2 float8);
|
|
|
|
insert into arrtest_f values(1,'cat1',1.21);
|
|
|
|
insert into arrtest_f values(2,'cat1',1.24);
|
|
|
|
insert into arrtest_f values(3,'cat1',1.18);
|
|
|
|
insert into arrtest_f values(4,'cat1',1.26);
|
|
|
|
insert into arrtest_f values(5,'cat1',1.15);
|
|
|
|
insert into arrtest_f values(6,'cat2',1.15);
|
|
|
|
insert into arrtest_f values(7,'cat2',1.26);
|
|
|
|
insert into arrtest_f values(8,'cat2',1.32);
|
|
|
|
insert into arrtest_f values(9,'cat2',1.30);
|
|
|
|
|
|
|
|
CREATE TEMP TABLE arrtest_i (f0 int, f1 text, f2 int);
|
|
|
|
insert into arrtest_i values(1,'cat1',21);
|
|
|
|
insert into arrtest_i values(2,'cat1',24);
|
|
|
|
insert into arrtest_i values(3,'cat1',18);
|
|
|
|
insert into arrtest_i values(4,'cat1',26);
|
|
|
|
insert into arrtest_i values(5,'cat1',15);
|
|
|
|
insert into arrtest_i values(6,'cat2',15);
|
|
|
|
insert into arrtest_i values(7,'cat2',26);
|
|
|
|
insert into arrtest_i values(8,'cat2',32);
|
|
|
|
insert into arrtest_i values(9,'cat2',30);
|
|
|
|
|
|
|
|
-- expressions
|
|
|
|
SELECT t.f[1][3][1] AS "131", t.f[2][2][1] AS "221" FROM (
|
|
|
|
SELECT ARRAY[[[111,112],[121,122],[131,132]],[[211,212],[221,122],[231,232]]] AS f
|
|
|
|
) AS t;
|
|
|
|
SELECT ARRAY[[[[[['hello'],['world']]]]]];
|
|
|
|
SELECT ARRAY[ARRAY['hello'],ARRAY['world']];
|
|
|
|
SELECT ARRAY(select f2 from arrtest_f order by f2) AS "ARRAY";
|
|
|
|
|
2005-11-17 23:14:56 +01:00
|
|
|
-- with nulls
|
|
|
|
SELECT '{1,null,3}'::int[];
|
|
|
|
SELECT ARRAY[1,NULL,3];
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
-- functions
|
2003-06-27 02:33:26 +02:00
|
|
|
SELECT array_append(array[42], 6) AS "{42,6}";
|
|
|
|
SELECT array_prepend(6, array[42]) AS "{6,42}";
|
2003-08-18 01:43:27 +02:00
|
|
|
SELECT array_cat(ARRAY[1,2], ARRAY[3,4]) AS "{1,2,3,4}";
|
2003-04-09 01:20:04 +02:00
|
|
|
SELECT array_cat(ARRAY[1,2], ARRAY[[3,4],[5,6]]) AS "{{1,2},{3,4},{5,6}}";
|
|
|
|
SELECT array_cat(ARRAY[[3,4],[5,6]], ARRAY[1,2]) AS "{{3,4},{5,6},{1,2}}";
|
|
|
|
|
2015-03-30 21:13:21 +02:00
|
|
|
SELECT array_position(ARRAY[1,2,3,4,5], 4);
|
|
|
|
SELECT array_position(ARRAY[5,3,4,2,1], 4);
|
|
|
|
SELECT array_position(ARRAY[[1,2],[3,4]], 3);
|
|
|
|
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'mon');
|
|
|
|
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], 'sat');
|
|
|
|
SELECT array_position(ARRAY['sun','mon','tue','wed','thu','fri','sat'], NULL);
|
|
|
|
SELECT array_position(ARRAY['sun','mon','tue','wed','thu',NULL,'fri','sat'], NULL);
|
|
|
|
SELECT array_position(ARRAY['sun','mon','tue','wed','thu',NULL,'fri','sat'], 'sat');
|
|
|
|
|
|
|
|
SELECT array_positions(NULL, 10);
|
|
|
|
SELECT array_positions(NULL, NULL::int);
|
|
|
|
SELECT array_positions(ARRAY[1,2,3,4,5,6,1,2,3,4,5,6], 4);
|
|
|
|
SELECT array_positions(ARRAY[[1,2],[3,4]], 4);
|
|
|
|
SELECT array_positions(ARRAY[1,2,3,4,5,6,1,2,3,4,5,6], NULL);
|
|
|
|
SELECT array_positions(ARRAY[1,2,3,NULL,5,6,1,2,3,NULL,5,6], NULL);
|
|
|
|
SELECT array_length(array_positions(ARRAY(SELECT 'AAAAAAAAAAAAAAAAAAAAAAAAA'::text || i % 10
|
2015-03-18 20:01:34 +01:00
|
|
|
FROM generate_series(1,100) g(i)),
|
|
|
|
'AAAAAAAAAAAAAAAAAAAAAAAAA5'), 1);
|
|
|
|
|
|
|
|
DO $$
|
|
|
|
DECLARE
|
|
|
|
o int;
|
|
|
|
a int[] := ARRAY[1,2,3,2,3,1,2];
|
|
|
|
BEGIN
|
2015-03-30 21:13:21 +02:00
|
|
|
o := array_position(a, 2);
|
2015-03-18 20:01:34 +01:00
|
|
|
WHILE o IS NOT NULL
|
|
|
|
LOOP
|
|
|
|
RAISE NOTICE '%', o;
|
2015-03-30 21:13:21 +02:00
|
|
|
o := array_position(a, 2, o + 1);
|
2015-03-18 20:01:34 +01:00
|
|
|
END LOOP;
|
|
|
|
END
|
|
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
|
2015-03-30 21:13:21 +02:00
|
|
|
SELECT array_position('[2:4]={1,2,3}'::int[], 1);
|
|
|
|
SELECT array_positions('[2:4]={1,2,3}'::int[], 1);
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
-- operators
|
|
|
|
SELECT a FROM arrtest WHERE b = ARRAY[[[113,142],[1,147]]];
|
|
|
|
SELECT NOT ARRAY[1.1,1.2,1.3] = ARRAY[1.1,1.2,1.3] AS "FALSE";
|
|
|
|
SELECT ARRAY[1,2] || 3 AS "{1,2,3}";
|
|
|
|
SELECT 0 || ARRAY[1,2] AS "{0,1,2}";
|
2003-08-18 01:43:27 +02:00
|
|
|
SELECT ARRAY[1,2] || ARRAY[3,4] AS "{1,2,3,4}";
|
2003-04-09 01:20:04 +02:00
|
|
|
SELECT ARRAY[[['hello','world']]] || ARRAY[[['happy','birthday']]] AS "ARRAY";
|
|
|
|
SELECT ARRAY[[1,2],[3,4]] || ARRAY[5,6] AS "{{1,2},{3,4},{5,6}}";
|
2003-08-18 01:43:27 +02:00
|
|
|
SELECT ARRAY[0,0] || ARRAY[1,1] || ARRAY[2,2] AS "{0,0,1,1,2,2}";
|
2003-04-09 01:20:04 +02:00
|
|
|
SELECT 0 || ARRAY[1,2] || 3 AS "{0,1,2,3}";
|
|
|
|
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i @> '{32}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i && '{32}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i @> '{17}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i && '{17}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i @> '{32,17}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i && '{32,17}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE i <@ '{38,34,32,89}' ORDER BY seqno;
|
Fix GIN to support null keys, empty and null items, and full index scans.
Per my recent proposal(s). Null key datums can now be returned by
extractValue and extractQuery functions, and will be stored in the index.
Also, placeholder entries are made for indexable items that are NULL or
contain no keys according to extractValue. This means that the index is
now always complete, having at least one entry for every indexed heap TID,
and so we can get rid of the prohibition on full-index scans. A full-index
scan is implemented much the same way as partial-match scans were already:
we build a bitmap representing all the TIDs found in the index, and then
drive the results off that.
Also, introduce a concept of a "search mode" that can be requested by
extractQuery when the operator requires matching to empty items (this is
just as cheap as matching to a single key) or requires a full index scan
(which is not so cheap, but it sure beats failing or giving wrong answers).
The behavior remains backward compatible for opclasses that don't return
any null keys or request a non-default search mode.
Using these features, we can now make the GIN index opclass for anyarray
behave in a way that matches the actual anyarray operators for &&, <@, @>,
and = ... which it failed to do before in assorted corner cases.
This commit fixes the core GIN code and ginarrayprocs.c, updates the
documentation, and adds some simple regression test cases for the new
behaviors using the array operators. The tsearch and contrib GIN opclass
support functions still need to be looked over and probably fixed.
Another thing I intend to fix separately is that this is pretty inefficient
for cases where more than one scan condition needs a full-index search:
we'll run duplicate GinScanEntrys, each one of which builds a large bitmap.
There is some existing logic to merge duplicate GinScanEntrys but it needs
refactoring to make it work for entries belonging to different scan keys.
Note that most of gin.h has been split out into a new file gin_private.h,
so that gin.h doesn't export anything that's not supposed to be used by GIN
opclasses or the rest of the backend. I did quite a bit of other code
beautification work as well, mostly fixing comments and choosing more
appropriate names for things.
2011-01-08 01:16:24 +01:00
|
|
|
SELECT * FROM array_op_test WHERE i = '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i @> '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i && '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i <@ '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i = '{NULL}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i @> '{NULL}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i && '{NULL}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE i <@ '{NULL}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t @> '{AAAAAAAA72908}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t && '{AAAAAAAA72908}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t @> '{AAAAAAAAAA646}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t && '{AAAAAAAAAA646}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t @> '{AAAAAAAA72908,AAAAAAAAAA646}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t && '{AAAAAAAA72908,AAAAAAAAAA646}' ORDER BY seqno;
|
2006-09-10 02:29:35 +02:00
|
|
|
SELECT * FROM array_op_test WHERE t <@ '{AAAAAAAA72908,AAAAAAAAAAAAAAAAAAA17075,AA88409,AAAAAAAAAAAAAAAAAA36842,AAAAAAA48038,AAAAAAAAAAAAAA10611}' ORDER BY seqno;
|
Fix GIN to support null keys, empty and null items, and full index scans.
Per my recent proposal(s). Null key datums can now be returned by
extractValue and extractQuery functions, and will be stored in the index.
Also, placeholder entries are made for indexable items that are NULL or
contain no keys according to extractValue. This means that the index is
now always complete, having at least one entry for every indexed heap TID,
and so we can get rid of the prohibition on full-index scans. A full-index
scan is implemented much the same way as partial-match scans were already:
we build a bitmap representing all the TIDs found in the index, and then
drive the results off that.
Also, introduce a concept of a "search mode" that can be requested by
extractQuery when the operator requires matching to empty items (this is
just as cheap as matching to a single key) or requires a full index scan
(which is not so cheap, but it sure beats failing or giving wrong answers).
The behavior remains backward compatible for opclasses that don't return
any null keys or request a non-default search mode.
Using these features, we can now make the GIN index opclass for anyarray
behave in a way that matches the actual anyarray operators for &&, <@, @>,
and = ... which it failed to do before in assorted corner cases.
This commit fixes the core GIN code and ginarrayprocs.c, updates the
documentation, and adds some simple regression test cases for the new
behaviors using the array operators. The tsearch and contrib GIN opclass
support functions still need to be looked over and probably fixed.
Another thing I intend to fix separately is that this is pretty inefficient
for cases where more than one scan condition needs a full-index search:
we'll run duplicate GinScanEntrys, each one of which builds a large bitmap.
There is some existing logic to merge duplicate GinScanEntrys but it needs
refactoring to make it work for entries belonging to different scan keys.
Note that most of gin.h has been split out into a new file gin_private.h,
so that gin.h doesn't export anything that's not supposed to be used by GIN
opclasses or the rest of the backend. I did quite a bit of other code
beautification work as well, mostly fixing comments and choosing more
appropriate names for things.
2011-01-08 01:16:24 +01:00
|
|
|
SELECT * FROM array_op_test WHERE t = '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE t @> '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE t && '{}' ORDER BY seqno;
|
|
|
|
SELECT * FROM array_op_test WHERE t <@ '{}' ORDER BY seqno;
|
2006-05-02 13:28:56 +02:00
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
-- array casts
|
|
|
|
SELECT ARRAY[1,2,3]::text[]::int[]::float8[] AS "{1,2,3}";
|
|
|
|
SELECT ARRAY[1,2,3]::text[]::int[]::float8[] is of (float8[]) as "TRUE";
|
|
|
|
SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] AS "{{a,bc},{def,hijk}}";
|
|
|
|
SELECT ARRAY[['a','bc'],['def','hijk']]::text[]::varchar[] is of (varchar[]) as "TRUE";
|
|
|
|
SELECT CAST(ARRAY[[[[[['a','bb','ccc']]]]]] as text[]) as "{{{{{{a,bb,ccc}}}}}}";
|
2003-06-27 02:33:26 +02:00
|
|
|
|
2003-06-29 02:33:44 +02:00
|
|
|
-- scalar op any/all (array)
|
|
|
|
select 33 = any ('{1,2,3}');
|
|
|
|
select 33 = any ('{1,2,33}');
|
|
|
|
select 33 = all ('{1,2,33}');
|
|
|
|
select 33 >= all ('{1,2,33}');
|
|
|
|
-- boundary cases
|
|
|
|
select null::int >= all ('{1,2,33}');
|
|
|
|
select null::int >= all ('{}');
|
|
|
|
select null::int >= any ('{}');
|
|
|
|
-- cross-datatype
|
|
|
|
select 33.4 = any (array[1,2,3]);
|
|
|
|
select 33.4 > all (array[1,2,3]);
|
|
|
|
-- errors
|
|
|
|
select 33 * any ('{1,2,3}');
|
|
|
|
select 33 * any (44);
|
2005-11-17 23:14:56 +01:00
|
|
|
-- nulls
|
|
|
|
select 33 = any (null::int[]);
|
|
|
|
select null::int = any ('{1,2,3}');
|
|
|
|
select 33 = any ('{1,null,3}');
|
|
|
|
select 33 = any ('{1,null,33}');
|
|
|
|
select 33 = all (null::int[]);
|
|
|
|
select null::int = all ('{1,2,3}');
|
|
|
|
select 33 = all ('{1,null,3}');
|
|
|
|
select 33 = all ('{33,null,33}');
|
2003-06-29 02:33:44 +02:00
|
|
|
|
2003-06-27 02:33:26 +02:00
|
|
|
-- test indexes on arrays
|
|
|
|
create temp table arr_tbl (f1 int[] unique);
|
|
|
|
insert into arr_tbl values ('{1,2,3}');
|
|
|
|
insert into arr_tbl values ('{1,2}');
|
|
|
|
-- failure expected:
|
|
|
|
insert into arr_tbl values ('{1,2,3}');
|
|
|
|
insert into arr_tbl values ('{2,3,4}');
|
|
|
|
insert into arr_tbl values ('{1,5,3}');
|
|
|
|
insert into arr_tbl values ('{1,2,10}');
|
2005-04-22 23:58:32 +02:00
|
|
|
|
2003-06-27 02:33:26 +02:00
|
|
|
set enable_seqscan to off;
|
2005-04-22 23:58:32 +02:00
|
|
|
set enable_bitmapscan to off;
|
2003-06-27 02:33:26 +02:00
|
|
|
select * from arr_tbl where f1 > '{1,2,3}' and f1 <= '{1,5,3}';
|
2008-11-05 13:27:09 +01:00
|
|
|
select * from arr_tbl where f1 >= '{1,2,3}' and f1 < '{1,5,3}';
|
2015-07-24 11:48:53 +02:00
|
|
|
|
|
|
|
-- test ON CONFLICT DO UPDATE with arrays
|
|
|
|
create temp table arr_pk_tbl (pk int4 primary key, f1 int[]);
|
|
|
|
insert into arr_pk_tbl values (1, '{1,2,3}');
|
|
|
|
insert into arr_pk_tbl values (1, '{3,4,5}') on conflict (pk)
|
|
|
|
do update set f1[1] = excluded.f1[1], f1[3] = excluded.f1[3]
|
|
|
|
returning pk, f1;
|
|
|
|
insert into arr_pk_tbl(pk, f1[1:2]) values (1, '{6,7,8}') on conflict (pk)
|
|
|
|
do update set f1[1] = excluded.f1[1],
|
|
|
|
f1[2] = excluded.f1[2],
|
|
|
|
f1[3] = excluded.f1[3]
|
|
|
|
returning pk, f1;
|
|
|
|
|
2008-11-05 13:27:09 +01:00
|
|
|
-- note: if above selects don't produce the expected tuple order,
|
2003-06-27 02:33:26 +02:00
|
|
|
-- then you didn't get an indexscan plan, and something is busted.
|
2005-04-22 23:58:32 +02:00
|
|
|
reset enable_seqscan;
|
|
|
|
reset enable_bitmapscan;
|
2004-04-05 05:07:26 +02:00
|
|
|
|
|
|
|
-- test [not] (like|ilike) (any|all) (...)
|
|
|
|
select 'foo' like any (array['%a', '%o']); -- t
|
|
|
|
select 'foo' like any (array['%a', '%b']); -- f
|
|
|
|
select 'foo' like all (array['f%', '%o']); -- t
|
|
|
|
select 'foo' like all (array['f%', '%b']); -- f
|
|
|
|
select 'foo' not like any (array['%a', '%b']); -- t
|
|
|
|
select 'foo' not like all (array['%a', '%o']); -- f
|
|
|
|
select 'foo' ilike any (array['%A', '%O']); -- t
|
|
|
|
select 'foo' ilike all (array['F%', '%O']); -- t
|
2004-08-28 21:31:29 +02:00
|
|
|
|
|
|
|
--
|
|
|
|
-- General array parser tests
|
|
|
|
--
|
|
|
|
|
|
|
|
-- none of the following should be accepted
|
|
|
|
select '{{1,{2}},{2,3}}'::text[];
|
|
|
|
select '{{},{}}'::text[];
|
2005-06-26 05:04:37 +02:00
|
|
|
select E'{{1,2},\\{2,3}}'::text[];
|
2004-08-28 21:31:29 +02:00
|
|
|
select '{{"1 2" x},{3}}'::text[];
|
|
|
|
select '{}}'::text[];
|
|
|
|
select '{ }}'::text[];
|
2008-03-20 22:42:48 +01:00
|
|
|
select array[];
|
2004-08-28 21:31:29 +02:00
|
|
|
-- none of the above should be accepted
|
|
|
|
|
|
|
|
-- all of the following should be accepted
|
|
|
|
select '{}'::text[];
|
|
|
|
select '{{{1,2,3,4},{2,3,4,5}},{{3,4,5,6},{4,5,6,7}}}'::text[];
|
|
|
|
select '{0 second ,0 second}'::interval[];
|
|
|
|
select '{ { "," } , { 3 } }'::text[];
|
|
|
|
select ' { { " 0 second " , 0 second } }'::text[];
|
|
|
|
select '{
|
|
|
|
0 second,
|
|
|
|
@ 1 hour @ 42 minutes @ 20 seconds
|
|
|
|
}'::interval[];
|
2008-03-20 22:42:48 +01:00
|
|
|
select array[]::text[];
|
2008-11-05 13:27:09 +01:00
|
|
|
select '[0:1]={1.1,2.2}'::float8[];
|
2004-08-28 21:31:29 +02:00
|
|
|
-- all of the above should be accepted
|
2005-02-28 04:45:24 +01:00
|
|
|
|
|
|
|
-- tests for array aggregates
|
|
|
|
CREATE TEMP TABLE arraggtest ( f1 INT[], f2 TEXT[][], f3 FLOAT[]);
|
|
|
|
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{1,2,3,4}','{{grey,red},{blue,blue}}','{1.6, 0.0}');
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{1,2,3}','{{grey,red},{grey,blue}}','{1.6}');
|
|
|
|
SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
|
|
|
|
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{3,3,2,4,5,6}','{{white,yellow},{pink,orange}}','{2.1,3.3,1.8,1.7,1.6}');
|
|
|
|
SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
|
|
|
|
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{2}','{{black,red},{green,orange}}','{1.6,2.2,2.6,0.4}');
|
|
|
|
SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
|
|
|
|
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{4,2,6,7,8,1}','{{red},{black},{purple},{blue},{blue}}',NULL);
|
|
|
|
SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
|
|
|
|
|
|
|
|
INSERT INTO arraggtest (f1, f2, f3) VALUES
|
|
|
|
('{}','{{pink,white,blue,red,grey,orange}}','{2.1,1.87,1.4,2.2}');
|
|
|
|
SELECT max(f1), min(f1), max(f2), min(f2), max(f3), min(f3) FROM arraggtest;
|
2007-05-12 02:55:00 +02:00
|
|
|
|
|
|
|
-- A few simple tests for arrays of composite types
|
|
|
|
|
|
|
|
create type comptype as (f1 int, f2 text);
|
|
|
|
|
|
|
|
create table comptable (c1 comptype, c2 comptype[]);
|
|
|
|
|
|
|
|
-- XXX would like to not have to specify row() construct types here ...
|
|
|
|
insert into comptable
|
|
|
|
values (row(1,'foo'), array[row(2,'bar')::comptype, row(3,'baz')::comptype]);
|
|
|
|
|
|
|
|
-- check that implicitly named array type _comptype isn't a problem
|
|
|
|
create type _comptype as enum('fooey');
|
|
|
|
|
|
|
|
select * from comptable;
|
|
|
|
select c2[2].f2 from comptable;
|
|
|
|
|
|
|
|
drop type _comptype;
|
|
|
|
drop table comptable;
|
|
|
|
drop type comptype;
|
2008-04-28 16:48:58 +02:00
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
create or replace function unnest1(anyarray)
|
2008-04-28 16:48:58 +02:00
|
|
|
returns setof anyelement as $$
|
|
|
|
select $1[s] from generate_subscripts($1,1) g(s);
|
|
|
|
$$ language sql immutable;
|
|
|
|
|
2010-11-23 21:27:50 +01:00
|
|
|
create or replace function unnest2(anyarray)
|
2008-04-28 16:48:58 +02:00
|
|
|
returns setof anyelement as $$
|
|
|
|
select $1[s1][s2] from generate_subscripts($1,1) g1(s1),
|
|
|
|
generate_subscripts($1,2) g2(s2);
|
|
|
|
$$ language sql immutable;
|
|
|
|
|
|
|
|
select * from unnest1(array[1,2,3]);
|
|
|
|
select * from unnest2(array[[1,2,3],[4,5,6]]);
|
|
|
|
|
|
|
|
drop function unnest1(anyarray);
|
|
|
|
drop function unnest2(anyarray);
|
2008-07-16 02:48:54 +02:00
|
|
|
|
|
|
|
select array_fill(null::integer, array[3,3],array[2,2]);
|
|
|
|
select array_fill(null::integer, array[3,3]);
|
|
|
|
select array_fill(null::text, array[3,3],array[2,2]);
|
|
|
|
select array_fill(null::text, array[3,3]);
|
|
|
|
select array_fill(7, array[3,3],array[2,2]);
|
|
|
|
select array_fill(7, array[3,3]);
|
|
|
|
select array_fill('juhu'::text, array[3,3],array[2,2]);
|
|
|
|
select array_fill('juhu'::text, array[3,3]);
|
|
|
|
-- raise exception
|
|
|
|
select array_fill(1, null, array[2,2]);
|
|
|
|
select array_fill(1, array[2,2], null);
|
|
|
|
select array_fill(1, array[3,3], array[1,1,1]);
|
|
|
|
select array_fill(1, array[1,2,null]);
|
2008-11-05 13:27:09 +01:00
|
|
|
|
|
|
|
select string_to_array('1|2|3', '|');
|
|
|
|
select string_to_array('1|2|3|', '|');
|
|
|
|
select string_to_array('1||2|3||', '||');
|
|
|
|
select string_to_array('1|2|3', '');
|
|
|
|
select string_to_array('', '|');
|
|
|
|
select string_to_array('1|2|3', NULL);
|
2010-08-10 23:51:00 +02:00
|
|
|
select string_to_array(NULL, '|') IS NULL;
|
|
|
|
select string_to_array('abc', '');
|
|
|
|
select string_to_array('abc', '', 'abc');
|
|
|
|
select string_to_array('abc', ',');
|
|
|
|
select string_to_array('abc', ',', 'abc');
|
|
|
|
select string_to_array('1,2,3,4,,6', ',');
|
|
|
|
select string_to_array('1,2,3,4,,6', ',', '');
|
|
|
|
select string_to_array('1,2,3,4,*,6', ',', '*');
|
|
|
|
|
|
|
|
select array_to_string(NULL::int4[], ',') IS NULL;
|
|
|
|
select array_to_string('{}'::int4[], ',');
|
|
|
|
select array_to_string(array[1,2,3,4,NULL,6], ',');
|
|
|
|
select array_to_string(array[1,2,3,4,NULL,6], ',', '*');
|
|
|
|
select array_to_string(array[1,2,3,4,NULL,6], NULL);
|
|
|
|
select array_to_string(array[1,2,3,4,NULL,6], ',', NULL);
|
2008-11-05 13:27:09 +01:00
|
|
|
|
|
|
|
select array_to_string(string_to_array('1|2|3', '|'), '|');
|
2008-11-12 14:09:28 +01:00
|
|
|
|
|
|
|
select array_length(array[1,2,3], 1);
|
|
|
|
select array_length(array[[1,2,3], [4,5,6]], 0);
|
|
|
|
select array_length(array[[1,2,3], [4,5,6]], 1);
|
|
|
|
select array_length(array[[1,2,3], [4,5,6]], 2);
|
|
|
|
select array_length(array[[1,2,3], [4,5,6]], 3);
|
2008-11-13 16:59:51 +01:00
|
|
|
|
2014-01-21 18:38:53 +01:00
|
|
|
select cardinality(NULL::int[]);
|
|
|
|
select cardinality('{}'::int[]);
|
|
|
|
select cardinality(array[1,2,3]);
|
|
|
|
select cardinality('[2:4]={5,6,7}'::int[]);
|
|
|
|
select cardinality('{{1,2}}'::int[]);
|
|
|
|
select cardinality('{{1,2},{3,4},{5,6}}'::int[]);
|
2014-02-01 16:49:17 +01:00
|
|
|
select cardinality('{{{1,9},{5,6}},{{2,3},{3,4}}}'::int[]);
|
2014-01-21 18:38:53 +01:00
|
|
|
|
2014-11-25 18:21:22 +01:00
|
|
|
-- array_agg(anynonarray)
|
2008-11-29 01:39:46 +01:00
|
|
|
select array_agg(unique1) from (select unique1 from tenk1 where unique1 < 15 order by unique1) ss;
|
|
|
|
select array_agg(ten) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
|
|
|
|
select array_agg(nullif(ten, 4)) from (select ten from tenk1 where unique1 < 15 order by unique1) ss;
|
2008-11-13 16:59:51 +01:00
|
|
|
select array_agg(unique1) from tenk1 where unique1 < -15;
|
2008-11-14 01:51:47 +01:00
|
|
|
|
2014-11-25 18:21:22 +01:00
|
|
|
-- array_agg(anyarray)
|
|
|
|
select array_agg(ar)
|
|
|
|
from (values ('{1,2}'::int[]), ('{3,4}'::int[])) v(ar);
|
|
|
|
select array_agg(distinct ar order by ar desc)
|
|
|
|
from (select array[i / 2] from generate_series(1,10) a(i)) b(ar);
|
|
|
|
select array_agg(ar)
|
|
|
|
from (select array_agg(array[i, i+1, i-1])
|
|
|
|
from generate_series(1,2) a(i)) b(ar);
|
|
|
|
select array_agg(array[i+1.2, i+1.3, i+1.4]) from generate_series(1,3) g(i);
|
|
|
|
select array_agg(array['Hello', i::text]) from generate_series(9,11) g(i);
|
|
|
|
select array_agg(array[i, nullif(i, 3), i+1]) from generate_series(1,4) g(i);
|
|
|
|
-- errors
|
|
|
|
select array_agg('{}'::int[]) from generate_series(1,2);
|
|
|
|
select array_agg(null::int[]) from generate_series(1,2);
|
|
|
|
select array_agg(ar)
|
|
|
|
from (values ('{1,2}'::int[]), ('{3}'::int[])) v(ar);
|
|
|
|
|
2008-11-14 01:51:47 +01:00
|
|
|
select unnest(array[1,2,3]);
|
|
|
|
select * from unnest(array[1,2,3]);
|
|
|
|
select unnest(array[1,2,3,4.5]::float8[]);
|
|
|
|
select unnest(array[1,2,3,4.5]::numeric[]);
|
|
|
|
select unnest(array[1,2,3,null,4,null,null,5,6]);
|
|
|
|
select unnest(array[1,2,3,null,4,null,null,5,6]::text[]);
|
2013-05-12 19:08:12 +02:00
|
|
|
select abs(unnest(array[1,2,null,-3]));
|
2012-07-11 19:59:35 +02:00
|
|
|
select array_remove(array[1,2,2,3], 2);
|
|
|
|
select array_remove(array[1,2,2,3], 5);
|
|
|
|
select array_remove(array[1,NULL,NULL,3], NULL);
|
|
|
|
select array_remove(array['A','CC','D','C','RR'], 'RR');
|
|
|
|
select array_remove('{{1,2,2},{1,4,3}}', 2); -- not allowed
|
2013-06-01 03:50:59 +02:00
|
|
|
select array_remove(array['X','X','X'], 'X') = '{}';
|
2012-07-11 19:59:35 +02:00
|
|
|
select array_replace(array[1,2,5,4],5,3);
|
|
|
|
select array_replace(array[1,2,5,4],5,NULL);
|
|
|
|
select array_replace(array[1,2,NULL,4,NULL],NULL,5);
|
|
|
|
select array_replace(array['A','B','DD','B'],'B','CC');
|
|
|
|
select array_replace(array[1,NULL,3],NULL,NULL);
|
|
|
|
select array_replace(array['AB',NULL,'CDE'],NULL,'12');
|
2010-02-18 19:41:47 +01:00
|
|
|
|
2014-11-25 18:21:22 +01:00
|
|
|
-- array(select array-value ...)
|
|
|
|
select array(select array[i,i/2] from generate_series(1,5) i);
|
|
|
|
select array(select array['Hello', i::text] from generate_series(9,11) i);
|
|
|
|
|
2010-02-18 19:41:47 +01:00
|
|
|
-- Insert/update on a column that is array of composite
|
|
|
|
|
|
|
|
create temp table t1 (f1 int8_tbl[]);
|
|
|
|
insert into t1 (f1[5].q1) values(42);
|
|
|
|
select * from t1;
|
|
|
|
update t1 set f1[5].q2 = 43;
|
|
|
|
select * from t1;
|
Fix failure to detoast fields in composite elements of structured types.
If we have an array of records stored on disk, the individual record fields
cannot contain out-of-line TOAST pointers: the tuptoaster.c mechanisms are
only prepared to deal with TOAST pointers appearing in top-level fields of
a stored row. The same applies for ranges over composite types, nested
composites, etc. However, the existing code only took care of expanding
sub-field TOAST pointers for the case of nested composites, not for other
structured types containing composites. For example, given a command such
as
UPDATE tab SET arraycol = ARRAY[(ROW(x,42)::mycompositetype] ...
where x is a direct reference to a field of an on-disk tuple, if that field
is long enough to be toasted out-of-line then the TOAST pointer would be
inserted as-is into the array column. If the source record for x is later
deleted, the array field value would become a dangling pointer, leading
to errors along the line of "missing chunk number 0 for toast value ..."
when the value is referenced. A reproducible test case for this was
provided by Jan Pecek, but it seems likely that some of the "missing chunk
number" reports we've heard in the past were caused by similar issues.
Code-wise, the problem is that PG_DETOAST_DATUM() is not adequate to
produce a self-contained Datum value if the Datum is of composite type.
Seen in this light, the problem is not just confined to arrays and ranges,
but could also affect some other places where detoasting is done in that
way, for example form_index_tuple().
I tried teaching the array code to apply toast_flatten_tuple_attribute()
along with PG_DETOAST_DATUM() when the array element type is composite,
but this was messy and imposed extra cache lookup costs whether or not any
TOAST pointers were present, indeed sometimes when the array element type
isn't even composite (since sometimes it takes a typcache lookup to find
that out). The idea of extending that approach to all the places that
currently use PG_DETOAST_DATUM() wasn't attractive at all.
This patch instead solves the problem by decreeing that composite Datum
values must not contain any out-of-line TOAST pointers in the first place;
that is, we expand out-of-line fields at the point of constructing a
composite Datum, not at the point where we're about to insert it into a
larger tuple. This rule is applied only to true composite Datums, not
to tuples that are being passed around the system as tuples, so it's not
as invasive as it might sound at first. With this approach, the amount
of code that has to be touched for a full solution is greatly reduced,
and added cache lookup costs are avoided except when there actually is
a TOAST pointer that needs to be inlined.
The main drawback of this approach is that we might sometimes dereference
a TOAST pointer that will never actually be used by the query, imposing a
rather large cost that wasn't there before. On the other side of the coin,
if the field value is used multiple times then we'll come out ahead by
avoiding repeat detoastings. Experimentation suggests that common SQL
coding patterns are unaffected either way, though. Applications that are
very negatively affected could be advised to modify their code to not fetch
columns they won't be using.
In future, we might consider reverting this solution in favor of detoasting
only at the point where data is about to be stored to disk, using some
method that can drill down into multiple levels of nested structured types.
That will require defining new APIs for structured types, though, so it
doesn't seem feasible as a back-patchable fix.
Note that this patch changes HeapTupleGetDatum() from a macro to a function
call; this means that any third-party code using that macro will not get
protection against creating TOAST-pointer-containing Datums until it's
recompiled. The same applies to any uses of PG_RETURN_HEAPTUPLEHEADER().
It seems likely that this is not a big problem in practice: most of the
tuple-returning functions in core and contrib produce outputs that could
not possibly be toasted anyway, and the same probably holds for third-party
extensions.
This bug has existed since TOAST was invented, so back-patch to all
supported branches.
2014-05-01 21:19:06 +02:00
|
|
|
|
|
|
|
-- Check that arrays of composites are safely detoasted when needed
|
|
|
|
|
|
|
|
create temp table src (f1 text);
|
|
|
|
insert into src
|
|
|
|
select string_agg(random()::text,'') from generate_series(1,10000);
|
|
|
|
create type textandtext as (c1 text, c2 text);
|
|
|
|
create temp table dest (f1 textandtext[]);
|
|
|
|
insert into dest select array[row(f1,f1)::textandtext] from src;
|
|
|
|
select length(md5((f1[1]).c2)) from dest;
|
|
|
|
delete from src;
|
|
|
|
select length(md5((f1[1]).c2)) from dest;
|
|
|
|
truncate table src;
|
|
|
|
drop table src;
|
|
|
|
select length(md5((f1[1]).c2)) from dest;
|
|
|
|
drop table dest;
|
|
|
|
drop type textandtext;
|
2014-09-09 21:34:10 +02:00
|
|
|
|
|
|
|
-- Tests for polymorphic-array form of width_bucket()
|
|
|
|
|
|
|
|
-- this exercises the varwidth and float8 code paths
|
|
|
|
SELECT
|
|
|
|
op,
|
|
|
|
width_bucket(op::numeric, ARRAY[1, 3, 5, 10.0]::numeric[]) AS wb_n1,
|
|
|
|
width_bucket(op::numeric, ARRAY[0, 5.5, 9.99]::numeric[]) AS wb_n2,
|
|
|
|
width_bucket(op::numeric, ARRAY[-6, -5, 2.0]::numeric[]) AS wb_n3,
|
|
|
|
width_bucket(op::float8, ARRAY[1, 3, 5, 10.0]::float8[]) AS wb_f1,
|
|
|
|
width_bucket(op::float8, ARRAY[0, 5.5, 9.99]::float8[]) AS wb_f2,
|
|
|
|
width_bucket(op::float8, ARRAY[-6, -5, 2.0]::float8[]) AS wb_f3
|
|
|
|
FROM (VALUES
|
|
|
|
(-5.2),
|
|
|
|
(-0.0000000001),
|
|
|
|
(0.000000000001),
|
|
|
|
(1),
|
|
|
|
(1.99999999999999),
|
|
|
|
(2),
|
|
|
|
(2.00000000000001),
|
|
|
|
(3),
|
|
|
|
(4),
|
|
|
|
(4.5),
|
|
|
|
(5),
|
|
|
|
(5.5),
|
|
|
|
(6),
|
|
|
|
(7),
|
|
|
|
(8),
|
|
|
|
(9),
|
|
|
|
(9.99999999999999),
|
|
|
|
(10),
|
|
|
|
(10.0000000000001)
|
|
|
|
) v(op);
|
|
|
|
|
|
|
|
-- ensure float8 path handles NaN properly
|
|
|
|
SELECT
|
|
|
|
op,
|
|
|
|
width_bucket(op, ARRAY[1, 3, 9, 'NaN', 'NaN']::float8[]) AS wb
|
|
|
|
FROM (VALUES
|
|
|
|
(-5.2::float8),
|
|
|
|
(4::float8),
|
|
|
|
(77::float8),
|
|
|
|
('NaN'::float8)
|
|
|
|
) v(op);
|
|
|
|
|
|
|
|
-- these exercise the generic fixed-width code path
|
|
|
|
SELECT
|
|
|
|
op,
|
|
|
|
width_bucket(op, ARRAY[1, 3, 5, 10]) AS wb_1
|
|
|
|
FROM generate_series(0,11) as op;
|
|
|
|
|
|
|
|
SELECT width_bucket(now(),
|
|
|
|
array['yesterday', 'today', 'tomorrow']::timestamptz[]);
|
|
|
|
|
|
|
|
-- corner cases
|
|
|
|
SELECT width_bucket(5, ARRAY[3]);
|
|
|
|
SELECT width_bucket(5, '{}');
|
|
|
|
|
|
|
|
-- error cases
|
|
|
|
SELECT width_bucket('5'::text, ARRAY[3, 4]::integer[]);
|
|
|
|
SELECT width_bucket(5, ARRAY[3, 4, NULL]);
|
|
|
|
SELECT width_bucket(5, ARRAY[ARRAY[1, 2], ARRAY[3, 4]]);
|