Re: Postgres Function in plpgsql, returning null results to Postfix

This is a discussion on Re: Postgres Function in plpgsql, returning null results to Postfix within the mailing.postfix.users forums, part of the Mail Servers and Related category; Thanks to all those who replied. I can get things to work if the plpgsql function returns "SETOF", ...


Go Back   Usenet Forums > Mail Servers and Related > mailing.postfix.users

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-25-2005
Simon Waters
 
Posts: n/a
Default Re: Postgres Function in plpgsql, returning null results to Postfix

Thanks to all those who replied.

I can get things to work if the plpgsql function returns "SETOF", rather than
write a function that returns one record (thanks Steve).

i.e. This sort of function doesn't work (unless I'm missing something Javier
has a function like this).

CREATE OR REPLACE FUNCTION virtual_user(VARCHAR) RETURNS VARCHAR as '
DECLARE
fname ALIAS FOR $1;
result RECORD;
BEGIN
SELECT INTO result touser FROM email_virtual WHERE fromuser = fname ;
IF NOT FOUND THEN
result.touser:=NULL;
END IF;
RETURN result.touser ;
END;
' LANGUAGE plpgsql;

This one does.

CREATE OR REPLACE FUNCTION virtual_users(VARCHAR) RETURNS SETOF VARCHAR as '
DECLARE

-- Intended for Postfix virtual_alias_maps

fname ALIAS FOR $1;
result RECORD;

BEGIN
FOR result IN SELECT touser FROM email_virtual WHERE fromuser = fname LOOP
RETURN NEXT result.touser ;
END LOOP;
RETURN;
END;
' LANGUAGE plpgsql;

The later needs to be called as "query = select * from virtual_users('%s');"
in the Postfix "cf" file.

Otherwise you get errors like;

postmap: warning: table pgsql:/etc/postfix/pgsql-email-virtual.cf: key
simow@zyworld.com: empty string result is not allowed
postmap: warning: table pgsql:/etc/postfix/pgsql-email-virtual.cf should
return NO RESULT in case of NOT FOUND

However I have seen suggestion that Postfix can call SQL queries with the
syntax "query = select functionname('%s');" so I assume there is a way to
persuade Postfix to accept that data is missing from the first type of
function. I just haven't understood it. Or perhaps the later format only
applies to things like access tables where a returned value is always present
"OK|REJECT|..." for every key. I looked at the source and bemoaned the
appalling state of my C know-how again.

For my own use I have reverted to just putting the SQL queries in the Postfix
config files because they are simple queries.

But using functions seems much more elegant way of hiding the database
structure from Postfix, and I need a function for the check_recipient_access
map at work, to implement the suggestion by mouss on validating virtual email
addresses before queuing to disk, because there we have 25000+ addresses, and
a thousand+ domains, and for my own use I currently have one domain, and just
a handful of virtual addresses.

(And this is all Postfix 2.1, and yes I know Postfix 2.2 changes a lot of
this, but so far everything else has been done with Debian Sarge standard
packages, and it will make management a lot easier if I can stick with Sarge
packages everywhere).
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:32 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0