I need to do some very complex manipulation on a set of rows from a very large table.
It seems like the manipulation ("backfill" null attributes based on a JOIN, UNION & several ORDER BYs) is best accomplished in a function. To minimize the # of rows I need a WHERE clause at the outset.
And because all of this will talk to Rails the outer layer needs to be a VIEW.
Minus the backfill what I have is
CREATE OR REPLACE VIEW testing.for_sale_layouts_view
AS SELECT * from bestforsalelayout((select address_id from public.for_sale_layouts limit 1));
;
and
CREATE OR REPLACE FUNCTION bestforsalelayout(addr_id int)
RETURNS public.for_sale_layouts
AS $$
select * from public.for_sale_layouts where address_id = addr_id;
$$
LANGUAGE SQL IMMUTABLE;
Called like
select * from testing.for_sale_layouts_view where address_id = <addr_id>;
This sort of works, however if there is more than one row returned by for_sale_layouts the function returns nothing, a singleton row works fine.
I'm at a loss, pointers in a working direction would be greatly appreciated.