Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fG2mw-0004iO-NK for pgsql-general@arkaria.postgresql.org; Tue, 08 May 2018 13:36:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fG2mv-0001WM-Nm for pgsql-general@arkaria.postgresql.org; Tue, 08 May 2018 13:36:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fG2mt-0001WB-Ol for pgsql-general@lists.postgresql.org; Tue, 08 May 2018 13:36:17 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fG2mp-0004WK-SE for pgsql-general@postgresql.org; Tue, 08 May 2018 13:36:14 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 6BE58226CF; Tue, 8 May 2018 09:36:10 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 08 May 2018 09:36:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=2C/vCiTj50dmDTdW4wq5oaYMAQD1z QzVv2Ubpn9m3Zs=; b=ZH5SULgWjQFJNsYdXTfO0qgSRVmMzTObwyA6lzmK9+7r/ nMPfK//9EhyA8ttrHB+aAUB+BOrKHow4HIKj3QxXH1HMWUDTVPzWRo5f9ozX8tCL R9TgV8+7ZDhigHZh9ndBQpl6vnNfNFYyCpcrSrK/2Zj8/ZzjEC2FZDkEVKJfasGd wB/B8s8WlIHe/x7uMSqegLSBIkdY9/3l36RHeoBQO4R8RZivnB6YJjSzGnohnePd pILGJVEJRS87pG7P6v9PKaAHV0vSrq5fA3DuErn5Cta66k3M38248ES9htolUgyq RbBoYxw6NvjoF8gBj07sSbfp55gWuQZFwEb1PChWQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=2C/vCi Tj50dmDTdW4wq5oaYMAQD1zQzVv2Ubpn9m3Zs=; b=agOQlIAs+KHcaOX4P8BxX0 xTDsGrRwWBpCBfybkJLv/3c4zZiqlKlVGFm8E6RlVx6jRvAyj+KcRQmPzmlFQ6rk q9eLvWbRFTqLCLuKjeW7hFjeNM5fztBNPWd8kS2kV+ihgBEYreoV9ZDfqIZNsmCd lrnk3iLZ4uUWcUZw2GSEVDIPZAJnlkTS7WQR6RdEsPNi/PTZplN7pXzojSmW213P SXfxmDLMRJIZVl6uUPjQhz5ZfcqES4on+LSNOzwBZ2b9WPwgyV14T7mEvfjzqeQX lemIn7qQDJa1idRgrMHmGGDtFwKzrZUWMwJXbwdEpI3ZxTdI0/lHxzUawSj5Y/fg == X-ME-Sender: Received: from [192.168.1.2] (unknown [71.212.135.74]) by mail.messagingengine.com (Postfix) with ESMTPA id C3F6F10256; Tue, 8 May 2018 09:36:09 -0400 (EDT) Subject: Re: cursor empty To: Philipp Kraus , "pgsql-general@postgresql.org" References: From: Adrian Klaver Message-ID: Date: Tue, 8 May 2018 06:36:08 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 05/08/2018 05:39 AM, Philipp Kraus wrote: > Hello, > > I have got a function with this definition: > > CREATE OR REPLACE FUNCTION vectorize(refcursor) > RETURNS SETOF refcursor > LANGUAGE 'plpgsql' > COST 100 > STABLE > ROWS 1000 > AS $BODY$ > > begin > perform pivottable( > '_vector', > 'select * from viewdata', > array['ideate', 'name', 'description', 'latitude', 'longitude'], > array['parametername'], > array['parametervalue::text', 'parametervaluetext'] > ); > open $1 scroll for select * from _vector; > return next $1; > end > > $BODY$; > > The perform call creates a dynamic column pivot table, if I run manually > > select pivottable( > '_vector', > 'select * from viewdata', > array['ideate', 'name', 'description', 'latitude', 'longitude'], > array['parametername'], > array['parametervalue::text', 'parametervaluetext'] > ); > select * from _vector; > > I get all the data in the output, so everything is fine. > > My goal is now to encapsulate the two lines into a function, so I define > a stable function and based on the dynamic column set a cursor. I get in > pgadmin the column names back, but the rows are empty if I run: > > select * from vectorize('myvec'); > fetch all from myvec; > > Can you explain me, which part is wrong? I am going to say: perform pivottable( ... https://www.postgresql.org/docs/10/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-NORESULT "Sometimes it is useful to evaluate an expression or SELECT query but discard the result, for example when calling a function that has side-effects but no useful result value. To do this in PL/pgSQL, use the PERFORM statement: PERFORM query; This executes query and discards the result. ..." ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > Thanks > > Phil > > > > -- Adrian Klaver adrian.klaver@aklaver.com