Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdCWO-0004fI-Pn for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 10:38:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fdCWN-0003v7-5n for pgsql-hackers@arkaria.postgresql.org; Wed, 11 Jul 2018 10:38:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdCWM-0003v0-VF for pgsql-hackers@lists.postgresql.org; Wed, 11 Jul 2018 10:38:55 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fdCWJ-0001pk-Kl for pgsql-hackers@postgresql.org; Wed, 11 Jul 2018 10:38:54 +0000 Received: from [192.168.127.1] (port=19231 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1fdCWF-000He3-OJ; Wed, 11 Jul 2018 10:38:47 +0000 Received: from [127.0.0.1] (port=49150 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1fdCWE-000LzD-Qp; Wed, 11 Jul 2018 10:38:46 +0000 From: Andrew Gierth To: Michael Paquier Cc: Dmitry Dolgov <9erthalion6@gmail.com>, PostgreSQL-development , Tom Lane Subject: Re: Problem with tupdesc in jsonb_to_recordset In-Reply-To: <20180711073916.GD14301@paquier.xyz> (Michael Paquier's message of "Wed, 11 Jul 2018 16:39:16 +0900") Message-ID: <87fu0qkrfr.fsf@news-spur.riddles.org.uk> References: <20180711062713.GA14301@paquier.xyz> <20180711073916.GD14301@paquier.xyz> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 11 Jul 2018 11:38:46 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Michael" == Michael Paquier writes: >> I don't think that your solution is correct. From my read of >> 37a795a6, the tuple descriptor is moved from the query-lifespan >> memory context (ecxt_per_query_memory) to a function-level context, >> which could cause the descriptor to become busted as your test is >> pointing out. Tom? Michael> Hacking my way out I am finishing with the attached, which Michael> fixes the problem and passes all regression tests. I am not Michael> completely sure that this the right approach though, I would Michael> need to think a bit more about it. What's happening in the original case is this: the SRF call protocol says that it's the executor's responsibility to free rsi.setDesc if it's not refcounted, under the assumption that in such a case it's in per-query memory (and not in either a shorter-lived or longer-lived context). The change to update_cached_tupdesc broke the protocol by causing populate_recordset_worker to set rsi.setDesc to a non-refcounted tupdesc allocated in a context that's not the per-query context. What's not clear here is why populate_recordset_record thinks it needs to update the tupdesc for every record in a single result set. The entire result set will ultimately be treated as having the same tupdesc regardless, so any per-record changes will break things later anyway. Your latest proposed fix isn't OK because it puts the wrong context in cache->fn_mcxt - data that's dangled off flinfo->fn_extra must NOT be in any context that has a different lifetime than flinfo->fn_mcxt (which might not be query lifetime), unless you're taking specific steps to free or invalidate it at the correct point. My first approach - assuming that update_cached_tupdesc has good reason to make a copy, which I'm not convinced is the case - would be to simply make a per-query-context copy of the tupdesc to assign to rsi.setDesc in order to conform to the call protocol. -- Andrew (irc:RhodiumToad)