Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sJcIw-005tCb-Dz for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2024 17:07:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sJcIt-00GmpH-PN for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2024 17:07:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sJcIt-00Gmp9-C9 for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2024 17:07:32 +0000 Received: from mail-oo1-xc31.google.com ([2607:f8b0:4864:20::c31]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sJcIs-002JmZ-5V for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2024 17:07:31 +0000 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-5bfb24e338bso12000eaf.0 for ; Tue, 18 Jun 2024 10:07:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718730448; x=1719335248; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=GPvDKppRr93RO+rmKit32wOC435yY483al+yjPwtJ+8=; b=DdrBliNGH9i3i1EMGCXyog+PD2YwZ6sAkkkEV4/LDXh7UpJdTV8IuAfaFIMY0N3jid uqI5xvtxxQ6EShyKcDEAylaaznABlI7diXVWcR+vmL8PMXS7ScC9wfyHKs5kSLJZfzE/ xq1WPQcw/wRcWo5UkdyAPxSVNEZBmX5qPBiVpcEwVHyY390voAWhPIPdoPC0b5zTy0jB wpiSoxiKZGH/c+b5jq7bil+nxw+U89gaedtTbfCy76Zi4Er/7MpGQzAiMKx8EVhWAH3Q VeX+dSa67nTm7JSqby/+Hn9i6pMd3NamNtrxgA951nE7Zhvo4n2CMblGYOmY1fgb+lNo UEiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718730448; x=1719335248; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GPvDKppRr93RO+rmKit32wOC435yY483al+yjPwtJ+8=; b=BkbRLcqq8rCWTUP1RIu48F7OOisVt+/2kvLKi7ND8tIcCmBt1o4pNNd87GGTh74AhY tPtBnqTf3e3B+JmNlQwXKXfEhY/EJMGJKzfS6rVGtshVEXUQFiLt1XuftoHrnEIus5UI /UYn37Ofg0z+DPwiCh9ESs8dfN40GfEArX+In+ONYwMOn9eUgGdj0e03UgqStLrrCkPP 3p0hvvXaXmLHmo3ZfsZfF1pheQcyI9XFb3ki1nTDrqnBpY7yoWFXLZa16CY8a8ZzQQLK 4gErEWmb8plOarMESJHcTBv2p53nS79+BNLbjPqiWYLFffgnBbrOI9wzxQnqrOt36lyr LFUA== X-Gm-Message-State: AOJu0Yz9lPa9pvBA18a4S8UrOCmS8FZL0R6Tw4HVDrpGqCYgqAgz7RLT s1cXsWGPeAdtNzUNBPjuesnchCMSKC3PTMT7LuZjt1FNETVoYkYHHhDiqyt2JIM0dHDxRsCL5HS snydEfYIUSLMfFZel+pqn48yDCm+RcQ== X-Google-Smtp-Source: AGHT+IGW7A8YFZAunDdKmwSvxylZjuXOyaieJHLoiRWA9WZLe+1JLcxt1BuM/As5qSgGbTYT4sltjk1mI4ynfPHb26I= X-Received: by 2002:a05:6870:e392:b0:259:8cc8:7301 with SMTP id 586e51a60fabf-2598cc89abamr493252fac.4.1718730447653; Tue, 18 Jun 2024 10:07:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ron Johnson Date: Tue, 18 Jun 2024 13:07:16 -0400 Message-ID: Subject: Re: Seeking Clarification on Function Definitions in PostgreSQL Extensions To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000cc1173061b2d1dfc" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000cc1173061b2d1dfc Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable fun1 returns a table set just like any other SELECT statement. fun2 puzzles me. Why would you return parameters AND *a single record* (un= less it's an error status). fun3 just returns two parameters. Why isn't it a procedure? fun2, returning parameters AND a function value, would have made my Comp Sci professors very, very angry. Definitely Bad Practice. You choose which to use based on how much data you want to return. On Tue, Jun 18, 2024 at 12:50=E2=80=AFPM Ayush Vatsa wrote: > Hi PostgreSQL community, > Recently I am exploring extensions in postgres and got a little confused > regarding the function definition present in SQL file. For example consid= er > below three functions: > > CREATE FUNCTION fun1(integer) > RETURNS TABLE( > col1 integer, > col2 text > ) > AS 'MODULE_PATHNAME', 'fun1' > LANGUAGE C; > > CREATE FUNCTION fun2( > IN input integer, > OUT col1 integer, > OUT col2 text > ) > RETURNS SETOF record > AS 'MODULE_PATHNAME', 'fun2' > LANGUAGE C; > > CREATE FUNCTION fun3( > IN input integer, > OUT col1 integer, > OUT col2 text > ) > AS 'MODULE_PATHNAME', 'fun3' > LANGUAGE C; > > 1/ I wanted to know what's the difference between the above three > definitions. > As per my understanding, "fun1" and "fun2" look the same, taking one > integer and returning two columns with multiple rows. > Whereas "fun3" is used when we are returning only one row, but my doubt > for "fun3" is that, Can the above definition (used for fun1 and fun2) cov= er > both single and multiple row scenarios. > > 2/ How does someone decide which type of definition is to be used? > > Regards > Ayush Vatsa > --000000000000cc1173061b2d1dfc Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
fun1 returns a table set just like any other SELECT s= tatement.
fun2 puzzles=C2=A0me.=C2=A0 Why would you return parame= ters AND a single record=C2=A0(unless it's an error status).
fun3 just returns two parameters.=C2=A0 Why isn't it a procedure= ?

fun2, returning parameters AND a function value,= would have made my Comp Sci professors very, very angry.=C2=A0 Definitely = Bad Practice.

You choose which to use based on how= much=C2=A0data you want to return.

On Tue, Jun 18, 2024 at 12:50=E2=80= =AFPM Ayush Vatsa <ayushvats= a1810@gmail.com> wrote:
Hi PostgreSQL community,
Recently I am e= xploring extensions in postgres and got a little confused regarding the fun= ction definition=C2=A0present in SQL file. For example consider below three= functions:

CREATE FUNCTION fun1(integer)
RETURNS TABLE(
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 col1 integer,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 co= l2 text
)
AS 'MODULE_PATHNAME', 'fun1'
LANGUAGE C;=

CREATE FUNCTION fun2(
=C2=A0 =C2=A0 IN =C2=A0input integer,
= =C2=A0 =C2=A0 OUT col1 integer,
=C2=A0 =C2=A0 OUT col2 text
)
RETU= RNS SETOF record
AS 'MODULE_PATHNAME', 'fun2'
LANGUAG= E C;

CREATE FUNCTION fun3(
=C2=A0 =C2=A0 IN =C2=A0input integer,<= br>=C2=A0 =C2=A0 OUT col1 integer,
=C2=A0 =C2=A0 OUT col2 text
)
A= S 'MODULE_PATHNAME', 'fun3'
LANGUAGE C;

1/ I want= ed to know what's the difference between the above three definitions. <= br>As per my understanding, "fun1" and "fun2" look the = same, taking one integer and returning two columns with multiple rows.
= Whereas=C2=A0"fun3" is used when we are returning only one row, b= ut my doubt for "fun3" is that, Can the above definition=C2=A0(us= ed for fun1 and fun2) cover both single and multiple row scenarios.

= 2/ How does someone decide which type of definition is to be used?

R= egards
Ayush Vatsa
--000000000000cc1173061b2d1dfc--