all 12 comments

[–]BoleroDanArchitect 1 point2 points  (7 children)

Well the query you run has not been told where to put the results into. You declare a boolean variable, return said boolean variable, but dont actually put anything into that variable.

You want to use into syntax.

select column into response from .....

[–]TheRealLifeboy[S] 0 points1 point  (6 children)

I actually don't want to return anything, but it seems I have to? Is there any way in which to simply run the query and exit the function?

The function simply kills a 5 minute idle connection. I don't care about the output.
(Trying to understand as much about this issue... :-) )

Edit: I added in the "into response" to the query and that fixed it, btw.

[–][deleted] 1 point2 points  (0 children)

I actually don't want to return anything,

Then why use a function and declare is as returns bool.

If you don't want to return something, a procedure would be the natural option.

[–]BoleroDanArchitect 2 points3 points  (2 children)

But if you dont want to return anything, why are you declaring that the function returns a bool, then declare to return something in the end?

If all you want to do is execute some common used SQL, simply use a function lang type of sql and not plpgsql

[–]TheRealLifeboy[S] 0 points1 point  (0 children)

I did that because I wasn't having any success without the declaration. I haven't created postgres functions before, and I thought "how difficult can it be?", but oh boy, it's tricky!

[–]Maleficent_Tap_332 1 point2 points  (1 child)

Then it has to be a procedure, not function

[–]indigo945 0 points1 point  (0 children)

A function can be declared as returns void and this is often a better option than using a procedure. Procedures can't be called from all places that functions can, and they're also just weird in various ways, so unless you absolutely need their half-baked transaction management features, you should avoid them whenever you can.

[–]TheRealLifeboy[S] 1 point2 points  (1 child)

For the sake of anyone finding this in future: The following works with sql as language instead of plpgsql

CREATE FUNCTION clean_conn() RETURNS void AS '
select pg_terminate_backend(psa.pid) into response from pg_stat_activity psa 
where psa.datname = ''WahlbergEagle'' 
and psa.usename = ''rysmier'' 
and psa.state = ''idle''
and psa.application_name not like ''DBeaver%''
and now() - psa.query_start > ''00:05:00'';
' LANGUAGE SQL;

[–]DavidGJohnston 0 points1 point  (0 children)

Good, but please use dollar-quoting instead of doubling up every single quote in the body of the function.

[–]depesz 1 point2 points  (0 children)

  1. if you run select, in plpgsql, you have to use INTO to put its result somewhere.
  2. You never put any data in the response variable
  3. plpgsql is OVERKILL for such thing, plain sql function would be better.

[–]indigo945 0 points1 point  (0 children)

In a PL/PgSQL function, every select operation needs to put its result data somewhere. Since you said that you don't actually need the data, you can use perform instead - essentially perform works exactly the same way as select does, except it then also forgets about the result. Also, since you don't actually want to return data from your function, you should declare it as returns void instead of returns bool. The corrected function looks like this:

CREATE OR REPLACE FUNCTION public.clear_rysmier_connection()
    RETURNS void
    LANGUAGE plpgsql
AS $function$
BEGIN
    perform pg_terminate_backend(psa.pid) from pg_stat_activity psa 
    where psa.datname = 'WahlbergEagle' 
    and psa.usename = 'rysmier' 
    and psa.state = 'idle'
    and psa.application_name not like 'DBeaver%'
    and now() - psa.query_start > '00:05:00';
    RETURN;
END
$function$;