Re: Simple question about running a function.

From: SQL Padawan <sql_padawan(at)protonmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: "pgsql-novice(at)lists(dot)postgresql(dot)org" <pgsql-novice(at)lists(dot)postgresql(dot)org>
Subject: Re: Simple question about running a function.
Date: 2021-11-26 18:33:01
Message-ID: CBHeX2fE8wfCaa2pRekG8NAVM0-ZOphl6XGAnFt-ZDWMwKmnVAwunxntpLigPLZWhh64aAa3P7NpUk5Ax_1ujqUNUm4iG2HWmjhOa4s-vbk=@protonmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-novice


Hi David and thank you for trying to help the terminally stupid!

> > How do I tell psql/PL/pgSQL that I want to suppress the output for
> > the test?

> By doing exactly what the error message told you.  Use PERFORM instead of SELECT...on the exact query whose output you wish to ignore, not an indirect caller.

OK - so, following your input, I searched further and I found a post by Pavel Stehule - great I thought, a major contributor - see here:
https://stackoverflow.com/a/42922661/470530

his example is this function which I duly created (and not, as you pointed out, compiled!)

CREATE OR REPLACE FUNCTION foo()
RETURNS void AS $$
BEGIN
RAISE NOTICE 'Hello from void function';
END;
$$ LANGUAGE plpgsql;

which I run as a direct call as in the example.

test=# SELECT foo();
NOTICE: Hello from void function
foo
-----
(1 row)

So, then I ran this.

-- in PLpgSQL
DO $$
BEGIN
PERFORM foo(); -- is ok
END;
$$;

with a result I found surprising - as follows.

NOTICE: Hello from void function
DO
Time: 8.704 ms

I thought that the PERFORM "swallowed" the output?

So, then I went back to my own function:

create or replace function test_fn_6()
returns VOID as $$
DECLARE
BEGIN
FOR r IN 1..10000 LOOP

SELECT ('A string');

END LOOP;
END;
$$ LANGUAGE plpgsql;

and then I tried.

DO $$
BEGIN
PERFORM test_fn_6();
END;
$$;

but get the error.

ERROR: query has no destination for result data
HINT: If you want to discard the results of a SELECT, use PERFORM instead.
CONTEXT: PL/pgSQL function test_fn_6() line 6 at SQL statement
SQL statement "SELECT test_fn_6()"
PL/pgSQL function inline_code_block line 3 at PERFORM

Now, I thought from Pavel Stehule's post that my function's output would/should be swallowed - but then the foo() example produced a text output?

I'm really puzzled here... I mean what I'm asking for is really simple - pl/pgsql 101 and I can't even get a simple function to execute multiple times in the background.

I'm not asking for the sun, moon and stars here...

rgs,

SQLP!

> David J.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message David G. Johnston 2021-11-26 18:36:40 Re: Simple question about running a function.
Previous Message David G. Johnston 2021-11-26 17:43:42 Re: Simple question about running a function.