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 1rkluJ-00FDYi-Qy for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Mar 2024 14:18:08 +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 1rkluI-000Ol2-4J for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Mar 2024 14:18:06 +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 1rkluH-000Oku-Mg for pgsql-hackers@lists.postgresql.org; Thu, 14 Mar 2024 14:18:06 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rkluD-004Tdh-KF for pgsql-hackers@postgresql.org; Thu, 14 Mar 2024 14:18:04 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-513173e8191so1491833e87.1 for ; Thu, 14 Mar 2024 07:18:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1710425881; x=1711030681; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=RZ3HNvaOlFKb1dQnSRRcJ3IY4jd5hhtPcSraW8kqNtU=; b=Me0hR78yQiqQEdzy/wRzKcJou69v2Ou3s+4lbjXw4MmqUde5AKSXWalIzqCijuH+8J 0qyJsrDVp0zal5rG6dG3ocw/pme0yk++c5Lj3fKWnJZTCdKgfyDDy/RA79LrDDdRt7r5 RVH6TJHuFcz15sQcjOkVHFulq/d8vWtSfeQENpCI/9RdK7BXsPG/O8wqdBEoURTxNn/R 2cDIzTezWdxWIQRNPq/nA/E2dzcIxm9sLvSibl20pOaFJsPMbRjn2MHyk7l/m+n1srYK 94E6fCaZlVpoT2Fzz9INVuoll5DFneLVQehMSBkbSC5Y6VfYPpRI9AbF4tSW41JrNE5L M/vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710425881; x=1711030681; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RZ3HNvaOlFKb1dQnSRRcJ3IY4jd5hhtPcSraW8kqNtU=; b=KQ1WXT3t4tV0n4J54iul+nezCF1TC9Zldbs5zrgugALVikC9l7ZFrNlln2sF6A4qPc wMDjrJcqrl8PUHH1CCOUTMlQQ9DKqT2c8q2Q8n2IeSyDUO+fxFyQSOTgUXikziV1OLr/ y7Iz8Gsz4n2LVd8OTBUX+ZKPXYfMOLcxazs9Pzi0SUbMoIFMebOwE92eORLFEF+sFRob HAo40BUdodZwc57E5sFhBgV5vBqnDnCumehgcyD9nCugOTbbDetBNthg+0XLmAlUKmnD 4ZsOiRFtHG7M/n6Uj5Tkneq/Tq8ApmyylJsT98/iXf12+njkJsfWwJ8AkySIpCL1s3Ei ghtQ== X-Forwarded-Encrypted: i=1; AJvYcCUfCVIslO7zaSpBQMadHwN67HeH/yLfNES9l2nbqhIKCjoqNJVRAWH6YxpmxNRY2I76P5lMZyuCKWr1YUdzp8sYqfUbH4qwf6SpIYqj X-Gm-Message-State: AOJu0YwoJEmW0fhFch1kcSnp/i/Qz7m3z9LI0t8epGM2yxXF9+THS04K qFFwhETRu9DAYzVMPNaWqhR91d8xUVV+Sd1RiXsd4JSBwzpYzyyhc0scHHOVbQ== X-Google-Smtp-Source: AGHT+IEeq8zfLzR2/cJzs7piYXHxOUw4ci9w1VD4ISHz3safsCPl1jvwBmWXSOU3PcdSOrAQ2QjnXQ== X-Received: by 2002:a05:6512:2e8:b0:511:49a0:200e with SMTP id m8-20020a05651202e800b0051149a0200emr1097600lfq.37.1710425881010; Thu, 14 Mar 2024 07:18:01 -0700 (PDT) Received: from [10.137.0.18] (84-217-81-189.customers.ownit.se. [84.217.81.189]) by smtp.gmail.com with ESMTPSA id v9-20020a197409000000b00513d021afd1sm280317lfe.103.2024.03.14.07.18.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Mar 2024 07:18:00 -0700 (PDT) Message-ID: <04ff688a-3bd4-4e6b-a1b6-d9e69001daaf@enterprisedb.com> Date: Thu, 14 Mar 2024 15:17:59 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: BitmapHeapScan streaming read user and prelim refactoring Content-Language: en-US To: Thomas Munro Cc: Melanie Plageman , Andres Freund , Pg Hackers , Heikki Linnakangas , Nazir Bilal Yavuz References: <45bed4f3-a5bf-4a34-b544-7e751bd437e1@enterprisedb.com> <91090d58-7d3f-4447-9425-f24ba66e292a@enterprisedb.com> <8de6f6d7-9b9d-47a4-98cc-84bf31a314db@enterprisedb.com> <35bdb7db-7412-4d18-9b06-fea5fcea37bc@enterprisedb.com> <0c7c0673-d012-41d1-8e76-31cc4a1b1eec@enterprisedb.com> <410c42e0-f17b-447f-aa48-fba052d5c080@enterprisedb.com> <186bcba1-a0e1-4871-8ed2-0d301901d0ba@enterprisedb.com> <4f9bab17-baec-4e32-9751-b19206c55373@enterprisedb.com> From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 3/13/24 23:38, Thomas Munro wrote: > On Sun, Mar 3, 2024 at 11:41 AM Tomas Vondra > wrote: >> On 3/2/24 23:28, Melanie Plageman wrote: >>> On Sat, Mar 2, 2024 at 10:05 AM Tomas Vondra >>> wrote: >>>> With the current "master" code, eic=1 means we'll issue a prefetch for B >>>> and then read+process A. And then issue prefetch for C and read+process >>>> B, and so on. It's always one page ahead. >>> >>> Yes, that is what I mean for eic = 1 > > I spent quite a few days thinking about the meaning of eic=0 and eic=1 > for streaming_read.c v7[1], to make it agree with the above and with > master. Here's why I was confused: > > Both eic=0 and eic=1 are expected to generate at most 1 physical I/O > at a time, or I/O queue depth 1 if you want to put it that way. But > this isn't just about concurrency of I/O, it's also about computation. > Duh. > > eic=0 means that the I/O is not concurrent with executor computation. > So, to annotate an excerpt from [1]'s random.txt, we have: > > effective_io_concurrency = 0, range size = 1 > unpatched patched > ============================================================================== > pread(43,...,8192,0x58000) = 8192 pread(82,...,8192,0x58000) = 8192 > *** executor now has page at 0x58000 to work on *** > pread(43,...,8192,0xb0000) = 8192 pread(82,...,8192,0xb0000) = 8192 > *** executor now has page at 0xb0000 to work on *** > > eic=1 means that a single I/O is started and then control is returned > to the executor code to do useful work concurrently with the > background read that we assume is happening: > > effective_io_concurrency = 1, range size = 1 > unpatched patched > ============================================================================== > pread(43,...,8192,0x58000) = 8192 pread(82,...,8192,0x58000) = 8192 > posix_fadvise(43,0xb0000,0x2000,...) posix_fadvise(82,0xb0000,0x2000,...) > *** executor now has page at 0x58000 to work on *** > pread(43,...,8192,0xb0000) = 8192 pread(82,...,8192,0xb0000) = 8192 > posix_fadvise(43,0x108000,0x2000,...) posix_fadvise(82,0x108000,0x2000,...) > *** executor now has page at 0xb0000 to work on *** > pread(43,...,8192,0x108000) = 8192 pread(82,...,8192,0x108000) = 8192 > posix_fadvise(43,0x160000,0x2000,...) posix_fadvise(82,0x160000,0x2000,...) > > In other words, 'concurrency' doesn't mean 'number of I/Os running > concurrently with each other', it means 'number of I/Os running > concurrently with computation', and when you put it that way, 0 and 1 > are different. > Interesting. For some reason I thought with eic=1 we'd issue the fadvise for page #2 before pread of page #1, so that there'd be 2 IO requests in flight at the same time for a bit of time ... it'd give the fadvise more time to actually get the data into page cache. > Note that the first read is a bit special: by the time the consumer is > ready to pull a buffer out of the stream when we don't have a buffer > ready yet, it is too late to issue useful advice, so we don't bother. > FWIW I think even in the AIO future we would have a synchronous read > in that specific place, at least when using io_method=worker, because > it would be stupid to ask another process to read a block for us that > we want right now and then wait for it wake us up when it's done. > > Note that even when we aren't issuing any advice because eic=0 or > because we detected sequential access and we believe the kernel can do > a better job than us, we still 'look ahead' (= call the callback to > see which block numbers are coming down the pipe), but only as far as > we need to coalesce neighbouring blocks. (I deliberately avoid using > the word "prefetch" except in very general discussions because it > means different things to different layers of the code, hence talk of > "look ahead" and "advice".) That's how we get this change: > > effective_io_concurrency = 0, range size = 4 > unpatched patched > ============================================================================== > pread(43,...,8192,0x58000) = 8192 pread(82,...,8192,0x58000) = 8192 > pread(43,...,8192,0x5a000) = 8192 preadv(82,...,2,0x5a000) = 16384 > pread(43,...,8192,0x5c000) = 8192 pread(82,...,8192,0x5e000) = 8192 > pread(43,...,8192,0x5e000) = 8192 preadv(82,...,4,0xb0000) = 32768 > pread(43,...,8192,0xb0000) = 8192 preadv(82,...,4,0x108000) = 32768 > pread(43,...,8192,0xb2000) = 8192 preadv(82,...,4,0x160000) = 32768 > > And then once we introduce eic > 0 to the picture with neighbouring > blocks that can be coalesced, "patched" starts to diverge even more > from "unpatched" because it tracks the number of wide I/Os in > progress, not the number of single blocks. > So, IIUC this means (1) the patched code is more aggressive wrt prefetching (because we prefetch more data overall, because master would prefetch N pages and patched prefetches N ranges, each of which may be multiple pages. And (2) it's not easy to quantify how much more aggressive it is, because it depends on how we happen to coalesce the pages into ranges. Do I understand this correctly? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company