Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZPbAE-0003gW-Ro for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2015 18:54:15 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZPbAE-0005VG-4z for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2015 18:54:14 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZPb8u-00043Z-NH for pgsql-hackers@postgresql.org; Wed, 12 Aug 2015 18:52:53 +0000 Received: from smtp-auth.no-ip.com ([8.23.224.61] helo=out.smtp-auth.no-ip.com) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZPb8q-0000y6-IX for pgsql-hackers@postgresql.org; Wed, 12 Aug 2015 18:52:51 +0000 X-No-IP: alvh.no-ip.org@noip-smtp X-Report-Spam-To: abuse@no-ip.com Received: from alvin.alvh.no-ip.org (unknown [200.104.169.63]) (Authenticated sender: alvh.no-ip.org@noip-smtp) by smtp-auth.no-ip.com (Postfix) with ESMTPA id 84E19401205; Wed, 12 Aug 2015 11:52:46 -0700 (PDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id E25EE3E4D; Wed, 12 Aug 2015 15:52:42 -0300 (CLT) Date: Wed, 12 Aug 2015 15:52:42 -0300 From: Alvaro Herrera To: Tom Lane Cc: Christoph Berg , pgsql-hackers@postgresql.org Subject: Re: [COMMITTERS] Using contrib modules in check (Re: pgsql: Fix BRIN to use SnapshotAny during summarization) Message-ID: <20150812185242.GL3040@postgresql.org> References: <928.1438900846@sss.pgh.pa.us> <20150810110038.GA20393@msg.df7cb.de> <2713.1439215712@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="UHN/qo2QbUvPLonB" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2713.1439215712@sss.pgh.pa.us> User-Agent: Mutt/1.5.23 (2014-03-12) X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --UHN/qo2QbUvPLonB Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Tom Lane wrote: > However, we did learn something valuable from the fact that all the > -DCLOBBER_CACHE_ALWAYS critters failed on it: per my earlier message, > brin_page_items() is unsafe against a relcache flush on the index. > I'll put that on the 9.5 open items list. > > (If I were tasked with fixing it, I'd be tempted to rewrite it to do > all the work in one call and return a tuplestore; the alternative > seems to be to try to keep the index open across multiple calls, > which would be a mess.) Here's a patch doing that. -- Álvaro Herrera http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --UHN/qo2QbUvPLonB Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="brin-page-items.patch" Content-Transfer-Encoding: quoted-printable commit 41eba17795e9cf9b0c23e05def5d33b0dbdd4807=1B[m Author: Alvaro Herrera AuthorDate: Wed Aug 12 15:41:15 2015 -0300 CommitDate: Wed Aug 12 15:41:15 2015 -0300 fix brin_page_items to use a tuplestore diff --git a/contrib/pageinspect/brinfuncs.c b/contrib/pageinspect/brinfunc= s.c index 7adcfa8..f695b18 100644 --- a/contrib/pageinspect/brinfuncs.c +++ b/contrib/pageinspect/brinfuncs.c @@ -37,18 +37,6 @@ typedef struct brin_column_state FmgrInfo outputFn[FLEXIBLE_ARRAY_MEMBER]; } brin_column_state; =20 -typedef struct brin_page_state -{ - BrinDesc *bdesc; - Page page; - OffsetNumber offset; - bool unusedItem; - bool done; - AttrNumber attno; - BrinMemTuple *dtup; - brin_column_state *columns[FLEXIBLE_ARRAY_MEMBER]; -} brin_page_state; - =20 static Page verify_brin_page(bytea *raw_page, uint16 type, const char *strtype); @@ -119,89 +107,90 @@ verify_brin_page(bytea *raw_page, uint16 type, const = char *strtype) Datum brin_page_items(PG_FUNCTION_ARGS) { - brin_page_state *state; - FuncCallContext *fctx; + bytea *raw_page =3D PG_GETARG_BYTEA_P(0); + Oid indexRelid =3D PG_GETARG_OID(1); + ReturnSetInfo *rsinfo =3D (ReturnSetInfo *) fcinfo->resultinfo; + TupleDesc tupdesc; + MemoryContext oldcontext; + Tuplestorestate *tupstore; + Relation indexRel; + Page page; + OffsetNumber offset; + AttrNumber attno; + bool unusedItem; + BrinDesc *bdesc; + BrinMemTuple *dtup; + brin_column_state **columns; =20 if (!superuser()) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), (errmsg("must be superuser to use raw page functions")))); =20 - if (SRF_IS_FIRSTCALL()) + /* check to see if caller supports us returning a tuplestore */ + if (rsinfo =3D=3D NULL || !IsA(rsinfo, ReturnSetInfo)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("set-valued function called in context that cannot accept a se= t"))); + if (!(rsinfo->allowedModes & SFRM_Materialize) || + rsinfo->expectedDesc =3D=3D NULL) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("materialize mode required, but it is not allowed in this cont= ext"))); + + /* Build a tuple descriptor for our result type */ + if (get_call_result_type(fcinfo, NULL, &tupdesc) !=3D TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + + /* Build tuplestore to hold the result rows */ + oldcontext =3D MemoryContextSwitchTo(rsinfo->econtext->ecxt_per_query_mem= ory); + + tupstore =3D tuplestore_begin_heap(true, false, work_mem); + rsinfo->returnMode =3D SFRM_Materialize; + rsinfo->setResult =3D tupstore; + rsinfo->setDesc =3D tupdesc; + + MemoryContextSwitchTo(oldcontext); + + indexRel =3D index_open(indexRelid, AccessShareLock); + + /* minimally verify the page we got */ + page =3D verify_brin_page(raw_page, BRIN_PAGETYPE_REGULAR, "regular"); + + bdesc =3D brin_build_desc(indexRel); + offset =3D FirstOffsetNumber; + unusedItem =3D false; + dtup =3D NULL; + + /* + * Initialize output functions for all indexed datatypes; simplifies + * calling them later. + */ + columns =3D palloc(sizeof(brin_column_state *) * RelationGetDescr(indexRe= l)->natts); + for (attno =3D 1; attno <=3D bdesc->bd_tupdesc->natts; attno++) { - bytea *raw_page =3D PG_GETARG_BYTEA_P(0); - Oid indexRelid =3D PG_GETARG_OID(1); - Page page; - TupleDesc tupdesc; - MemoryContext mctx; - Relation indexRel; - AttrNumber attno; + Oid output; + bool isVarlena; + BrinOpcInfo *opcinfo; + int i; + brin_column_state *column; =20 - /* minimally verify the page we got */ - page =3D verify_brin_page(raw_page, BRIN_PAGETYPE_REGULAR, "regular"); + opcinfo =3D bdesc->bd_info[attno - 1]; + column =3D palloc(offsetof(brin_column_state, outputFn) + + sizeof(FmgrInfo) * opcinfo->oi_nstored); =20 - /* create a function context for cross-call persistence */ - fctx =3D SRF_FIRSTCALL_INIT(); - - /* switch to memory context appropriate for multiple function calls */ - mctx =3D MemoryContextSwitchTo(fctx->multi_call_memory_ctx); - - /* Build a tuple descriptor for our result type */ - if (get_call_result_type(fcinfo, NULL, &tupdesc) !=3D TYPEFUNC_COMPOSITE) - elog(ERROR, "return type must be a row type"); - - indexRel =3D index_open(indexRelid, AccessShareLock); - - state =3D palloc(offsetof(brin_page_state, columns) + - sizeof(brin_column_state) * RelationGetDescr(indexRel)->natts); - - state->bdesc =3D brin_build_desc(indexRel); - state->page =3D page; - state->offset =3D FirstOffsetNumber; - state->unusedItem =3D false; - state->done =3D false; - state->dtup =3D NULL; - - /* - * Initialize output functions for all indexed datatypes; simplifies - * calling them later. - */ - for (attno =3D 1; attno <=3D state->bdesc->bd_tupdesc->natts; attno++) + column->nstored =3D opcinfo->oi_nstored; + for (i =3D 0; i < opcinfo->oi_nstored; i++) { - Oid output; - bool isVarlena; - BrinOpcInfo *opcinfo; - int i; - brin_column_state *column; - - opcinfo =3D state->bdesc->bd_info[attno - 1]; - column =3D palloc(offsetof(brin_column_state, outputFn) + - sizeof(FmgrInfo) * opcinfo->oi_nstored); - - column->nstored =3D opcinfo->oi_nstored; - for (i =3D 0; i < opcinfo->oi_nstored; i++) - { - getTypeOutputInfo(opcinfo->oi_typcache[i]->type_id, &output, &isVarlen= a); - fmgr_info(output, &column->outputFn[i]); - } - - state->columns[attno - 1] =3D column; + getTypeOutputInfo(opcinfo->oi_typcache[i]->type_id, &output, &isVarlena= ); + fmgr_info(output, &column->outputFn[i]); } =20 - index_close(indexRel, AccessShareLock); - - fctx->user_fctx =3D state; - fctx->tuple_desc =3D BlessTupleDesc(tupdesc); - - MemoryContextSwitchTo(mctx); + columns[attno - 1] =3D column; } =20 - fctx =3D SRF_PERCALL_SETUP(); - state =3D fctx->user_fctx; - - if (!state->done) + for (;;) { - HeapTuple result; Datum values[7]; bool nulls[7]; =20 @@ -211,39 +200,32 @@ brin_page_items(PG_FUNCTION_ARGS) * signal for obtaining and decoding the next one. If that's not the * case, we output the next attribute. */ - if (state->dtup =3D=3D NULL) + if (dtup =3D=3D NULL) { BrinTuple *tup; - MemoryContext mctx; ItemId itemId; =20 - /* deformed tuple must live across calls */ - mctx =3D MemoryContextSwitchTo(fctx->multi_call_memory_ctx); - /* verify item status: if there's no data, we can't decode */ - itemId =3D PageGetItemId(state->page, state->offset); + itemId =3D PageGetItemId(page, offset); if (ItemIdIsUsed(itemId)) { - tup =3D (BrinTuple *) PageGetItem(state->page, - PageGetItemId(state->page, - state->offset)); - state->dtup =3D brin_deform_tuple(state->bdesc, tup); - state->attno =3D 1; - state->unusedItem =3D false; + tup =3D (BrinTuple *) + PageGetItem(page, PageGetItemId(page, offset)); + dtup =3D brin_deform_tuple(bdesc, tup); + attno =3D 1; + unusedItem =3D false; } else - state->unusedItem =3D true; - - MemoryContextSwitchTo(mctx); + unusedItem =3D true; } else - state->attno++; + attno++; =20 MemSet(nulls, 0, sizeof(nulls)); =20 - if (state->unusedItem) + if (unusedItem) { - values[0] =3D UInt16GetDatum(state->offset); + values[0] =3D UInt16GetDatum(offset); nulls[1] =3D true; nulls[2] =3D true; nulls[3] =3D true; @@ -253,17 +235,17 @@ brin_page_items(PG_FUNCTION_ARGS) } else { - int att =3D state->attno - 1; + int att =3D attno - 1; =20 - values[0] =3D UInt16GetDatum(state->offset); - values[1] =3D UInt32GetDatum(state->dtup->bt_blkno); - values[2] =3D UInt16GetDatum(state->attno); - values[3] =3D BoolGetDatum(state->dtup->bt_columns[att].bv_allnulls); - values[4] =3D BoolGetDatum(state->dtup->bt_columns[att].bv_hasnulls); - values[5] =3D BoolGetDatum(state->dtup->bt_placeholder); - if (!state->dtup->bt_columns[att].bv_allnulls) + values[0] =3D UInt16GetDatum(offset); + values[1] =3D UInt32GetDatum(dtup->bt_blkno); + values[2] =3D UInt16GetDatum(attno); + values[3] =3D BoolGetDatum(dtup->bt_columns[att].bv_allnulls); + values[4] =3D BoolGetDatum(dtup->bt_columns[att].bv_hasnulls); + values[5] =3D BoolGetDatum(dtup->bt_placeholder); + if (!dtup->bt_columns[att].bv_allnulls) { - BrinValues *bvalues =3D &state->dtup->bt_columns[att]; + BrinValues *bvalues =3D &dtup->bt_columns[att]; StringInfoData s; bool first; int i; @@ -272,14 +254,14 @@ brin_page_items(PG_FUNCTION_ARGS) appendStringInfoChar(&s, '{'); =20 first =3D true; - for (i =3D 0; i < state->columns[att]->nstored; i++) + for (i =3D 0; i < columns[att]->nstored; i++) { char *val; =20 if (!first) appendStringInfoString(&s, " .. "); first =3D false; - val =3D OutputFunctionCall(&state->columns[att]->outputFn[i], + val =3D OutputFunctionCall(&columns[att]->outputFn[i], bvalues->bv_values[i]); appendStringInfoString(&s, val); pfree(val); @@ -295,35 +277,35 @@ brin_page_items(PG_FUNCTION_ARGS) } } =20 - result =3D heap_form_tuple(fctx->tuple_desc, values, nulls); + tuplestore_putvalues(tupstore, tupdesc, values, nulls); =20 /* * If the item was unused, jump straight to the next one; otherwise, * the only cleanup needed here is to set our signal to go to the next * tuple in the following iteration, by freeing the current one. */ - if (state->unusedItem) - state->offset =3D OffsetNumberNext(state->offset); - else if (state->attno >=3D state->bdesc->bd_tupdesc->natts) + if (unusedItem) + offset =3D OffsetNumberNext(offset); + else if (attno >=3D bdesc->bd_tupdesc->natts) { - pfree(state->dtup); - state->dtup =3D NULL; - state->offset =3D OffsetNumberNext(state->offset); + pfree(dtup); + dtup =3D NULL; + offset =3D OffsetNumberNext(offset); } =20 /* - * If we're beyond the end of the page, set flag to end the function - * in the following iteration. + * If we're beyond the end of the page, we're done. */ - if (state->offset > PageGetMaxOffsetNumber(state->page)) - state->done =3D true; - - SRF_RETURN_NEXT(fctx, HeapTupleGetDatum(result)); + if (offset > PageGetMaxOffsetNumber(page)) + break; } =20 - brin_free_desc(state->bdesc); + /* clean up and return the tuplestore */ + brin_free_desc(bdesc); + tuplestore_donestoring(tupstore); + index_close(indexRel, AccessShareLock); =20 - SRF_RETURN_DONE(fctx); + return (Datum) 0; } =20 Datum --UHN/qo2QbUvPLonB Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --UHN/qo2QbUvPLonB--