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 1hj0jP-0001VV-1H for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 12:20:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hj0jN-0006iF-GQ for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 12:20:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hj0jN-0006gN-1N for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 12:20:53 +0000 Received: from mail-lj1-x241.google.com ([2a00:1450:4864:20::241]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hj0jK-0005Q5-D8 for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 12:20:52 +0000 Received: by mail-lj1-x241.google.com with SMTP id a21so5982945ljh.7 for ; Thu, 04 Jul 2019 05:20:49 -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:subject:mime-version :content-transfer-encoding; bh=p++gcOLuCkQsLYOJD10f7xxjvO8HgQMFYJYzAzJNBvA=; b=IHRLX3kXYpBv8HeUWVogoVU0SVAb+fI18QxvOkCTTSoZ6RJou9PTzfT7tzZ2VF6YOY kCI5Bw3XrZCEGD+ijCndbyiwbGQ7tN8UnL2pgxGwP+FCkZtYwBdSyFXBnF8Za7NVeUUj V3QMpSmiK+pfezp4OABwUUQMsg+Z3qgXemz8iZP2k0DHCWnblHCz+ykuhyMQr4199hdH HPaawKhlbu6lZbOqtS4wpd8Ur0PPpFv8D7PCQZt5cCaw1OfVkg6VqqtcBzSEvSQO7reI iElT5aKqJxgBOEcV/olOibl20ob6LyRe8c2cUlfmLg+3kAU/uKMbIMmyVKzgIkd9VgNy PRUA== 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:subject :mime-version:content-transfer-encoding; bh=p++gcOLuCkQsLYOJD10f7xxjvO8HgQMFYJYzAzJNBvA=; b=DmL8bxd1XBW7zBM85UOA35NmkgSAuhkOPChJpsmsJhv4EQP049V0mrP41YkZKEVYPj t63pSCEsXm8YpTh3Q8bSknHwzyB0ZW5SL3+4YJiLBNSmokBRQRzD8Ohh3Un4cYir/Xz5 Z5A3+/ZLHIr9SjEh1O2l/mxN1CiC6W/ppbLgatp6Uuso5t+B+zZb/KZMkaMNOSTUhZO1 BObJ6s3RfP3oFZlaAr2jUstXAfaW5tsfVBxfS2Z9Hyf884DNmO90FE1E/G53wDAlmI94 ExuUkeT+BoKj+8DN5twADMae39t9s8+SbXN02RUIjciXLkzMQQFY+6N0VNTEswK/zHLg V0ZA== X-Gm-Message-State: APjAAAUplHpPoDKnQ3x3G4BAqIYZEw/ThQRtWoa38jZaVHMTZN0mmt8/ zj0eFDiDOyFMlZ8i84/uX/TgydsL4uxFcw== X-Google-Smtp-Source: APXvYqwXVFju4xFBUWz2L2Y1AC624m/gzbeiR5N6kHVPyRyr8zPtfb2UglzRbQqx+m+pLQm+0iWMrw== X-Received: by 2002:a2e:b009:: with SMTP id y9mr24640183ljk.152.1562242849231; Thu, 04 Jul 2019 05:20:49 -0700 (PDT) Received: from [192.168.67.102] ([84.201.247.191]) by smtp.gmail.com with ESMTPSA id g4sm867800lfb.31.2019.07.04.05.20.47 for (version=TLS1 cipher=AES128-SHA bits=128/128); Thu, 04 Jul 2019 05:20:48 -0700 (PDT) Date: Thu, 4 Jul 2019 16:20:46 +0400 From: Andrey Sychev Organization: =?windows-1251?B?zs7OICLW6PTw4PHu9PIi?= X-Priority: 3 (Normal) Message-ID: <851561973.20190704162046@cifrasoft.com> To: pgsql-general@lists.postgresql.org Subject: Error: rows returned by function are not all of the same row type 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 Hi, everyone, 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 have added some checkpoints. P5 and P6 are present in snippet. The function always reaches checkpoint P5, but when number of returning rows relatively large, sometimes before P6 it returns error: "rows returned by function are not all of the same row type" Supposedly, at some iteration on SRF_RETURN_NEXT Any ideas? Below is a snippet of code: #include "postgres.h" #include "funcapi.h" #include "executor/spi.h" Datum my_func(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(show_eudc); Datum my_eudc(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; int call_cntr; int max_calls; TupleDesc tupleDesc; /* Build a tuple descriptor for our result type */ if(get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE) { ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Function returning record called in context that cannot accept type record"))); } if(SRF_IS_FIRSTCALL()) { MemoryContext oldcontext; funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); My_SPI_call_context ctx; memset(&ctx, 0, sizeof(My_SPI_call_context)); int ret; /* Connect to SPI manager */ if((ret = SPI_connect()) < 0) { /* internal error */ elog(ERROR, "spi_match: SPI_connect returned %d", ret); SPI_finish(); PG_RETURN_VOID(); } /* some setup code */ const char* stSQLDef_0[1] = { "CREATE TEMPORARY TABLE results (v1 BIGINT NOT NULL, v2 INTEGER NOT NULL)", }; for(int k=0; k<1; k++) { ret = SPI_exec(stSQLDef_0[k], 0); if(ret != SPI_OK_UTILITY) { elog(ERROR, "SPI_exec (0)-(%d) returned %d", k, ret); my_spi_free_context(&ctx); PG_RETURN_VOID(); } } /* many code */ const char* stSQLResultsInsert = "INSERT INTO results (v1, v2) VALUES (%ld, %d)"; for(int k=0; kmax_calls = 0; funcctx->user_fctx = NULL; if(proc <= 0) ereport(NOTICE, (errmsg("SPI_execute (10) returned %d", ret))); } else if(proc) { spi_tuptable = SPI_tuptable; funcctx->max_calls = proc; funcctx->user_fctx = spi_tuptable; } my_spi_free_context(&ctx); tupleDesc = BlessTupleDesc(tupleDesc); funcctx->tuple_desc = tupleDesc; MemoryContextSwitchTo(oldcontext); } funcctx = SRF_PERCALL_SETUP(); call_cntr = funcctx->call_cntr; max_calls = funcctx->max_calls; if(call_cntr < max_calls) { SPITupleTable* table = (SPITupleTable*)funcctx->user_fctx; Datum results; /* Results tuple */ Datum column[2]; bool isColumnNull[2]; HeapTuple tuple; int m; if(table) { for(m=0; m<2; m++) { column[m] = SPI_getbinval(table->vals[call_cntr], table->tupdesc, m+1, &isColumnNull[m]); } tuple = heap_form_tuple(funcctx->tuple_desc, column, isColumnNull); results = HeapTupleGetDatum(tuple); SRF_RETURN_NEXT(funcctx, results); } } else { int ret; SPITupleTable* table = (SPITupleTable*)funcctx->user_fctx; if(table) { SPI_freetuptable(table); } ereport(NOTICE, (errmsg("P6: [%s]-(%d)", (const char*)__FUNCTION__, max_calls))); ret = SPI_exec("DROP TABLE results", 0); if(ret != SPI_OK_UTILITY) { elog(ERROR, "spi_match: SPI_exec (20) returned %d", ret); } SPI_finish(); SRF_RETURN_DONE(funcctx); } PG_RETURN_VOID(); } -- Best regards, Andrey Sychev andrey.sychev@cifrasoft.com