Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jZjQE-0000sg-VB for pgsql-general@arkaria.postgresql.org; Fri, 15 May 2020 23:07:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jZjQA-0005FJ-TP for pgsql-general@arkaria.postgresql.org; Fri, 15 May 2020 23:07:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jZjQA-0005F1-Dz for pgsql-general@lists.postgresql.org; Fri, 15 May 2020 23:07:14 +0000 Received: from mail-io1-xd42.google.com ([2607:f8b0:4864:20::d42]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jZjQ6-0001hD-DF for pgsql-general@lists.postgresql.org; Fri, 15 May 2020 23:07:13 +0000 Received: by mail-io1-xd42.google.com with SMTP id o5so4440917iow.8 for ; Fri, 15 May 2020 16:07:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=ikLa7aqwUXoNb/SUKabOgV2Yw/p0Hpb7mjfB3/aEj8c=; b=F7zgbgI7KnuUlNr7nWCUFMPbSTH3xdHzQMN8jK6T/W2uWPcGvTsyNGfX7bCfQTrdUT M2EsP6QrFaoaVESrswn3XURpgDiaRVmfcJulbhgRIg+OpQJA0sjNSL9iwBO5W0b4F/Rt 5MNKh9Dy4lTY8NEngPBqdniM3UGLz4LTbBWjXDFKsyfaZzO4IkOR+UbMqSyWei091MPV oaTr94ximjP2MiVhzyD8HyIo1ssIiL4YMsuBSgKziYrelVToidSmxb/ZjrqDNry0BXKg tHPZaSFknMx8eEKlMou6CcMKbMDRdzx5NQgbFgH0PcfYE0beySPLDP6SdJV+5Dt1DMNW uqPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=ikLa7aqwUXoNb/SUKabOgV2Yw/p0Hpb7mjfB3/aEj8c=; b=XexjVkf6cKXQ3foHo9GTAzCdrklSpG/CaDRvoLMCXkL1KXiW3aciEdfGI9a00vIogF pSM0f2LqCaHjwwSsKyDpx/n/RPD/6C6dLyv6+iQqWL35AAaLsPyyR+Jwpu4r8yeLscTL oh5V17AQjqXlppDrhMSgTEQv1qC4KGsMOQDjYtFyEIL9sUW5YhWTUhz1nZ+uoodZbYPR +cStgSKy0o0sqhksI8eOevy0owYc7Drx1waG48AgmSUAg57F2u0wapXYJWf5TE3LyFM8 xgTXqQW9SU8Y7Jl+nypEGXVvyb7qXv7nsAf2VlOlFjd1f5kPhVb8rTZeD/jGMWjuceAy LfSw== X-Gm-Message-State: AOAM531nB22AWTJoFyDXSApcvhAKCP1/rxGzvJf+0uUwxwXawfrNiGT3 2iFta/aYuFW912BwZrzjore9vES/ X-Google-Smtp-Source: ABdhPJzvDekJC30BwxO88iABrrL8VLMVd/r84CXC50tIjmOSEAUODTVOfPJC/j/yQ1OE71jxLsVHWg== X-Received: by 2002:a5d:8613:: with SMTP id f19mr5121485iol.173.1589584029364; Fri, 15 May 2020 16:07:09 -0700 (PDT) Received: from ?IPv6:2601:681:5500:dde0:f120:2f2:891d:e9fe? ([2601:681:5500:dde0:f120:2f2:891d:e9fe]) by smtp.gmail.com with ESMTPSA id e13sm1430631ils.27.2020.05.15.16.07.08 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 May 2020 16:07:08 -0700 (PDT) Subject: Re: schema agnostic functions in language sql To: Adrian Klaver , pgsql-general@lists.postgresql.org References: <92aa43c7-6b96-ab7f-ddde-61346f957801@gmail.com> From: Rob Sargent Message-ID: Date: Fri, 15 May 2020 17:07:07 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/15/20 4:58 PM, Adrian Klaver wrote: > On 5/15/20 3:53 PM, Rob Sargent wrote: >> >> >> On 5/15/20 4:43 PM, Adrian Klaver wrote: >>> On 5/15/20 3:26 PM, Rob Sargent wrote: >>>> I'm using postgres 12.2, with multiple identical schema per >>>> database (each with a matching role).  I can write public plpgsql >>>> functions without using a schema identifier and let the runtime >>>> search_path find the correct schema-dependent table.  The same does >>>> not appear to be true for plain sql functions. The 'parser'? does >>>> not recognize the tables (sans schema qualifier): >>>> >>>>     ERROR:  relation "" does not exist. >>> >>> You probably need to show example code, because I don't see this: >>> >>> show search_path ; >>>                            search_path >>> ------------------------------------------------------------------ >>>  public,accounting,history,main,utility,timeclock,table_templates >>> >>> \d utility.login_info >>>                                Table "utility.login_info" >>>    Column    |              Type              | Collation | Nullable >>> |    Default >>> -------------+--------------------------------+-----------+----------+------------------ >>> >>>  user_name   | character varying              | | not null | >>>  user_pwd    | character varying              | | not null | >>>  user_role   | character varying              | |          | >>>  ts_insert   | timestamp(0) without time zone | |          | now() >>>  ts_update   | timestamp(0) without time zone | |          | >>>  user_update | character varying(20)          | |          | >>>  user_insert | character varying(20)          | |          | >>> "session_user"() >>> >>> >>> CREATE FUNCTION getli(varchar) RETURNS login_info AS $$ >>>     SELECT * FROM login_info WHERE user_name = $1; >>> $$ LANGUAGE SQL; >>> >>> select * from getli('aklaver'); >>>  user_name | user_pwd | user_role | ts_insert      | ts_update      >>> | user_update | user_insert >>> -----------+----------+-----------+---------------------+---------------------+-------------+------------- >>> >>>  aklaver   | ranger   |           | 12/29/2012 12:23:17 | 05/15/2020 >>> 15:41:14 |             | postgres >>> (1 row) >>> >> Isn't "utility" in your path above? > > Yes. In your OP you had: > > "I can write public plpgsql functions without using a schema > identifier and let the runtime search_path find the correct > schema-dependent table.  The same does not appear to be true for plain > sql functions." > > I was showing that search_path works with SQL functions, which you > indicated was not happening for you. > > Are you talking about some other case? > I'm terribly sorry:  I needed to add that plpgsql works without any knowledge of the schema, where as defining a plain sql functions does not work without schema qualification. sarge=# \dn   List of schemas   Name  |  Owner --------+----------  base   | postgres  bulk   | postgres  gt     | postgres  public | postgres  sss    | postgres (5 rows) sarge=# show search_path;    search_path -----------------  "$user", public (1 row) sarge=# sarge=# create or replace function public.segment_calls(segid uuid) returns table (name text, firsti int, lasti int, calls text) as $$ select     p.name,     s.firstmarker,     s.lastmarker,     regexp_replace(substr(g.calls,1+(2*s.firstmarker), 2*(s.lastmarker-s.firstmarker+1))::text, '(..)', ' \1','g') as calls from     segment s     join probandset b on s.probandset_id = b.id     join people l on b.people_id = l.id     join people_member m on l.id = m.people_id     join person p on m.person_id = p.id     join genotype g on g.markerset_id = s.markerset_id and g.person_id = p.id where s.id = segid; $$ language sql ; sarge-# ERROR:  relation "segment" does not exist LINE 11:     segment s              ^ sarge=# create or replace function public.segment_calls(segid uuid) returns table (name text, firsti int, lasti int, calls text) as $$ begin select     p.name,     s.firstmarker,     s.lastmarker,     regexp_replace(substr(g.calls,1+(2*s.firstmarker), 2*(s.lastmarker-s.firstmarker+1))::text, '(..)', ' \1','g') as calls from     segment s     join probandset b on s.probandset_id = b.id     join people l on b.people_id = l.id     join people_member m on l.id = m.people_id     join person p on m.person_id = p.id     join genotype g on g.markerset_id = s.markerset_id and g.person_id = p.id where s.id = segid; end; $$ language plpgsql; sarge-# CREATE FUNCTION sarge=# Query buffer reset (cleared). sarge=# \dt gt.*                   List of relations  Schema |          Name           | Type  |  Owner --------+-------------------------+-------+----------  gt     | chaseable               | table | postgres  gt     | duo_chaseable           | table | postgres  gt     | genotype                | table | postgres  gt     | ld                      | table | postgres  gt     | probandset              | table | postgres  gt     | probandset_group        | table | postgres  gt     | probandset_group_member | table | postgres  gt     | process                 | table | postgres  gt     | process_arg             | table | postgres  gt     | process_input           | table | postgres  gt     | process_output          | table | postgres  gt     | projectfile             | table | postgres  gt     | segment                 | table | postgres  gt     | segmentset              | table | postgres  gt     | threshold               | table | postgres  gt     | threshold_duo_segment   | table | postgres  gt     | threshold_segment       | table | postgres (17 rows)