pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andres Freund <andres@anarazel.de>
To: Dmitry Dolgov <9erthalion6@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: PostgreSQL mailing lists <pgsql-bugs@postgresql.org>
Subject: Re: LLVM jit and matview
Date: Wed, 25 Jul 2018 14:47:02 -0700
Message-ID: <20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de> (raw)
In-Reply-To: <20180725154129.lvaabpypucdajliz@alap3.anarazel.de>
References: <CA+q6zcWO7CeAJtHBxgcHn_hj+PenM=tvG0RJ93X1uEJ86+76Ug@mail.gmail.com>
	<20180710001958.GD1661@paquier.xyz>
	<20180725024956.6gxqscgk5fmuqbb7@alap3.anarazel.de>
	<20180725054857.GB6660@paquier.xyz>
	<CA+q6zcVFD9601Bfo43dPtLOtWHTYt7A5NqNcsRUMzZX64dBO3A@mail.gmail.com>
	<20180725154129.lvaabpypucdajliz@alap3.anarazel.de>

Hi,

On 2018-07-25 08:41:29 -0700, Andres Freund wrote:
> Oh, interesting. It only crashes when compiling LLVM without LLVM's
> asserts enabled, even when using exactly the same LLVM checkout for both
> builds. No idea what that's about, yet.

Oh, well, this took me longer than it should have.  The problem, to my
possibly demented mind, is actually kinda funny:

The problematic expression in the query invokes ExecEvalRowNull() (it's
EEOP_WHOLEROW followed by EEOP_NULLTEST_ROWISNOTNULL). With inlining
enabled, that ends up inlining ExecEvalRowNull(), ExecEvalRowNullInt(),
get_cached_rowtype(), ShutdownTupleDescRef() (largely because these are
static functions that'd otherwise prevent ExecEvalRowNull from being
inlined).
get_cached_rowtype() does
            /* Need to register shutdown callback to release tupdesc */
            RegisterExprContextCallback(econtext,
                                        ShutdownTupleDescRef,
                                        PointerGetDatum(cache_field));
which, in the inlined version, means that ShutdownTupleDescRef is the
inlined copy. So, the query execution sets up a shutdown callback, that
is a JITed function.

But note standard_ExecutorEnd():

	/* release JIT context, if allocated */
	if (estate->es_jit)
	{
		jit_release_context(estate->es_jit);
		estate->es_jit = NULL;
	}

	/*
	 * Must switch out of context before destroying it
	 */
	MemoryContextSwitchTo(oldcontext);

	/*
	 * Release EState and per-query memory context.  This should release
	 * everything the executor has allocated.
	 */
	FreeExecutorState(estate);

FreeExecutorState(), which calls the shutdown callbacks, is executed
*after* the JIT context is destroyed! Thereby causing the segfault, as
the shutdown callback now invokes unmapped memory.

The fix is easy, releasing the JIT context should just happen in
FreeExecutorState(). Only thing is that that function has the following
comment in the header:
 * Note: this is not responsible for releasing non-memory resources,
 * such as open relations or buffer pins.  But it will shut down any
 * still-active ExprContexts within the EState.  That is sufficient
 * cleanup for situations where the EState has only been used for expression
 * evaluation, and not to run a complete Plan.

I don't really think throwing away functions is a violation of that, but
I think it's possible to argue the other way?

Greetings,

Andres Freund




view thread (12+ messages)  latest in thread

Message-ID: <20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de>
Permalink:  ../20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de/
Also on:    postgresql.org/message-id/20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: andres@anarazel.de, 9erthalion6@gmail.com, michael@paquier.xyz, pgsql-hackers@postgresql.org
  Subject: Re: LLVM jit and matview
  In-Reply-To: <20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de>
  References: <CA+q6zcWO7CeAJtHBxgcHn_hj+PenM=tvG0RJ93X1uEJ86+76Ug@mail.gmail.com> <20180710001958.GD1661@paquier.xyz> <20180725024956.6gxqscgk5fmuqbb7@alap3.anarazel.de> <20180725054857.GB6660@paquier.xyz> <CA+q6zcVFD9601Bfo43dPtLOtWHTYt7A5NqNcsRUMzZX64dBO3A@mail.gmail.com> <20180725154129.lvaabpypucdajliz@alap3.anarazel.de> <20180725214702.6v7jxi2zkcv3exar@alap3.anarazel.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox