Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjKgU-0005Yf-KY for pgsql-general@arkaria.postgresql.org; Fri, 05 Jul 2019 09:39:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hjKgT-0003VX-Gl for pgsql-general@arkaria.postgresql.org; Fri, 05 Jul 2019 09:39:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjKgS-0003VQ-Vw for pgsql-general@lists.postgresql.org; Fri, 05 Jul 2019 09:39:13 +0000 Received: from mail-lf1-x142.google.com ([2a00:1450:4864:20::142]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hjKgP-0003Dx-D3 for pgsql-general@lists.postgresql.org; Fri, 05 Jul 2019 09:39:11 +0000 Received: by mail-lf1-x142.google.com with SMTP id b17so2307656lff.7 for ; Fri, 05 Jul 2019 02:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cifrasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:organization:message-id:to:cc:subject:in-reply-to :references:mime-version:content-transfer-encoding; bh=jppeKgKmAIlIBT+024q3ZpNt32Z69PPk7HELOoN6sXo=; b=r6ZqIfgFcYxs6qCF7jRDDO2CJqdMffMrj5qns9Jy+r50i6HSDTqem0ho93pBN6BqkM RsTKVEYnFYOfjFkUlhjgGZcNHzyXxiGXoZao3Ks+i93q5nt7uGvj+BD5Q8aDIYlLj8EX D3RiTJaFdpDueekRyo55kxDPQ4NAzYBJ3Rku0D7MbPpCU+xq+u3LRTLj0LcQg0LLwnkn PzFxFnGBoJmrGYvhPlxqbGxzkC2FpkH4tvifCeSRMUTF/3zIgLUNldJfzw1x3+DXtC0f Y/wvkLxV1r1TlURfXB8OzZC3PfN6GTyd7GQfr1kz/z58/9CeW+bzjxOT+IgJJVR4DPPV U+IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:organization:message-id:to:cc:subject :in-reply-to:references:mime-version:content-transfer-encoding; bh=jppeKgKmAIlIBT+024q3ZpNt32Z69PPk7HELOoN6sXo=; b=IdJQCLIXxk3fPbbaMXiLlbsr0Kf6GDKWV7KBzx7wyoPgmKVUfATJotuEfSom4Q+XHB KyN9BAo7sVEXWlsVVxpGIC9shJgnvsi2Za9mzdWENpishhek7WVgGdn4FbTh2XVOLDjI gMcfDcu47amWqjZTfscLeLEF8nay9K91VSNpG5+LnbP2ZbsRiPceTGcBHMbum8OSLaUh +CeaUNjM9axhbrNkROmFD6JApU5S9j1uJWqWPP6YRpHHfY5SsO2BT6QyBmp45ZqDDoHJ oWIH4/iqf6bEpMFXH30ioO4sTXRgE7J+DJmTKHaMu+SbwZsIpkncvizfTIHRc6QmmhOG InOA== X-Gm-Message-State: APjAAAWyPmCFCEx0vsNWhZCiWnDczyufXgRSuYqZ73Kn3+WGWjpemJn1 4dqTHB/WBgcYytLxEn1GwhV7Zw== X-Google-Smtp-Source: APXvYqyeVy2Z208qwhOmKFcEMWiOdCTtIzsLOIDt/CDpYfzqhYBS8FoyOjdMEu+Fy0U25TZeCDQQKg== X-Received: by 2002:ac2:44c5:: with SMTP id d5mr1554201lfm.134.1562319546571; Fri, 05 Jul 2019 02:39:06 -0700 (PDT) Received: from [192.168.67.102] ([84.201.247.191]) by smtp.gmail.com with ESMTPSA id n10sm1310173lfe.24.2019.07.05.02.39.04 (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 05 Jul 2019 02:39:04 -0700 (PDT) Date: Fri, 5 Jul 2019 13:39:03 +0400 From: Andrey Sychev Organization: =?windows-1251?B?zs7OICLW6PTw4PHu9PIi?= X-Priority: 3 (Normal) Message-ID: <123684459.20190705133903@cifrasoft.com> To: Tom Lane CC: pgsql-general@lists.postgresql.org Subject: Re: Error: rows returned by function are not all of the same row type In-Reply-To: <28764.1562251122@sss.pgh.pa.us> References: <851561973.20190704162046@cifrasoft.com> <28764.1562251122@sss.pgh.pa.us> 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: Precedence: bulk Thank you very much for answering my question, Tom. Yes, I have always assumed that returning from function without calling SPI_freetuptable is not good idea, but I do not know another way to achieve same result. As I am not expert in PostgreSQL internals and mostly work according to official documentation, I never know about tuplestore before. This look like pretty interesting idea. I have searched for tuplestore over Internet and found some topics, relative to subject: 1. https://postgres.cz/wiki/Iter%C3%A1tor_pole 2. https://www.postgresql.org/message-id/1073862553.1475.93.camel%40jeff 3. https://www.postgresql.org/message-id/753432.21663.qm%40web65511.mail.ac4.yahoo.com (with your comment) 4. http://web.mit.edu/ghudson/trac/attic/src/postgresql-7.4.5/contrib/tablefunc/tablefunc.c I have examined this topics briefly and got some questions: 1. As I understand there are tests for SFRM_Materialize in code above. In my case a caller of my function is PL/pgSQL procedure. Does it accept this returning mode? 2. Our current production server versions is 9.1 and 9.6. Do this versions support returning of tuplestore? 3. Currently my function defined as "RETURNS SETOF". Does definition of the function need to be changed if I rewrite code to return tuplestore? > Andrey Sychev writes: >> I have written C-language function that returns >> multiple composite rows. >> Generally function works as expected, but sometimes problem takes place. >> At rough guess the problem occurs when number of returning rows >> relatively large (more than 100K - 1M). > I do not think it's valid to return from your function with the SPI > context still open. Probably, it seems to accidentally sort of work > as long as you don't return enough rows to cause the outer query to > do anything interesting like spill to disk. > Probably you should re-code this to execute just once and return > a tuplestore. > regards, tom lane -- Best regards, Andrey Sychev andrey.sychev@cifrasoft.com