pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: sramsay@uga.edu
To: pgsql-sql@postgresql.org
Subject: Re: functions in WHERE clause
Date: Sun, 5 Mar 2006 15:10:43 -0500
Message-ID: <20060305201043.GA14773@cantor.english.uga.edu> (raw)
In-Reply-To: <20060305113419.P64418@megazone.bigpanda.com>
References: <20060305151652.GA14216@cantor.english.uga.edu>
	<20060305172635.GA77911@winnie.fuhr.org>
	<20060305174827.GA14433@cantor.english.uga.edu>
	<10317.1141582600@sss.pgh.pa.us>
	<20060305184903.GA11613@lachesis.english.uga.edu>
	<20060305113419.P64418@megazone.bigpanda.com>

On Sun, Mar 05, 2006 at 11:38:46AM -0800, Stephan Szabo wrote:
> > But imagine instead that this function is more generic.  You know
> > that you're trying to get something that's equal to x and equal to
> > y, but you don't know (until the function is called) what those
> > rvalues should be.  In other words, it's the 42 and the 47 that you
> > don't know until runtime -- you always know what columns your
> > searching on.
> 
> Then you need to pass those in as well as something representing the row
> that's being tested -- where clauses are filters on rows. The whole row
> representation might be better than columns for some cases.
> 
> For example:
> 
> create table tt1(a int, b int);
> create function f1(tt1, int) returns bool as 'select $1.a = $2' language
> 'sql';
> 
> select * from tt1 where f1(tt1, 1);

Eureka!  That does it:

CREATE OR REPLACE FUNCTION xpath(event, lquery) RETURNS bool AS $$
    SELECT $1.ltree ~ $2;
$$ LANGUAGE SQL;

And it's fast (the event table is a view)!

Interestingly, this version is very slow:

CREATE OR REPLACE FUNCTION xpath(event, lquery) RETURNS bool AS
$$
BEGIN
     RETURN $1.ltree ~ $2;
END
$$ LANGUAGE plpgsql STABLE;

EXPLAIN shows lots of sequential scans when you try to do it this
way.  No inlining, perhaps?

I guess I am still a little confused as to why you have to pass the
table in as a parameter -- why you can't just do:

RETURN event.ltree ~ $2;

But I'll live . . .

Thanks to one and all for the very generous assistance.  I have
learned much.

Steve

> The other option is to do this as a set returning function in the first
> place rather than trying to do a wierd where clause thing.

Mostly for syntactic clarity on the caller's end, but I understand what
you're saying.

-- 
Stephen Ramsay
Assistant Professor
Department of English
University of Georgia
email: sramsay@uga.edu
web: http://cantor.english.uga.edu/
PGP Public Key ID: 0xA38D7B11



view thread (11+ messages)  latest in thread

Message-ID: <20060305201043.GA14773@cantor.english.uga.edu>
Permalink:  ../20060305201043.GA14773@cantor.english.uga.edu/
Also on:    postgresql.org/message-id/20060305201043.GA14773@cantor.english.uga.edu

 · 

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-sql@postgresql.org
  Cc: sramsay@uga.edu
  Subject: Re: functions in WHERE clause
  In-Reply-To: <20060305201043.GA14773@cantor.english.uga.edu>

* 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