Skip to content

Commit 9718c93

Browse files
author
Amit Kapila
committed
MAXALIGN the target address where we store flattened value.
The API (EOH_flatten_into) that flattens the expanded value representation expects the target address to be maxaligned. All it's usage adhere to that principle except when serializing datums for parallel query. Fix that usage. Diagnosed-by: Tom Lane Author: Tom Lane and Amit Kapila Backpatch-through: 9.6 Discussion: https://postgr.es/m/11629.1536550032@sss.pgh.pa.us
1 parent 6483381 commit 9718c93

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/backend/utils/adt/datum.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,19 @@ datumSerialize(Datum value, bool isnull, bool typByVal, int typLen,
338338
}
339339
else if (eoh)
340340
{
341-
EOH_flatten_into(eoh, (void *) *start_address, header);
341+
char *tmp;
342+
343+
/*
344+
* EOH_flatten_into expects the target address to be maxaligned,
345+
* so we can't store directly to *start_address.
346+
*/
347+
tmp = (char *) palloc(header);
348+
EOH_flatten_into(eoh, (void *) tmp, header);
349+
memcpy(*start_address, tmp, header);
342350
*start_address += header;
351+
352+
/* be tidy. */
353+
pfree(tmp);
343354
}
344355
else
345356
{

src/test/regress/expected/select_parallel.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,33 @@ EXPLAIN (analyze, timing off, summary off, costs off) SELECT * FROM tenk1;
628628
-> Parallel Seq Scan on tenk1 (actual rows=2000 loops=5)
629629
(4 rows)
630630

631+
-- test passing expanded-value representations to workers
632+
CREATE FUNCTION make_some_array(int,int) returns int[] as
633+
$$declare x int[];
634+
begin
635+
x[1] := $1;
636+
x[2] := $2;
637+
return x;
638+
end$$ language plpgsql parallel safe;
639+
CREATE TABLE fooarr(f1 text, f2 int[], f3 text);
640+
INSERT INTO fooarr VALUES('1', ARRAY[1,2], 'one');
641+
PREPARE pstmt(text, int[]) AS SELECT * FROM fooarr WHERE f1 = $1 AND f2 = $2;
642+
EXPLAIN (COSTS OFF) EXECUTE pstmt('1', make_some_array(1,2));
643+
QUERY PLAN
644+
------------------------------------------------------------------
645+
Gather
646+
Workers Planned: 3
647+
-> Parallel Seq Scan on fooarr
648+
Filter: ((f1 = '1'::text) AND (f2 = '{1,2}'::integer[]))
649+
(4 rows)
650+
651+
EXECUTE pstmt('1', make_some_array(1,2));
652+
f1 | f2 | f3
653+
----+-------+-----
654+
1 | {1,2} | one
655+
(1 row)
656+
657+
DEALLOCATE pstmt;
631658
-- provoke error in worker
632659
select stringu1::int2 from tenk1 where unique1 = 1;
633660
ERROR: invalid input syntax for integer: "BAAAAA"

src/test/regress/sql/select_parallel.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,22 @@ explain (costs off)
218218
-- to increase the parallel query test coverage
219219
EXPLAIN (analyze, timing off, summary off, costs off) SELECT * FROM tenk1;
220220

221+
-- test passing expanded-value representations to workers
222+
CREATE FUNCTION make_some_array(int,int) returns int[] as
223+
$$declare x int[];
224+
begin
225+
x[1] := $1;
226+
x[2] := $2;
227+
return x;
228+
end$$ language plpgsql parallel safe;
229+
CREATE TABLE fooarr(f1 text, f2 int[], f3 text);
230+
INSERT INTO fooarr VALUES('1', ARRAY[1,2], 'one');
231+
232+
PREPARE pstmt(text, int[]) AS SELECT * FROM fooarr WHERE f1 = $1 AND f2 = $2;
233+
EXPLAIN (COSTS OFF) EXECUTE pstmt('1', make_some_array(1,2));
234+
EXECUTE pstmt('1', make_some_array(1,2));
235+
DEALLOCATE pstmt;
236+
221237
-- provoke error in worker
222238
select stringu1::int2 from tenk1 where unique1 = 1;
223239

0 commit comments

Comments
 (0)