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 1rqG20-00BPOg-Vw for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Mar 2024 17:28:45 +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 1rqG20-003M14-34 for pgsql-hackers@arkaria.postgresql.org; Fri, 29 Mar 2024 17:28:44 +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 1rqFyx-003IgV-DD for pgsql-hackers@lists.postgresql.org; Fri, 29 Mar 2024 17:25:35 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rqFyt-0075Ja-Jc for pgsql-hackers@postgresql.org; Fri, 29 Mar 2024 17:25:34 +0000 Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-56c197d042fso2666127a12.0 for ; Fri, 29 Mar 2024 10:25:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1711733131; x=1712337931; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to; bh=4ApOtM5Qxnn49mzkQ5+efdkT+gTr4Wcpp4gh8Fur8ok=; b=jbVZ6b7CxXiTx9OpsPAH+JAowGZ1CfLNr31U5XL8P+VsL+WHgF8ZzOYGUxlbZmQk2s swnugBWoFFdSxcrFlk6ypf8V8mbMtkDkEYsWCU6FjsAoShlQZ5W/aP7Ojwt8gKrcU/o3 UlNzfIOHFGAEj06pwcPcRzJW5LIzfRhqFShjyX67ATQM6HEtViJjOkZUJxIUzlc02Cjz mnP8mZ/jdthFYF0JHoAapK0oAcA9uHN+L3//i26lGmMN1IkgHxL7gKnVUOfvbROKHpH+ t5JWdwC2TVAddFtXwVxkYcWd43CbNY5+0pP5Y1XgzgY2nb5BPyYX99/ugz1I0d+Im5JB gc3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711733131; x=1712337931; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4ApOtM5Qxnn49mzkQ5+efdkT+gTr4Wcpp4gh8Fur8ok=; b=nVKVvOMTz5SssHbRQXO1GCdR/5W3XdGB0VDBfzFJ9ehZy/VGtna38bSBAuf21/K0WZ 1dn8+Ze43JPmukgTcH03LDDMwlFmCXSYHsRqAVP8cjakK9ycyr2mnXQlh9qHCGZnuBUp qWpvI5UdVzwJrnlUwmQzc8lDoMnYxPxEZVGtxWPg7vxubLLtUm8NwVEjwbDxbURsp94X 38NYDiJH48xoziWO4lfgC6YjaRojihb2Ui1Uk0ccek+4EqKOKEfjlVg3OGuLqzcgStJW iEw+emginKMTrpqmFfNY6oLItfMmoefVrSREKKZVh2y4v/rkhalHEuCI5ujyhddVKfy0 ZRLw== X-Gm-Message-State: AOJu0YxYA9kz+AtLkoP8kElYOVTp6bZ1BH07wEwd0zy7Yh9Viu3swfJ6 T5XG8Ll7QtTguJxPwX8vElpWz6SdEQn0DUzoDASm6oy5N0kN1gXlo3ynJXfpRTI= X-Google-Smtp-Source: AGHT+IEoOJ9vackThufFzS7KPfemBSUw6j2nzLch5NF686rMgGY6BOErJ8cCuxxF9fcqRxug4ICiTw== X-Received: by 2002:a50:d698:0:b0:56b:cf40:f712 with SMTP id r24-20020a50d698000000b0056bcf40f712mr1654375edi.19.1711733131014; Fri, 29 Mar 2024 10:25:31 -0700 (PDT) Received: from localhost.localdomain ([2001:871:260:d122:c45a:b81c:b6bb:c89d]) by smtp.gmail.com with ESMTPSA id n7-20020a509347000000b0056c24df7a78sm2319104eda.5.2024.03.29.10.25.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 10:25:30 -0700 (PDT) Message-ID: <56ac94b3670febcffdd9faf10a08fa9255ca0f04.camel@cybertec.at> Subject: Re: psql's FETCH_COUNT (cursor) is not being respected for CTEs From: Laurenz Albe To: Daniel Verite Cc: PostgreSQL Hackers , Tom Lane , Robert Haas , Jakub Wartak Date: Fri, 29 Mar 2024 18:25:29 +0100 In-Reply-To: <7a3a478be2a16465ac93ba01b13e1081568cbbd9.camel@cybertec.at> References: <7a3a478be2a16465ac93ba01b13e1081568cbbd9.camel@cybertec.at> Autocrypt: addr=laurenz.albe@cybertec.at; prefer-encrypt=mutual; keydata=mQINBGGDwAQBEADgbWy5cKXQld3N2mF+DFyiNFbi2oBl2T+XgxpPF8wTRw2D/u4bBKXP0SYSE/lA86jIVNWWU0gf1KODIkVvgJm2w4vH2VBV1b7ddVViGl1Iu+9zaRnv9wulhnH42KefepXnoean6UT1EzLM0opF/Ik0j+40TxdRtobkBprkQUyHDXWlHc2ffPs3SipyFEP9AVLf7ejRC46CXWDnsqjOBSMEW8Z4HiK/8RrPZBsKLts8dJxKF4pygOdJb0CWk8k/X1jbcfdxo+zOLjOMvJcSJ2pFdJmQHU+JufB3rePziqQ2S9Ur6sccr9XnTC1GVBWN4Lf5VHq+vf+bFJjVwg+2hrySZnAVfcOrxoqFLErr7ug1zN2nM1kcpgA4VWn4gxlJtYNYYq+9WxX5dtvnNANlG3ZCrRKQzl8lxtzoF6Zo7LUhEqPaHDwn7Rvs+IdbOn41lF5UDTJGqmC4gS/bZydW2Fy3YWm4aSaN9fgFf8D+PVkrlKAZB7gBLz1TyHjbcRf85cYF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Mh5Q2FIUfqs46qWhhXjRrdKOEoStYAaikdLu5ZXr7vfb0ZaDh+ZwTQtbSMFolyOkecwI81MCdbMfT/1TqIGTOdAj5as9fAakk0jb2pXgUYQ8X1DVTR8ahSDVEaw9VTmWiSvTxvguVJ1Mb7gG4Gmh6aviDTJhfXtH4rPUNXhDLqrTH8JkJjyKROOMakIF68Hjse5vUfUxreBEOtb5r1Coa2Fe7ncJayaSE7ryrDbFqpZ 36UMAx4ulWMyqJajLNGY0DdG8qIsR5nxRhrnK/mrCidZ8F9/D3bWAl4rjtHlsztN59 +AnW5l0HsQcY9ntFL/zEBOaonjdJf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.4 (3.50.4-1.fc39) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2024-03-29 at 14:07 +0100, Laurenz Albe wrote: > I had a look at patch 0001 (0002 will follow). Here is the code review for patch number 2: > diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c [...] +static bool +SetupGOutput(PGresult *result, FILE **gfile_fout, bool *is_pipe) [...] +static void +CloseGOutput(FILE *gfile_fout, bool is_pipe) It makes sense to factor out this code. But shouldn't these functions have a prototype at the beginning of the file= ? > + /* > + * If FETCH_COUNT is set and the context allows it, use the single ro= w > + * mode to fetch results and have no more than FETCH_COUNT rows in > + * memory. > + */ That comment talks about single-row mode, whey you are using chunked mode. You probably forgot to modify the comment from a previous version of the pa= tch. > + if (fetch_count > 0 && !pset.crosstab_flag && !pset.gexec_flag && !is= _watch > + && !pset.gset_prefix && pset.show_all_results) > + { > + /* > + * The row-by-chunks fetch is not enabled when SHOW_ALL_RESULTS i= s false, > + * since we would need to accumulate all rows before knowing > + * whether they need to be discarded or displayed, which contradi= cts > + * FETCH_COUNT. > + */ > + if (!PQsetChunkedRowsMode(pset.db, fetch_count)) > + { I think that comment should be before the "if" statement, not inside it. Here is a suggestion for a consolidated comment: Fetch the result in chunks if FETCH_COUNT is set. We don't enable chunki= ng if SHOW_ALL_RESULTS is false, since that requires us to accumulate all ro= ws before we can tell what should be displayed, which would counter the idea of FETCH_COUNT. Chunk fetching is also disabled if \gset, \crosstab, \gexec and \watch are used. > + if (fetch_count > 0 && result_status =3D=3D PGRES_TUPLES_CHUNK) Could it be that result_status =3D=3D PGRES_TUPLES_CHUNK, but fetch_count i= s 0? if not, perhaps there should be an Assert that verifies that, and the "if" statement should only check for the latter condition. > --- a/src/bin/psql/t/001_basic.pl > +++ b/src/bin/psql/t/001_basic.pl > @@ -184,10 +184,10 @@ like( > "\\set FETCH_COUNT 1\nSELECT error;\n\\errverbose", > on_error_stop =3D> 0))[2], > qr/\A^psql::2: ERROR: .*$ > -^LINE 2: SELECT error;$ > +^LINE 1: SELECT error;$ > ^ *^.*$ > ^psql::3: error: ERROR: [0-9A-Z]{5}: .*$ > -^LINE 2: SELECT error;$ > +^LINE 1: SELECT error;$ Why does the output change? Perhaps there is a good and harmless explanation, but the na=C3=AFve expectation would be that it doesn't. The patch does not apply any more because of a conflict with the non-blocking PQcancel patch. After fixing the problem manually, it builds without warning. The regression tests pass, and the feature works as expected. Yours, Laurenz Albe