Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eli5X-0007mm-56 for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 21:26:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eli5W-0004kW-5v for pgsql-general@arkaria.postgresql.org; Tue, 13 Feb 2018 21:26:06 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eli5V-0004kM-J4 for pgsql-general@lists.postgresql.org; Tue, 13 Feb 2018 21:26:05 +0000 Received: from mail-it0-x22e.google.com ([2607:f8b0:4001:c0b::22e]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eli5S-00062Q-Rn for pgsql-general@postgresql.org; Tue, 13 Feb 2018 21:26:04 +0000 Received: by mail-it0-x22e.google.com with SMTP id e1so12545911ita.0 for ; Tue, 13 Feb 2018 13:26:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=cYJw+kvJZkPsAXxh9X9RR2tVLPB4vUpGgf8x+TwbmW4=; b=Z4cQNUhZy6hklTbRElGMZmC4fARY/H1DZs6AJUaCeo89ASZ+pw1ll3KIiR3WjUkZHf g4YLxifdBtgpFpblezTtmhbD3fNP+f20Co15/RYJ7rjCCtDtPwT+duhFWSV5knd+AC1q laPhDLPe9hM3B1udcs02u0ZxI4QEGtjm8Ry/rCbMf6hGPZCJxYjgHdwUYQKcDs8izp/r M34EwUOh7PdUsaHNGIuMzTyGH2JMxhbFb/7tzJjmJFaZwMv/MtVSjtZ8r7VafQ1mXxAS +riEMCsaGNONgcD6fnopW8Md0ks4N/VbjZ9piurNxDyOpnismYURXuGhesWCiGu8xJ2N PFpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=cYJw+kvJZkPsAXxh9X9RR2tVLPB4vUpGgf8x+TwbmW4=; b=umLLG0/pTFiOdmFdP+CMf/XHjPtocpqXSDybe565YCgXwvCufbQRObUnuzi8PdYJ2e 5MIPCp1DG94dXkIwlmJrUNKxKWqbxMO6Jf0UzgbscjEnqii5LMfH2fnRnyM11XrIggc6 L6K7a2OWef/KQ0FrGWtIV7WyW6ay+gaFvTWQhVPE6MF+ZgOuJPMlm+HDk5uMaX0RUddn 6shR8tZ0Vn/qcN2HRaX0SQuBAPwg31VqMeuraM3yAWp8S39Iy5pls3HundTOqZZdMfrE p9hDNCIh7Jn2/vSiXtlcnd4UFODnES6G7EwD3dqaQRYGwHrKvgQMs0kLZ8SXoP1clPFa zXgA== X-Gm-Message-State: APf1xPCsCCWxebteK/hDARRKXJeOiKka3ft6mh7CDLex8krbAYGftRzX omFKaR1B/r9twA1AxIa2q1xAnQLa X-Google-Smtp-Source: AH8x226hbdXAKn2tB7qXsmu+zsz5xRnN9t53yn9B5vlHiPlok6iSbasMRfOdBnSMagAMoPemBZav1g== X-Received: by 10.36.46.22 with SMTP id i22mr3456080ita.23.1518557161609; Tue, 13 Feb 2018 13:26:01 -0800 (PST) Received: from ?IPv6:2605:a601:2062:d00:e97b:9c5a:f9a8:1f5? ([2605:a601:2062:d00:e97b:9c5a:f9a8:1f5]) by smtp.gmail.com with ESMTPSA id b8sm733353itc.3.2018.02.13.13.26.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Feb 2018 13:26:00 -0800 (PST) From: armand pirvu Message-Id: <0B711AA9-3ACA-4A26-BC40-9F971E250AE0@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_3DF16DEC-1B41-46D4-B935-9DC9EA929565" Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: cursors and function question Date: Tue, 13 Feb 2018 15:25:26 -0600 In-Reply-To: Cc: "pgsql-general@postgresql.org" To: Adrian Klaver References: <149EBC11-E33E-4DBA-9130-8C93BE4B1B43@gmail.com> <83c954ba-4d14-ac71-f4ea-30ce25b9a310@aklaver.com> X-Mailer: Apple Mail (2.3273) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_3DF16DEC-1B41-46D4-B935-9DC9EA929565 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Feb 13, 2018, at 1:22 PM, Adrian Klaver = wrote: >=20 > On 02/13/2018 11:17 AM, armand pirvu wrote: >>> On Feb 13, 2018, at 12:54 PM, Adrian Klaver = >> wrote: >>>=20 >>> On 02/13/2018 10:22 AM, armand pirvu wrote: >>>> Hi >>>> Is there any elegant way not a two steps way I can output the = cursor value at each step? >>>> testtbl table has this content >>>> col1 | col2 | col3 >>>> ------------+------------+------ >>>> E1 | CAT1 | 0 >>>> E1 | CAT2 | 0 >>>> E1 | CAT3 | 0 >>>> E4 | CAT1 | 0 >>>> E5 | CAT1 | 0 >>>> E6 | CAT1 | 0 >>>> E7 | CAT1 | 0 >>>> This works >>>> BEGIN WORK; >>>> DECLARE fooc CURSOR FOR SELECT * FROM testtbl; >>>> FETCH ALL FROM fooc; >>>> CLOSE fooc; >>>> COMMIT WORK; >>>> col1 | col2 | col3 >>>> ------------+------------+------ >>>> E1 | CAT1 | 0 >>>> E1 | CAT2 | 0 >>>> E1 | CAT3 | 0 >>>> E4 | CAT1 | 0 >>>> E5 | CAT1 | 0 >>>> E6 | CAT1 | 0 >>>> E7 | CAT1 | 0 >>>> But >>>> CREATE OR REPLACE FUNCTION foofunc() >>>> RETURNS text AS $$ >>>> DECLARE >>>> var2 RECORD; >>>> cur CURSOR FOR SELECT * from testtbl; >>>> BEGIN >>>> OPEN cur; >>>> LOOP >>>> FETCH cur INTO var2; >>>> return var2; >>>> END LOOP; >>>> CLOSE cur; >>>> END; $$ >>>> LANGUAGE plpgsql; >>>=20 >>>=20 >>> CREATE OR REPLACE FUNCTION public.foofunc() >>> RETURNS SETOF testtbl >>> LANGUAGE sql >>> AS $function$ >>> SELECT * FROM testtbl; >>> $function$ >>>=20 >>>=20 >>> test=3D> select * from foofunc(); >>> col1 | col2 | col3 >>> ------+------+------ >>> E1 | CAT1 | 0 >>> E1 | CAT2 | 0 >>> E1 | CAT3 | 0 >>> E4 | CAT1 | 0 >>> E5 | CAT1 | 0 >>> E6 | CAT1 | 0 >>> E7 | CAT1 | 0 >>> (7 rows) >>>=20 >>>=20 >>>> select foofunc(); >>>> foofunc >>>> ------------------------------- >>>> ("E1 ","CAT1 ",0) >>>> But I am looking to get >>>> foofunc >>>> ------------------------------- >>>> ("E1 ","CAT1 ",0) >>>> ("E1 ","CATs ",0) >>>> etc >>>> Many thanks >>>> =E2=80=94 Armand >>>=20 >>>=20 >>> -- >>> Adrian Klaver >>> adrian.klaver@aklaver.com = > >> Thanks Adrian >> That one I figured it out as well. The idea is that said table has = some records which I need to loop and do some processing using cursors = similar with >> DECLARE >> cur CURSOR FOR SELECT * >> FROM testtbl FOR UPDATE; >> BEGIN >> FOR row IN cur LOOP >> UPDATE testtbl >> SET col3=3D1 >> WHERE CURRENT OF cur; >> END LOOP; >> return cur; >> END >> For a row update the goal is to return the cursor value be it = before/after the update, hence my question and test >=20 > Not following, are you looking to do this in an UPDATE trigger or = somewhere else? >=20 > Another way to ask is why do you want to use a cursor? >=20 >> I found some code which seems to do what I need but it involves two = functions >> CREATE or replace FUNCTION reffunc(refcursor) RETURNS refcursor AS $$ >> BEGIN >> OPEN $1 FOR SELECT col FROM test; >> RETURN $1; >> END; >> $$ LANGUAGE plpgsql; >> BEGIN; >> SELECT reffunc('funccursor'); >> FETCH ALL IN funccursor; >> COMMIT; >> And this is what beats me , aka can I put all in one / how ? >> Thanks >> Armand >=20 >=20 > --=20 > Adrian Klaver > adrian.klaver@aklaver.com Not a trigger , but the idea is we will do some batch processing from = said table let=E2=80=99s name it testtbl 1 - we get the records using select for update with a limit 100 for = example 2 - update each record using using cursor 3 - print the cursor content so that way I have an idea what was updated I was thinking that if I can put a unique constraint on the table, I can = generate a global table in the function , update main table from global = table and return select from global table I can see the developer desire to use cursors to minimize some effort on = his side Thanks=20 Armand --Apple-Mail=_3DF16DEC-1B41-46D4-B935-9DC9EA929565 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

On Feb 13, 2018, at 1:22 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:

On 02/13/2018 11:17 = AM, armand pirvu wrote:
On Feb 13, 2018, at 12:54 PM, Adrian Klaver <adrian.klaver@aklaver.com<mailto:adrian.klaver@aklaver.com>> wrote:

On 02/13/2018 10:22 AM, armand pirvu wrote:
Hi
Is = there any elegant way not a two steps way I can output the cursor value = at each step?
testtbl table has this content
    col1    | =    col2    | col3
------------+------------+------
 E1 =         | CAT1 =       |    0
 E1         | = CAT2       |    0
 E1         | = CAT3       |    0
 E4         | = CAT1       |    0
 E5         | = CAT1       |    0
 E6         | = CAT1       |    0
 E7         | = CAT1       |    0
This works
BEGIN WORK;
DECLARE = fooc  CURSOR FOR SELECT * FROM testtbl;
FETCH ALL = FROM fooc;
CLOSE fooc;
COMMIT WORK;
    col1    | =    col2    | col3
------------+------------+------
 E1 =         | CAT1 =       |    0
 E1         | = CAT2       |    0
 E1         | = CAT3       |    0
 E4         | = CAT1       |    0
 E5         | = CAT1       |    0
 E6         | = CAT1       |    0
 E7         | = CAT1       |    0
But
CREATE OR REPLACE FUNCTION foofunc()
   RETURNS text AS $$
DECLARE
 var2   RECORD;
 cur CURSOR = FOR SELECT * from testtbl;
BEGIN
   OPEN cur;
    LOOP
      FETCH cur INTO var2;
      return var2;
   END LOOP;
   CLOSE cur;
END; $$
LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION public.foofunc()
RETURNS SETOF testtbl
LANGUAGE sql
AS $function$
   SELECT * FROM = testtbl;
$function$


test=3D> select * from foofunc();
col1 | = col2 | col3
------+------+------
E1 =   | CAT1 |    0
E1   | = CAT2 |    0
E1   | CAT3 | =    0
E4   | CAT1 | =    0
E5   | CAT1 | =    0
E6   | CAT1 | =    0
E7   | CAT1 | =    0
(7 rows)


select = foofunc();
          &nb= sp; foofunc
-------------------------------
 ("E1        ","CAT1 =      ",0)
But I am looking to = get
          &nb= sp; foofunc
-------------------------------
 ("E1        ","CAT1 =      ",0)
 ("E1 =        ","CATs =      ",0)
etc
Many = thanks
=E2=80=94 Armand


--
Adrian Klaver
adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
Thanks Adrian
That one I figured = it out as well. The idea is that said table has some records which I = need to loop and do some processing using cursors similar with
DECLARE
cur CURSOR FOR SELECT *
    FROM testtbl FOR UPDATE;
BEGIN
  FOR row IN cur LOOP
    UPDATE testtbl
    SET col3=3D1
    WHERE CURRENT OF cur;
  END = LOOP;
  return cur;
END
For a row update the goal is to return the = cursor value  be it before/after the update, hence my question and = test

Not following, are = you looking to do this in an UPDATE trigger or somewhere else?

Another way to ask is why do you want to = use a cursor?

I found some code which = seems to do what I need but it involves two functions
CREATE= or replace FUNCTION reffunc(refcursor) RETURNS refcursor AS $$
BEGIN
    OPEN $1 FOR SELECT col FROM = test;
    RETURN $1;
END;
$$ LANGUAGE plpgsql;
BEGIN;SELECT reffunc('funccursor');
FETCH ALL IN = funccursor;
COMMIT;
And this is what beats =  me , aka can I put all in one / how ?
Thanks
Armand


-- 
Adrian Klaver
adrian.klaver@aklaver.com


Not a trigger , but the = idea is we will do some batch processing from said table let=E2=80=99s = name it testtbl

1 - we get the = records using  select for update with a limit 100 for = example
2 - update each record using using cursor
3 = - print the cursor content so that way I have an idea what was = updated

I was thinking that if I can = put a unique constraint on the table, I can generate a global table in = the function , update main table from global table and return select = from global table

I can see the = developer desire to use cursors to minimize some effort on his = side

Thanks 

Armand


= --Apple-Mail=_3DF16DEC-1B41-46D4-B935-9DC9EA929565--