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 1rufPB-006qeO-PV for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2024 21:22:54 +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 1rufPA-008iWS-Us for pgsql-general@arkaria.postgresql.org; Wed, 10 Apr 2024 21:22:52 +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 1rufPA-008iVo-K1 for pgsql-general@lists.postgresql.org; Wed, 10 Apr 2024 21:22:52 +0000 Received: from gaoxing.magnetkern.de ([167.235.225.147]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rufP8-000GMI-9N for pgsql-general@lists.postgresql.org; Wed, 10 Apr 2024 21:22:52 +0000 Received: from titanium.fritz.box (p200300c26f1e6f00264bfefffe54b09c.dip0.t-ipconnect.de [IPv6:2003:c2:6f1e:6f00:264b:feff:fe54:b09c]) by gaoxing.magnetkern.de (Postfix) with ESMTPSA id B003E53C8B for ; Wed, 10 Apr 2024 23:22:08 +0200 (CEST) Date: Wed, 10 Apr 2024 23:22:47 +0200 From: Jan Behrens To: pgsql-general@lists.postgresql.org Subject: (When) can a single SQL statement return multiple result sets? Message-Id: <20240410232247.b48cdf2677f87d37b167e140@magnetkern.de> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; amd64-portbld-freebsd14.0) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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=> CREATE VIEW magic AS SELECT; CREATE VIEW db=> CREATE RULE r1 AS ON DELETE TO magic db-> DO INSTEAD SELECT 42 AS "answer"; CREATE RULE db=> CREATE RULE r2 AS ON DELETE TO magic db-> DO ALSO SELECT 'Hello' AS "col1", 'World!' AS "col2"; CREATE RULE db=> 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. (Note that this isn't possible with rules ON SELECT because it is only allowed to create a single SELECT rule on a view.) The case outlined above seems to be a somewhat special case. I haven't found any other way to return multiple results (other than sending several semicolon-separated statements, which is not supported by PQsendQueryParams). So is there any (other) case where I reasonably should expect several result sets returned by PQgetResult (before PQgetResult returns NULL)? Wouldn't it make sense to disallow such behavior altogether? And if not, why can't I write a stored procedure or function that returns multiple result sets? These questions are relevant to me because it may have an effect on the API design if a statement can return several result sets. Kind regards, Jan Behrens