Skip to content

Commit dca44d0

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 d589571 commit dca44d0

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
@@ -334,8 +334,19 @@ datumSerialize(Datum value, bool isnull, bool typByVal, int typLen,
334334
}
335335
else if (eoh)
336336
{
337-
EOH_flatten_into(eoh, (void *) *start_address, header);
337+
char *tmp;
338+
339+
/*
340+
* EOH_flatten_into expects the target address to be maxaligned,
341+
* so we can't store directly to *start_address.
342+
*/
343+
tmp = (char *) palloc(header);
344+
EOH_flatten_into(eoh, (void *) tmp, header);
345+
memcpy(*start_address, tmp, header);
338346
*start_address += header;
347+
348+
/* be tidy. */
349+
pfree(tmp);
339350
}
340351
else
341352
{

src/test/regress/expected/select_parallel.out

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,33 @@ explain (costs off)
235235
Index Cond: (unique1 = 1)
236236
(5 rows)
237237

238+
-- test passing expanded-value representations to workers
239+
CREATE FUNCTION make_some_array(int,int) returns int[] as
240+
$$declare x int[];
241+
begin
242+
x[1] := $1;
243+
x[2] := $2;
244+
return x;
245+
end$$ language plpgsql parallel safe;
246+
CREATE TABLE fooarr(f1 text, f2 int[], f3 text);
247+
INSERT INTO fooarr VALUES('1', ARRAY[1,2], 'one');
248+
PREPARE pstmt(text, int[]) AS SELECT * FROM fooarr WHERE f1 = $1 AND f2 = $2;
249+
EXPLAIN (COSTS OFF) EXECUTE pstmt('1', make_some_array(1,2));
250+
QUERY PLAN
251+
------------------------------------------------------------------
252+
Gather
253+
Workers Planned: 3
254+
-> Parallel Seq Scan on fooarr
255+
Filter: ((f1 = '1'::text) AND (f2 = '{1,2}'::integer[]))
256+
(4 rows)
257+
258+
EXECUTE pstmt('1', make_some_array(1,2));
259+
f1 | f2 | f3
260+
----+-------+-----
261+
1 | {1,2} | one
262+
(1 row)
263+
264+
DEALLOCATE pstmt;
238265
do $$begin
239266
-- Provoke error, possibly in worker. If this error happens to occur in
240267
-- the worker, there will be a CONTEXT line which must be hidden.

src/test/regress/sql/select_parallel.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ explain (costs off)
9696
explain (costs off)
9797
select stringu1::int2 from tenk1 where unique1 = 1;
9898

99+
-- test passing expanded-value representations to workers
100+
CREATE FUNCTION make_some_array(int,int) returns int[] as
101+
$$declare x int[];
102+
begin
103+
x[1] := $1;
104+
x[2] := $2;
105+
return x;
106+
end$$ language plpgsql parallel safe;
107+
CREATE TABLE fooarr(f1 text, f2 int[], f3 text);
108+
INSERT INTO fooarr VALUES('1', ARRAY[1,2], 'one');
109+
110+
PREPARE pstmt(text, int[]) AS SELECT * FROM fooarr WHERE f1 = $1 AND f2 = $2;
111+
EXPLAIN (COSTS OFF) EXECUTE pstmt('1', make_some_array(1,2));
112+
EXECUTE pstmt('1', make_some_array(1,2));
113+
DEALLOCATE pstmt;
114+
99115
do $$begin
100116
-- Provoke error, possibly in worker. If this error happens to occur in
101117
-- the worker, there will be a CONTEXT line which must be hidden.

0 commit comments

Comments
 (0)