pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: pgsql-general@postgresql.org <pgsql-general@postgresql.org>
Subject: specifying table in function args
Date: Thu, 28 Feb 2019 12:10:21 -0700
Message-ID: <be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com> (raw)

Using PG10.7, I have a database per investigator with one or more 
identical schemata per project.  So far I've gotten by defining public 
functions (as postgres) which reference tables /without/ schema 
reference.  Each project has a role which sets the search_path such that 
the project specific schema supplies the table definition at function 
call.  This seems to be working quite well.

I've written a function which needs a specific record:

    create or replace function public.pvr(seg segment, plus float
    default 1.0)
    returns float as $$
    declare
       retval float;
    begin
       select
    ((1.0*seg.events_equal)+seg.events_greater+plus)/(seg.events_less+seg.events_equal+seg.events_greater+plus)
    into retval;
       return retval;
    end;
    $$ language plpgsql;

but this fails in one of two ways:  either the create function call 
fails lacking a definition of "segment" or, if I create a public.segment 
table, create the function, set search_path to include a project's 
schema then drop public.segment fails because pvr() relies on it.

This is all to avoid (the existing) pv() function which requires all 
events values as arguments (i.e. much typing). Is there a way through 
this or must I generate a function per schema in this case? It's easy 
enough to do, but has a certain odor to it. I suppose I could leave the 
public.segment table is place (revoke all inserts etc) then let the 
search_path take over.

view thread (4+ messages)  latest in thread

Message-ID: <be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com>
Permalink:  ../be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com/
Also on:    postgresql.org/message-id/be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com
  Subject: Re: specifying table in function args
  In-Reply-To: <be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com>
  References: <be24ead6-fee6-8bbb-3407-23a5538588ba@gmail.com>

* 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