Hello.
I'm writing SQL functions that take an action code and determine the
rows visible by accessing application-maintained privilege tables.
Here's an example:
CREATE FUNCTION sql_areas(bigin t) RETURNS SETOF bigint AS '
SELECT _areas.area
FROM _members, _webgroups, _stores, _areas
WHERE _members.webuse r = getWebuser() AND
_members.webgro up = _webgroups.webg roup AND
_webgroups.comp any = _stores.company AND
_stores.store = _areas.store AND
_webgroups.isro ot AND
_members.deacti ve IS NULL AND
_webgroups.deac tive IS NULL
UNION
SELECT _areas.area
FROM privileges, privobjs, _areas
WHERE privileges.acti on = $1 AND
privobjs.relnam e = ''areas'' AND
privobjs.privob j = privileges.priv obj AND
((privileges.is parent = true AND
privileges.obji d = _areas.store) OR
(privileges.isp arent = false AND
privileges.obji d = _areas.area)) AND
(privileges.gra ntee = getWebuser() OR
privileges.gran tee IN (
SELECT _members.webgro up
FROM _members
WHERE _members.webuse r = getWebuser() AND
_members.deacti ve IS NULL
)
)
' LANGUAGE 'sql' STABLE;
I then want to build views atop this function like so:
CREATE VIEW areas AS
SELECT _areas.*
FROM _areas, sql_areas(5) x
WHERE _areas.area = x;
I then have queries like:
SELECT *
FROM areas
WHERE areas.name = 'Foo';
which I suppose would be recursively transformed by the planner into
something far more interesting. But the wording of the 7.4 changelog of
"Simple SQL functions can now be inlined by including their SQL in the
main query. This improves performance by eliminating per-call overhead.
That means simple SQL functions now behave like macros."
has me a bit worried. What does "simple" mean? Will the planner be able
to treat my underlying SQL-language functions as macros and in-line them
into the final query for full optimization possibilities? In fact, my
plan is to have:
SQL-language function
VIEW 1 accessing SQL function
VIEW 2 accessing VIEW 1
SQL query accessing VIEW 2
Should I abandon the SQL-language function, which eliminates some
redundant code elsewhere and incorporate the functions myself into View
1, or can I count on PostgreSQL doing it for me?
Mike Mascari
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match