Re: [GSoC] Question about returning bytea array

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Charles Cui <charles(dot)cui1984(at)gmail(dot)com>
Cc: Aleksander Alekseev <a(dot)alekseev(at)postgrespro(dot)ru>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Anastasia Lubennikova <a(dot)lubennikova(at)postgrespro(dot)ru>, Andrey Borodin <x4mmm(at)yandex-team(dot)ru>
Subject: Re: [GSoC] Question about returning bytea array
Date: 2018-05-17 05:15:25
Message-ID: CAFj8pRD1u1Jxn6ZiX-=jOp6f_V37S6Ddu0vM-TbHk-LQsQr0GQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

2018-05-17 7:03 GMT+02:00 Charles Cui <charles(dot)cui1984(at)gmail(dot)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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2018-05-17 05:15:48 Re: Needless additional partition check in INSERT?
Previous Message Charles Cui 2018-05-17 05:03:10 [GSoC] Question about returning bytea array