[GSoC] Question about returning bytea array

Started by Charles Cuiover 7 years ago4 messages
#1Charles Cui
charles.cui1984@gmail.com

Hi Aleksander,

I have the requirements to return a bytea array for some functions in
pg_thrift plugin.
What I did was similar to the following,

bytea** ret = palloc(len * sizeof(bytea*));
for (int i = 0; i < len; i++) {
// allocate space
ret[i] = palloc(size);
// fill in ret[i]
...
}
PG_RETURN_POINTER(ret);
I can verify each ret[i] is correct by logging, but server crash when
executed PG_RETURN_POINTER(ret).
So, I am wondering what's the reason of the crash?
and what's the recommended way (best practice) to construct a return value
to be bytea array?

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Charles Cui (#1)
Re: [GSoC] Question about returning bytea array

2018-05-17 7:03 GMT+02:00 Charles Cui <charles.cui1984@gmail.com>:

Hi Aleksander,

I have the requirements to return a bytea array for some functions in
pg_thrift plugin.
What I did was similar to the following,

bytea** ret = palloc(len * sizeof(bytea*));
for (int i = 0; i < len; i++) {
// allocate space
ret[i] = palloc(size);
// fill in ret[i]
...
}
PG_RETURN_POINTER(ret);
I can verify each ret[i] is correct by logging, but server crash when
executed PG_RETURN_POINTER(ret).
So, I am wondering what's the reason of the crash?
and what's the recommended way (best practice) to construct a return value
to be bytea array?

You should to set a size of any varlena structure.

check postgresql/src/backend/utils/adt/varlena.c code

When I started with PostgreSQL hacking my often problem was memory
allocation in bad (deallocated) context. Good to use own self compiled
version of PostgreSQL configured with --enable-cassert option.

Regards

Pavel

#3Andrew Gierth
andrew@tao11.riddles.org.uk
In reply to: Charles Cui (#1)
Re: [GSoC] Question about returning bytea array

"Charles" == Charles Cui <charles.cui1984@gmail.com> writes:

Charles> I have the requirements to return a bytea array for some
Charles> functions in pg_thrift plugin.

If you mean you want the return value to be of type bytea[], i.e. an SQL
array of bytea values, then you need to be using construct_array to
construct the result.

--
Andrew (irc:RhodiumToad)

#4Charles Cui
charles.cui1984@gmail.com
In reply to: Andrew Gierth (#3)
Re: [GSoC] Question about returning bytea array

construct_md_array works for me, thanks for inputs!

2018-05-17 13:42 GMT-07:00 Andrew Gierth <andrew@tao11.riddles.org.uk>:

Show quoted text

"Charles" == Charles Cui <charles.cui1984@gmail.com> writes:

Charles> I have the requirements to return a bytea array for some
Charles> functions in pg_thrift plugin.

If you mean you want the return value to be of type bytea[], i.e. an SQL
array of bytea values, then you need to be using construct_array to
construct the result.

--
Andrew (irc:RhodiumToad)