Re: Modifying WHERE input conditions for a VIEW

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tony Holmes <tony(at)crosswinds(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Modifying WHERE input conditions for a VIEW
Date: 2003-03-30 20:54:19
Message-ID: 6330.1049057659@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Tony Holmes <tony(at)crosswinds(dot)net> writes:
> I then attempted to use a SELECT rule on that view. It looked like
> this (the view was imap_lookup):

> CREATE RULE imap_lookup AS
> ON SELECT TO imap_lookup
> WHERE strpos(username, '@foo.com') DO INSTEAD

This isn't going to work; we do not support WHERE conditions in ON
SELECT rules at all. (There is no difference between ON SELECT rules
and views, actually.)

>> My advice is to change the schema.

> I agree, but I'm kinda bound to what we have at the moment. Changing it
> is in the plans, but more long term.

You might be able to make it work efficiently with a functional index.
To meet the syntactic limitations of functional indexes, you'd need to
define a function "addfoo(text) returns text" that returns the input
with "@foo.com" appended. (Be sure to mark it strict and immutable.)
Now you can create an index on addfoo(username) for foo_users, and
then your view becomes

CREATE VIEW all_users as
SELECT uid, addfoo(username) AS username, password, active
FROM foo_users
UNION ALL
SELECT uid, username, password, active
FROM domain_users

I haven't tested, but I believe 7.3 will optimize this fairly decently
when the query is "SELECT ... FROM all_users WHERE username = something'.

regards, tom lane

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Ennio-Sr 2003-03-30 21:29:53 Re: Extended display and extended ascii characters
Previous Message Tony Holmes 2003-03-30 20:15:47 Re: Modifying WHERE input conditions for a VIEW