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 1sJcP2-005tdp-Fz for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2024 17:13:52 +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 1sJcP0-00GtER-9b for pgsql-general@arkaria.postgresql.org; Tue, 18 Jun 2024 17:13:51 +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 1sJcOz-00GtEI-Ux for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2024 17:13:50 +0000 Received: from mail-oa1-x2f.google.com ([2001:4860:4864:20::2f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sJcOy-002Jp9-Kh for pgsql-general@lists.postgresql.org; Tue, 18 Jun 2024 17:13:50 +0000 Received: by mail-oa1-x2f.google.com with SMTP id 586e51a60fabf-24c9f892aeaso2967666fac.2 for ; Tue, 18 Jun 2024 10:13:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718730826; x=1719335626; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bibSjg6A+YDR21W2v7lyWa5SwgFAyVhnCXLsK5U6Zc0=; b=cfCV+g+dka7coy9qpf8m/VhHJVfXK0TCuYfOGUlVdYnTS4Z0zBVvD0/fp1JmhoieAv xgVIffpIV2R+HFajykOyRDSqqalqDqBPpVE2kWbjCjuHH5s9/lTa7EppS5Wq4Qg/ihrm qZET7PlOFyJqNhgljLi/iUj2gbJcwjHO0tBAolcZD3Zl8M/mppO7oXLRm0GqDN5rUqZP CPlvu/c9p8rtZetaGDxhVaqkXe0r0yWrGig0QZ3ywi6CyFj3xBEv6Kba8GzFaxQS0Ea8 8ykob6dUCw11hPAlS3Ka0sxoRQK9pUC6BoZiMtBndv/pVqJ5TE2HhHxCCgtw1lY5reLH T4CQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718730826; x=1719335626; h=cc: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=bibSjg6A+YDR21W2v7lyWa5SwgFAyVhnCXLsK5U6Zc0=; b=OoD5yoTmmaF0+V3BTP1NhoeMkrVcIap05komFoR0WeXJ7Lo3C5paZXFV/1cNZSWSrD 9p2lcSSnGWqIxzTMOh7a/yroeK78TZZju2wCJ+2nw0TuV1JqTqWLRp63D2sRlu4UzkjZ b97LKB/WHVyr4wA/CiKOOFnu/fMgoaXdL9i58eI9iFra5W3yFxEjtyDOOUxc45ezxQSI Wg8cBNERV41hte/5N2IiXOpr0q9ZHKDftXOlUPPyTG/OCMJEnDuBT2qA/nZVA+iBxQI1 oaSHAxFPhoMVqWkxXA7+Xpu4RxIH9B7e9t6dMW6eKeN9u2MSdbNhhQaiSweCHnXCZvGh av+Q== X-Gm-Message-State: AOJu0Yybdv7o7EsDcO5k6aC6tqAQC17yOBa7cG4aBLOpZGA0pph6fVQd YZFzBMQ/BhoDQXBG7W9nzdtZ7z0kcj3YTdUGHBczGAIYDq6IbcE75iZlRlQ860nhu9LZXx1sx/E fYmlU8otsaT3tOQHe32G3IX+6pYw= X-Google-Smtp-Source: AGHT+IFzlL7lLYNlyqKXQq6qHH5v+9dzINrBZKqDmBOp6S+9uKn9AbFGr9wtyxVKdMjiGWGD58imOqIzBFAEDfUl+K4= X-Received: by 2002:a05:6870:218c:b0:254:a8ec:306e with SMTP id 586e51a60fabf-25c949cd745mr509084fac.7.1718730826207; Tue, 18 Jun 2024 10:13:46 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: "David G. Johnston" Date: Tue, 18 Jun 2024 10:13:08 -0700 Message-ID: Subject: Re: Seeking Clarification on Function Definitions in PostgreSQL Extensions To: Ayush Vatsa Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000005c5702061b2d34ad" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000005c5702061b2d34ad Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Jun 18, 2024 at 9:50=E2=80=AFAM Ayush Vatsa wrote: > 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. > Yes. > > Can the above definition (used for fun1 and fun2) cover both single and > multiple row scenarios. > In so far as one is a valid number of rows to return from a function that returns zero or more rows, yes. But if the function is incapable of returning more than one result it should not be marked with table/setof on semantic grounds. > 2/ How does someone decide which type of definition is to be used? > > Between 1 and 2 is a style choice. I prefer TABLE. Using setof is more useful when the returned type is predefined. Or a true record where the caller has to specify the shape. For 3, having a non-set-returning-function that outputs multiple columns is just odd, IMO. Personally I'd go for pre-defining a composite type, then return that type. David J. --0000000000005c5702061b2d34ad Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Jun 18, 2024 at 9:50=E2=80=AFAM Ayush Vatsa <ayushvatsa1810@gmail.com> = wrote:
1/ I wanted to know = what's the difference between the above three definitions.
As per m= y understanding, "fun1" and "fun2" look the same, takin= g one integer and returning two columns with multiple rows.

Yes.
=C2=A0

Can the above defin= ition=C2=A0(used for fun1 and fun2) cover both single and multiple row scen= arios.

In so far as one = is a valid number of rows to return from a function that returns zero or mo= re rows, yes.=C2=A0 But if the function is incapable of returning more than= one result it should not be marked with table/setof on semantic grounds.

=

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


Between 1 and 2 is= a style choice.=C2=A0 I prefer TABLE.=C2=A0 Using setof is more useful whe= n the returned type=C2=A0is predefined.=C2=A0 Or a true record where the ca= ller has to specify the shape.

For 3, having a non-set= -returning-function that outputs multiple columns is just odd, IMO.=C2=A0 P= ersonally I'd go for pre-defining a composite type, then return that ty= pe.

David J.

--0000000000005c5702061b2d34ad--