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 1rugj3-006zvd-AN for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2024 22:47:29 +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 1rugj2-009hKe-9l for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2024 22:47:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rugj1-009hHX-UT for pgsql-general@lists.postgresql.org; Wed, 10 Apr 2024 22:47:27 +0000 Received: from mail-lf1-x12e.google.com ([2a00:1450:4864:20::12e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rugiz-002Fko-Ez for pgsql-general@lists.postgresql.org; Wed, 10 Apr 2024 22:47:26 +0000 Received: by mail-lf1-x12e.google.com with SMTP id 2adb3069b0e04-516d04fc04bso11237475e87.2 for ; Wed, 10 Apr 2024 15:47:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712789243; x=1713394043; 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=HGrrOTeCaYsbHGQIDvOSKYJqPuGwT/bhnGR170cXsoM=; b=jOk2W8FOJVvPp6QTGPVpWkzKQN3cChgFxJQpkCJ6A6Bvt7hODJH5rsVP759H0fVIDI K0qHCQHPn0MjwnkfXtpOSoA09QKIK7RHipEjpSmDgN6MRNTwubkw2MgQw/eULAMRbBxn h8jDw5D4ISXgmDNWgKozA/KVR7FCao0eSXZ4rUufTErrIqZqpZBrxCjH0+p9+23zArKd /dHSgUFtoVftzkmxGckjaqFWfiZv5MThdOvk8Ua7asNBYFAh7hcAx8r3Pmk5RdvfGilt Y2qgl6W+L5XxRqzDtmpI1rPqkcIBxHrh3xgTpLJ1gRHqxxwaxdV4Fzx0p3ySNhGDSoJL KacA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712789243; x=1713394043; 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=HGrrOTeCaYsbHGQIDvOSKYJqPuGwT/bhnGR170cXsoM=; b=a1cxGIJs/OShqLwvtgs6d2/5ELvWdSqusUevxcwIgRJ1bwfRD5q28fsUkSwV6jjgcD y63HILpqro6juvWoGus9C0Qbn9UZsiHwug1ee1VnRKwpup2whI1lFh909DUqpGSEzEHv Db5chnLf5vvttRvyUgjfc0W6LXTbgftj6ZhBEMlqVNDPcxvkU5xTS7SNkCq0zTXaQav0 JjEPOi5BBzPcmrktytRbZ+oWf26AwiJJS4EDu0uig41fIEb4lExwksEXLW7Jet5SlSWW 1doJJxDMlJQxNJCjg1r1V0p9rq8KN6DCrB0kKhpO5JxjT+Mq4sHHStvKIObJRnKD/IFu l4eg== X-Gm-Message-State: AOJu0YwndWYOH0W/hBYKbBodMaJmFkFUr4JO8ZSwSCySZYbtMD3JPvef DiIkdKtHO1jClJeSSsYNKaR3Qpr6kLd5xtJWeU3gGM6pyqGvcfPcWM20ZiuCPYSjZhT5od0edE/ YWksND/6q73bF/qNMbYF39dfwqH+R1H9p X-Google-Smtp-Source: AGHT+IHE5BQeddNjxymEBxlOKPTeCfuQq1QEoF5F9HuFwVGId1suSlCgjYMv9GFU36EdVQ4kH1Zl1WvPNNEszqwAhvg= X-Received: by 2002:ac2:4e4f:0:b0:516:d4ce:d826 with SMTP id f15-20020ac24e4f000000b00516d4ced826mr2870918lfr.51.1712789242887; Wed, 10 Apr 2024 15:47:22 -0700 (PDT) MIME-Version: 1.0 References: <20240410232247.b48cdf2677f87d37b167e140@magnetkern.de> In-Reply-To: <20240410232247.b48cdf2677f87d37b167e140@magnetkern.de> From: Merlin Moncure Date: Wed, 10 Apr 2024 17:47:09 -0500 Message-ID: Subject: Re: (When) can a single SQL statement return multiple result sets? To: Jan Behrens Cc: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000065c8b50615c5d2ae" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000065c8b50615c5d2ae Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Apr 10, 2024 at 4:22=E2=80=AFPM Jan Behrens wrote: > Hello, > > While writing a PostgreSQL client library for Lua supporting > Pipelining (using PQsendQueryParams), I have been wondering if there > are any single SQL commands that return multiple result sets. It is > indeed possible to create such a case by using the RULE system: > > db=3D> CREATE VIEW magic AS SELECT; > CREATE VIEW > db=3D> CREATE RULE r1 AS ON DELETE TO magic > db-> DO INSTEAD SELECT 42 AS "answer"; > CREATE RULE > db=3D> CREATE RULE r2 AS ON DELETE TO magic > db-> DO ALSO SELECT 'Hello' AS "col1", 'World!' AS "col2"; > CREATE RULE > db=3D> DELETE FROM magic; -- single SQL statement! > answer > -------- > 42 > (1 row) > > col1 | col2 > -------+-------- > Hello | World! > (1 row) > > DELETE 0 > > Here, "DELETE FROM magic" returns multiple result sets, even though it > is only a single SQL statement. > I guess you should have named your table, "sorcery", because that's what this is. In the corporate world, we might regard the 'CREATE RULE' feature as a 'solution opportunity' :-). You might be able to overlook this on your end IMO as the view triggers feature has standardized and fixed the feature. > why can't I write a stored procedure or function that returns multiple result sets? Functions arguably should not be able to do this, doesn't the standard allow for procedures (top level statements invoked with CALL) to return multiple results? merlin --00000000000065c8b50615c5d2ae Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Wed, Apr 10, 2024 at 4:22=E2=80=AFPM J= an Behrens <jbe-mlist@magnetk= ern.de> wrote:
Hello,

While writing a PostgreSQL client library for Lua supporting
Pipelining (using PQsendQueryParams), I have been wondering if there
are any single SQL commands that return multiple result sets. It is
indeed possible to create such a case by using the RULE system:

db=3D> CREATE VIEW magic AS SELECT;
CREATE VIEW
db=3D> CREATE RULE r1 AS ON DELETE TO magic
db-> DO INSTEAD SELECT 42 AS "answer";
CREATE RULE
db=3D> CREATE RULE r2 AS ON DELETE TO magic
db-> DO ALSO SELECT 'Hello' AS "col1", 'World!'= ; AS "col2";
CREATE RULE
db=3D> DELETE FROM magic; -- single SQL statement!
=C2=A0answer
--------
=C2=A0 =C2=A0 =C2=A042
(1 row)

=C2=A0col1=C2=A0 |=C2=A0 col2=C2=A0
-------+--------
=C2=A0Hello | World!
(1 row)

DELETE 0

Here, "DELETE FROM magic" returns multiple result sets, even thou= gh it
is only a single SQL statement.


=C2=A0I guess you should have named your table, "sorcery"= , because that's what=C2=A0this is.=C2=A0 In the corporate=C2=A0world, = we might regard the 'CREATE RULE' feature as a 'solution opport= unity'=C2=A0=C2=A0:-).=C2=A0 You might be able to overlook this on your= end IMO as the view triggers feature=C2=A0has standardized and fixed the f= eature.

> why can't I write a stored proced= ure or function that returns multiple result sets?

Functions arguably should not be able to do this, doesn&#= 39;t the=C2=A0standard allow for procedures (top level statements invoked w= ith CALL) to return multiple=C2=A0results?
=
merlin



--00000000000065c8b50615c5d2ae--