Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q6AjN-0001a5-F3 for pgsql-novice@arkaria.postgresql.org; Mon, 05 Jun 2023 13:58:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q6AjM-00073h-Bl for pgsql-novice@arkaria.postgresql.org; Mon, 05 Jun 2023 13:58:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q6AjM-00073X-3G for pgsql-novice@lists.postgresql.org; Mon, 05 Jun 2023 13:58:44 +0000 Received: from mail-ej1-x635.google.com ([2a00:1450:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q6AjJ-000LtP-BT for pgsql-novice@lists.postgresql.org; Mon, 05 Jun 2023 13:58:42 +0000 Received: by mail-ej1-x635.google.com with SMTP id a640c23a62f3a-977d4a1cf0eso196309566b.1 for ; Mon, 05 Jun 2023 06:58:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20221208.gappssmtp.com; s=20221208; t=1685973519; x=1688565519; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=dNAVrxt5RQXGaUnXQyrlhCvrpJkufWSq4DiIOg4IpfM=; b=datXcxukGDJFbeF1orVHMVz4ziVWiBnvGU7StwGNAIqQF437Bp5Ck7DCdvluSQYljy q4G+YxH2L9CcnXIWhAr/m/WRAaMEbA4x1MVHie1zl6btatQvZAmiKMcnsiIx5J3gjpI8 xT7tewbItmbq4eOU8E7D0CHpu5OYzJUSPOqzpDYp5CsANrT2eKVUqE5l5YF9IuUxyY2/ 37bAzt8nzvCFd61BvqIZ+8T5XwYZvN1kfDsph6YPfM4MrEbfDp+4cO8jBAEg85feWh5k cUx0LBlB+U371YfWtsRtKbjyql9ZzYGqK6hqpsDy7LxFFh+sdGEQrlKqqn0RTJFjM2zl Cq9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685973519; x=1688565519; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=dNAVrxt5RQXGaUnXQyrlhCvrpJkufWSq4DiIOg4IpfM=; b=UD3yjy3wTluLvHX8ZtSOdRMLEd4/x3tXawehwJGJ+aK70oTcege2Sn741evk0KYHhc muZoGZ1RBnTug/Zw9qdVIWn4S4xPVzVwAuOP5UjKIAORf5M/YnHZ2Vt0TjWdLY6Ye/kK 9eiADPkYoEf64cYwKOLJaEalYIWgw74rW4gUGJMhovV2oiocfUhas8MS804nPTZ/zjro 9O7AEI5VqJC8xNd5dZd62FkgTvZUxcUiHikM0SuxGEoYSVUwevB5yxZGUpusJL8v25vD TwrdaLStT3nS5kN+Xz/2vg3cTCwmF3AuieE5450wRRTwZbxIJQhDXOCz6idrw0MTj6dA OmjQ== X-Gm-Message-State: AC+VfDwluSbYv4J1GuWNFBghr+/Nc991clH29UgyC1Cb8D/sLQWw4AaQ +jFSolNBuicjHuqul5cBnx6yf//T350yCC4C/dY= X-Google-Smtp-Source: ACHHUZ4F068lsh/J503mYT2R6xzzbVzCFGYGoo0S/azuzJJnaqeApp0vCitJ0oVhhMueu8rOnP13Ig== X-Received: by 2002:a17:907:7fa7:b0:974:1f03:fcd1 with SMTP id qk39-20020a1709077fa700b009741f03fcd1mr6966768ejc.3.1685973519589; Mon, 05 Jun 2023 06:58:39 -0700 (PDT) Received: from localhost.localdomain ([2001:871:5e:6888:bdff:4d5b:3b36:4e7b]) by smtp.gmail.com with ESMTPSA id e18-20020a170906c01200b0096ace550467sm4373259ejz.176.2023.06.05.06.58.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 06:58:39 -0700 (PDT) Message-ID: <9471e8396d5e0de1b0b087678f52e9dc478837a8.camel@cybertec.at> Subject: Re: Does PostgreSQL cache all columns of a table after SELECT? From: Laurenz Albe To: Tim Schwenke , pgsql-novice Date: Mon, 05 Jun 2023 15:58:38 +0200 In-Reply-To: <1888b7bb7fa.112313046679561.2788161417267896332@trallnag.com> References: <1888b7bb7fa.112313046679561.2788161417267896332@trallnag.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2023-06-05 at 14:15 +0200, Tim Schwenke wrote: > Hello, >=20 > I have the following table with the following columns: >=20 > large_a: text (few dozen characters up to a few hundred) > large_b: text (few dozen characters up to a few hundred) >=20 > The table has several million rows. The DB is running on a large machine. >=20 > I perform the following query: >=20 > select large_a from table; >=20 > The first query takes a few minutes. Afterwards I see that the cache in m= emory has grown. Next query only takes a few seconds. >=20 > What I want to know: >=20 > Does the cache also contain large_b? Or is only large_a cached? Assumptio= n is that memory is large enough to fit everything. It depends. If "large_a" and "large_b" are TOASTed, then they will only be cached if th= e column is explicitly selected. For shorter attributes, the whole row is in one piece, and all columns will= get cached. However, for sequential scans that are larger than a quarter of shared buff= ers, PostgreSQL uses a ring buffer to avoid blowing out the cache. In that case= , most data won't be cached for the next query. Yours, Laurenz Albe