pg.ddx.io  pgsql-performance@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Conway <mail@joeconway.com>
To: Eliot Gable <egable+pgsql-performance@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Merlin Moncure <mmoncure@gmail.com>
Cc: Craig Ringer <craig@postnewspapers.com.au>
Cc: Craig James <craig_james@emolecules.com>
Cc: pgsql-performance@postgresql.org
Subject: Re: Highly Efficient Custom Sorting
Date: Tue, 06 Jul 2010 13:00:33 -0700
Message-ID: <4C338B61.60409@joeconway.com> (raw)
In-Reply-To: <AANLkTikLPKC8ebtLs6qxR85A4aPnH5klOwljROl4D2eu@mail.gmail.com>
References: <AANLkTikrA8M3cncIHkFqsJ78TzCnQdriE0GjvK8XjB9E@mail.gmail.com>
	<4C2D574E.5030308@postnewspapers.com.au>
	<4767.1278043730@sss.pgh.pa.us>
	<AANLkTimZISmcXkPN059pz5bx-TXKVOnm6iuJ4y4qQedu@mail.gmail.com>
	<4C2E1587.70001@emolecules.com>
	<4C2E9606.8050903@postnewspapers.com.au>
	<AANLkTimaUPkOn6OkCspsZ7iTiY9S6MFTrKTgFXKckl2u@mail.gmail.com>
	<AANLkTikn4wgydmzY79aH2UrLAXPPrMVw8Uu-fNLjCEZk@mail.gmail.com>
	<AANLkTiln7OejssTCZzsnbvYnXOyJL_v5CkEhZ9asHy6X@mail.gmail.com>
	<AANLkTik51nNmr6E_fu5lJtwCmvRhl3frzoMsgvB1-qf2@mail.gmail.com>
	<AANLkTikLPKC8ebtLs6qxR85A4aPnH5klOwljROl4D2eu@mail.gmail.com>

On 07/06/2010 12:42 PM, Eliot Gable wrote:
> Thanks for suggesting array_unnest(). I think that will actually prove
> more useful to me than the other example I'm using for extracting my
> data from an array. I was actually planning on computing the order on
> the first call and storing it in a linked list which gets returned one
> item at a time until all rows have been returned. Also, I found a code
> example using Google that showed someone storing data across function
> calls using that pointer. I used their example to produce this:
> 
> <snip>
>     if(SRF_IS_FIRSTCALL()) {
>         funcctx = SRF_FIRSTCALL_INIT();
> 
>         /* This is where we stick or sorted data for returning later */
>         funcctx->user_fctx =
> MemoryContextAlloc(funcctx->multi_call_memory_ctx, sizeof(sort_data));
>         oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
>         data = (sort_data*) funcctx->user_fctx;
> </snip>
> 
> I have a structure set up that is typedef'd to "sort_data" which stores
> pointers to various things that I need to survive across the calls.
> Since this seems to be what you are suggesting, I assume this is the
> correct approach.

This approach works, but you could also use the SFRM_Materialize mode
and calculate the entire result set in one go. That tends to be simpler.
See, for example crosstab_hash() in contrib/tablefunc for an example.

FWIW, there are also some good examples of array handling in PL/R, e.g.
pg_array_get_r() in pg_conversion.c

HTH,

Joe

-- 
Joe Conway
credativ LLC: http://www.credativ.us
Linux, PostgreSQL, and general Open Source
Training, Service, Consulting, & Support

Attachments:

  signature.asc (application/pgp-signature, 898B) download

view thread (22+ messages)  latest in thread

Message-ID: <4C338B61.60409@joeconway.com>
Permalink:  ../4C338B61.60409@joeconway.com/
Also on:    postgresql.org/message-id/4C338B61.60409@joeconway.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-performance@postgresql.org
  Cc: mail@joeconway.com, egable+pgsql-performance@gmail.com, robertmhaas@gmail.com, mmoncure@gmail.com, craig@postnewspapers.com.au, craig_james@emolecules.com
  Subject: Re: Highly Efficient Custom Sorting
  In-Reply-To: <4C338B61.60409@joeconway.com>

* 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