Re: checking if jsonb was detoasted and releasing memory

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Igor Stassiy <istassiy(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: checking if jsonb was detoasted and releasing memory
Date: 2015-02-12 15:04:43
Message-ID: 14615.1423753483@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Igor Stassiy <istassiy(at)gmail(dot)com> writes:
> Let us say that the datum of type Datum contains a Jsonb* type.
> Then after the call

> Jsonb *jb = DatumGetJsonb(datum);

> the jb might point to a palloc'ed structure, in case detoasting took place.
> So the question is if this is the right way to free up the memory after
> checking that the jb was detoasted?

> if ((void *)jb != DatumGetPointer(datum))
> pfree(jb); // free if detoasted

For the most part the answer is "don't bother". Except in very narrow
cases, it's not worth it to clean up such values retail as opposed to
letting the next evaluation context reset get rid of them in bulk.

The code you proposed will work, but it's ugly and usually a net waste
of cycles. It also embeds assumptions we might want to change someday.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ted Toth 2015-02-12 16:27:42 9.5 RLS 'check policy' function arguments
Previous Message Igor Stassiy 2015-02-12 14:26:46 checking if jsonb was detoasted and releasing memory