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.96) (envelope-from ) id 1w5Fa5-0033c5-0O for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 04:10:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5Fa3-00Bb26-1S for pgsql-hackers@arkaria.postgresql.org; Wed, 25 Mar 2026 04:10:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w5Fa3-00Bb1y-0V for pgsql-hackers@lists.postgresql.org; Wed, 25 Mar 2026 04:10:55 +0000 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5Fa0-0000000105c-46Wy for pgsql-hackers@postgresql.org; Wed, 25 Mar 2026 04:10:55 +0000 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-4852a9c6309so40813875e9.0 for ; Tue, 24 Mar 2026 21:10:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774411852; cv=none; d=google.com; s=arc-20240605; b=KwvaPootiBLm2xMrlR8irIX73aGtHiwlDIFavb1RgE2kNjdqSq7JhGDLygducszHbu VpsWnGdFHMiYjCug++VpxCHUmhp6o3XvE+YYH/Y9PqAGXAyjiQE11zAGcmJykPt2v36R VC2MFttsjxpUJ0qFWrFrIpziQjU4Q94l0hzX9vTyj9XEmCR+fBI0zkaBDg/L9QmvkdGP dyEXWDhuCXDa+KkeSFxG8Io15P0wyscZOzE+SD6hHC6TLGUfMK6wBalI1m+9C099ccnO whVs0n3ddjyraGlftiGP60I0anyrAoBeAdt78ptl/pvmq+T4ja9cUUgd5d6pLyBARdpL 7NVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=hgjFc9AXAm8dRXfZvF2b2fW5z7DMHbZNlG6JW3DzgUA=; fh=TVfGSc1ROJnfT2DI0+LhstR3EAj4703kqaeMLTgraqk=; b=Knxe97UQdJoRbqdJgbrh+Z7pfwDLmomlm1i6aP8klfpzaHZu0Chim3k34EXyJIpXio VZISrlal52QP6Buja6jKNQM7uXjEb7CGRy61yI8G3ljs4x6ox4Nw70hZviPQPjfM1O2d 1DrU7VY6U1En0WuMIA1prrvSZToafT/802kjlER2KXyBbJG8Hb6knEojdwefvuhvxeBz MkT6FT+BHGBjP18eYnqydn62atShDRPMQqmui1pZ5i9vXzf8eBwrAjJlOA4g7ZxJapTo DAs9gyxoEitR0rxW/WCyV5iS3J2Wsj0HPnjbyxl5eZ7QIQ15fK67kaYIrLHZ+i2SvoC7 N+Vg==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774411852; x=1775016652; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hgjFc9AXAm8dRXfZvF2b2fW5z7DMHbZNlG6JW3DzgUA=; b=B9h/SBgNIRFSa+dXQqOnanzf06E37lXfu90Mf9rXHk8yO7G50HwEMpPSH0FRWiDqq9 LtOGPV+m+lap/fX+A9BIVG6Cz/4PBlcckdG7QaY0n68Hq3N7JV+ObBCIzfDCtp1JyC8w UKTPEr7iENPUti+1pssqe6awv8/TeUxT4xC4JB/VxaIthiZOo9uukGhFp794EriXrmVI mN+qjaTs3WcD5Fthu+zmL4l9/wZaT6/ioUwy/aBIrd52DMEzDRhKdX3hRIpoApFis6/C /iPpJExhtLtD/KaTAnZJ27RP11qAVhyTpikrV1VsUKWiKoLQ0lgSLmfzIpPBnSpW0BvA awpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774411852; x=1775016652; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=hgjFc9AXAm8dRXfZvF2b2fW5z7DMHbZNlG6JW3DzgUA=; b=j+AYdQNdRyAc43xnxbYR96r1erffJLgO08qvA1TRSoi7rs9rYx+p1rBXXpQ+DyFnuT /G7Vhq4xPRrMugb4z1iAG+UnxCuhc+VGHjxuoyVGfw0M26TPx2L7yEZC9vwLQWnuBCHz wRcG0YM9KHTUjpiGfBCgRb0g6KqlEIAQiGJ/7YlxMHU2lm10s9TMcuEdWQwox035jFte XAZDpEm9OZ7RwqA3jCmfB6RBD+829C92AqWNPQW6M287g0pwhIdCtBZUf22fmfD84IYj cSFrcGStsKyzmOc/8ZjqBHeXCnnCPpsNkhjBWhNmq86fEtqE3LLnjgyhs4ha+F8gcuij lXrg== X-Gm-Message-State: AOJu0Yw0hqMtI1yAa9AQ6qYyUNNDSFV4rZut4G6y+pM3vJzA3hhl///+ 4+hQuKCssaZvZRHj6+taJbF867nRg5A+d9o8GvLempwkuk1fQJ8A8f62rTq4hDFXQ34PVmhgfh9 XCJcNmDQaWuYxnJF79lxLPADufG3/s9g= X-Gm-Gg: ATEYQzwSfOFt8vK28CY8W6BLtP5goywjBP51bLYigagXZW4oMCk6PHJJpW4FPhK/kdQ doUFwKTOmi1QvHJ3oRmPGpKJvDjlP3IT36PYpmClkV6+2UKrhrdx7G0A+45RUX/VxSUzs1HWQYD Sisnvl2Yp3OA8jziHDmVMJCar9qPJMhze+ErlgYVSt22G+ZqE+5nAorCf1R6MQ6lUvk5IQNh8oQ y6cucQ5b3TUSRk0EZjCfucR05aHiclCrknnJ7B3FYtsCSxYQcVYk4GlDgP84WoPFnP1DUYlkkbU 4erspenZ97IalVrZvVhWLcGQyPmuWjvq/QR6N4JrRpnNIgr6gWeQnSdBowN00yId5Glkf4dzqMN YFjK8Dw== X-Received: by 2002:a5d:5d02:0:b0:439:b486:ba6b with SMTP id ffacd0b85a97d-43b88a3dbf6mr2636721f8f.39.1774411851817; Tue, 24 Mar 2026 21:10:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Wed, 25 Mar 2026 17:10:40 +1300 X-Gm-Features: AQROBzDAV6Jt0mSOjimInazmByZC4AiwHScAcpjGxn8L2gUwmRXOEf1ciJuVHV8 Message-ID: Subject: Re: Avoid multiple calls to memcpy (src/backend/access/index/genam.c) To: Ranier Vilela Cc: Pg Hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 10 Mar 2026 at 02:16, Ranier Vilela wrote: > In the functions *systable_beginscan* and *systable_beginscan_ordered*, > is possible a small optimization. > The array *idxkey* can be constructed in one go with a single call to mempcy. > The excess might not make much of a difference, but I think it's worth the effort. This whole thread reeks of you running a static analyser to find optimisation opportunities. If you want to find these, then please be aware that static analysis of code is pretty much useless for this purpose. It has no idea how hot the code in question is. If you want to find places that actually matter, try using perf. Even perf top --pid=N will allow you to see which functions matter enough to warrant further analysis. By all means, if you find something interesting and see a way to make a meaningful improvement, then post about it and show a benchmark script and results which demonstrate the performance gain. For the patch and the analysis that's been done on it so far, please be aware that the struct size that you use will be critical to both the code emitted by the compiler and to the performance. On my machine, ScanKeyData is 72 bytes. None of the godbolt.org links I looked at got this right. One had 8 bytes, and another had 24 bytes. With the default -march flags, you're probably only going to get SSE instructions which will move 16 bytes at a time. Moving 72 bytes is going to need 4 of those SSE moves and a MOV for the 8 byte remainder. That's 5 instructions. 8 bytes is only 1 instruction, and 24 is 2 instructions (16+8). I've forgotten the exact thresholds, but both gcc and clang will inline fixed-sized memcpys below something like 256-512 bytes. I assume the compiler authors did a reasonable amount of analysis to discover those thresholds. 4 ScanKeyData is 288 bytes, which is pretty close to that threshold. Flicking through our code, I see most systable_beginscan() calls are only using 1-2 scan keys. I did see some using 3. I didn't look at all of them, but the ones I saw are not even near the threshold for not inlining the memcpy. But, by all means, prove me wrong and do something like add logging in systable_beginscan and run the regression tests and use the logs to build a histogram of the calls by nkeys. For me, I think the unmodified code is better as the memcpy is a fixed size and can be inlined by the compiler. I might feel different about it if the outer loop could disappear, but that's not possible since sk_attno needs to be set. I also doubt that it matters very much as most of the time scans to catalogue tables will be cached in sys/rel/cat caches. A few tables don't, like pg_inherits, possibly others. But by all means, prove me wrong with perf results and the query that shows systable_beginscan using a meaningful amount of CPU. On a final note, please please stop thinking your static analyser is going to help you improve the performance of PostgreSQL. Go and learn a new tool that's actually useful and fit for the purpose. There's likely years of work doing small things like this in places that actually matter. You seem to have lots of energy for PostgreSQL, so I highly recommend that you take the time and go and learn to use a tool that finds those for you. You can then focus on the places it highlights for you. Otherwise, you're just wasting your time and all of ours. David