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 1u1hca-006gOr-AX for pgsql-general@arkaria.postgresql.org; Mon, 07 Apr 2025 08:14:20 +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 1u1hcY-00Dk0G-Hz for pgsql-general@arkaria.postgresql.org; Mon, 07 Apr 2025 08:14:18 +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 1u1hcY-00DjxS-6W for pgsql-general@lists.postgresql.org; Mon, 07 Apr 2025 08:14:18 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1u1hcW-003QvG-1R for pgsql-general@lists.postgresql.org; Mon, 07 Apr 2025 08:14:17 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-ac2bdea5a38so666537666b.0 for ; Mon, 07 Apr 2025 01:14:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1744013655; x=1744618455; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=l/V0Go0fxZhGQxkV5UgG7V3lBXWnu6rU+d3/0Mi8Sss=; b=knfyhk0bbgtikC3GhaWLlbRraT52grLWoZSexKSLQEywlf4XENv7RaEZKwSpDRN12V mwXopOCTKR9B9ZdT3V9c+qP+S1RzmTpuodvwbff0Tkxjbx22nORBcECxupO5fHFzIbEA YF7fxT3e/zCOSzoAFrDknNefdm3sVouJ9dApDb73Etf9dy2UPYJ8apWz+BEJomk6Bbmf xeWPvHsz1OkwdQojhTlmHAUZRoF3OLKzswwqIJ4eghLO/MhaUvKi5M/fU/BMGwFFC5br xXkkALnMOxAFMmT7nH+//otDUi6YhaQDp360VPgsF9EQvKu1UnI9mgttqgGCFS5ikJJT eFWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744013655; x=1744618455; h=mime-version:user-agent:content-transfer-encoding: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=l/V0Go0fxZhGQxkV5UgG7V3lBXWnu6rU+d3/0Mi8Sss=; b=uiUUEvGgxNsiTx1g/BNFUWm0LXXLKIpjX/6M4oDo81gOXqEEsrK7YRUcEnihS292Bc f2BnThgS3UPuhnOdItalbXL0gr5Fr/V48nwj+ZM8rUazRVjQGxi1vpV52CySUyypoJ1A 5ZqwhnALcKJVODLNnnM+WuF8GMvpCtyesI+vepZk4lRTr+DdJ5xrotktaZpzV3+l2PLn KAaLO92Jp2eaOqshiDa57yiZdRuHytG6NfJgf6BTVL6uz4Vt3Zsb9fRYCOUzRJOBqTGQ BHsxfpXMrYPPS7maZ/jhQ4Az6DEyGmlsQT6tu4VNip0Jy7mU0Ku0CwiPGptp3Y43oUtu Yf7w== X-Forwarded-Encrypted: i=1; AJvYcCXpvhPHKO7gsdsTKp815mY5gEvDrDPqeYEQs92eOAVlGWRhqVanLYa2R9yiJ8l9+GU+mG+Zf1lwMvFNZbp9@lists.postgresql.org X-Gm-Message-State: AOJu0YwSdcYehsSvOtheRmbdL41oYTIgwH6OkGrby4sIE23Tnw3imkVP QqeFxcbD3A4LzylvvZE1HkqstBqxa6qRbnIYrQT5L718lui91pyaOxGxY6pfJaw= X-Gm-Gg: ASbGncugfN2Ybx8HI3BB9ga9dwkuUi+0Xh6keW4HSQDUmpDhy6BPkpcBKOJYH+xGKnA X2XaoUtqj6AwMLDMLK4OQ/aE1N7AU/SP0qrVt/2HJs/ERccyjLPcvFMypqdqeHN7HesBeror4cP nrLwMAbh0ogor9dMnMqsqZbZ3ffgjZA7P/yZEgN7bNCQ5GaQ9YwUU3bJUFliv2k5oZdi3Vfis7m PlVc5Euwca4gj08THDdaj+GJpelRMphbFdc/nKJVWaGrq8ox2vIj29hhjdAiqPfNzC9+Qd8avNO YrAezycw95Vt3nvTX43i6FqelPYn9zCO3jH6NKeyCx2uIunjJtdTkS+ZQAJGdE5sWA== X-Google-Smtp-Source: AGHT+IHekxeKPbf0B+2i6U92rj5gXnx2SP3rv04P8Mz7EPZy87mq8Eb5NVaLPthkCkJgW48kFzlOpQ== X-Received: by 2002:a17:906:3e4c:b0:ac7:ee4c:f4c2 with SMTP id a640c23a62f3a-ac7ee4cf50cmr555701466b.40.1744013655050; Mon, 07 Apr 2025 01:14:15 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:9a63:6e75:d921:166e:9c61]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ac7bfea2a67sm702332566b.73.2025.04.07.01.14.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Apr 2025 01:14:14 -0700 (PDT) Message-ID: <589a474cc1f5e804bad53467238bb15187ba38a4.camel@cybertec.at> Subject: Re: Will PQsetSingleRowMode get me results faster? From: Laurenz Albe To: Stijn Sanders , Tom Lane Cc: Daniel Verite , pgsql-general@lists.postgresql.org Date: Mon, 07 Apr 2025 10:14:14 +0200 In-Reply-To: References: <140214eb-a8ce-4285-afdd-7770c5a9d274@manitou-mail.org> <1335670.1736193981@sss.pgh.pa.us> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.3 (3.54.3-1.fc41) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 2025-04-04 at 13:41 +0200, Stijn Sanders wrote: > On Mon, Jan 6, 2025 at 9:06=E2=80=AFPM Tom Lane wrote= : > > So in principle, you might get best results by defining your query > > with DECLARE CURSOR and then using PQsetSingleRowMode on the FETCH. > > But it'd really depend on the particular query whether this gives > > any benefit. >=20 > That's a really nice suggestion, and it took me some time to set up a > suitable test environment to see if it would work, but using separate > PQsendquery/PQexec calls for: >=20 > start transaction read only > declare cr1 no scroll cursor for select (and the rest of my query) > fetch next in cr1 >=20 > it seems like the fetch instruction still takes about as much time as > the full 'normal' select would, I tried a few different queries, but > I'm still suspecting PostgreSQL's internals is waiting for the data to > all 'be ready' before it can send any data over, even if these > PQgetResult's for each fetch will have a PQntuples of 1. > (I even tried with "fetch 8", and PQntuples neatly serves 8 at a time, > but still after about the same time PQsendquery(,'select... would > take) > Or could there still be something that I'm doing that prevents 'firehosin= g'? That looks like you are doing everything right, but there just isn't a "fast start" execution plan, and calculating the first row already is taking a lot of time. Perhaps you can tell PostgreSQL to optimize for a fast start plan more aggressively by lowering "cursor_tuple_fraction" inside the transaction: SET LOCAL cursor_tuple_fraction =3D 0.001; Yours, Laurenz Albe