Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvANQ-0006y9-Rd for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Feb 2019 00:32:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gvAMQ-0006Ga-4C for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Feb 2019 00:31:10 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvAMP-0006GT-Ov for pgsql-hackers@lists.postgresql.org; Sun, 17 Feb 2019 00:31:09 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvAMI-0000HH-7d for pgsql-hackers@postgresql.org; Sun, 17 Feb 2019 00:31:09 +0000 Received: by mail-wm1-x341.google.com with SMTP id a62so13755405wmh.4 for ; Sat, 16 Feb 2019 16:31:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=KigNmUyj54HV0JL7NFRFJO8D6Pz2WQA441nmiMX7pY8=; b=P2BxkDMDZD5/Ed+8wUgMtliIIEhi5NNAPdDRqFEQOHqvIwJaMd6QOdrWNaC0pUACn+ v6WRFyIQ4tDSXG2BstUBYl5HF06DGcFxaqvPip+7/GMVhDmlnMFE6ZbRPQrlClbGzzAI //27KqlrVbEr7hDuJTkpvLaDJbVwhHNXeL4bDPsltqlrmc4CSVvyrx2KDtJBHESiVXse t8OAxl3jywt5FWEJah9VsN4UzwWPV9RobfbMsX9PEdYW1Zjs3vKMmM8n+7xmrT8RXZ4C FpGG+lF2e/0RFAXrdw4AQcBw4FmzI6fawbMVZx/gkT7uDMstog1lvkD20XAaUAKhXIrZ baeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=KigNmUyj54HV0JL7NFRFJO8D6Pz2WQA441nmiMX7pY8=; b=BZq/aIezR7zkMLEfyMNr5nkSd1/n6sdOKbHZFajHRda/GHhBPETWbIcamkPXstZiL8 YLVFEJoyo1mjLR55bNh0m6WfYr6Qt1yPlVUteSY23FKYAGX1O/lyZSagG75Wws2MkxHV ACLE80MVrPtyZibAVpnqeZ7e2GeokKO4HNLSdTYa5DTPe1qKqpZdfkqIIN90hV7azu3v yfaV6wHq8YspYOi5M137GMBeFk2ZSkK0NVYxgnZASWqk47GH7Iimqe9s3annWyqufecI 3UPtZiOmlAsLSj9zk2j963fVZ7bEoD+JYx7g/CA056bnktzZK/mxr4fYiywqdGxOl4mc nJXw== X-Gm-Message-State: AHQUAuawi7elmi+6s4zl8KRCpUUEPwloiOfLSX67/7DgK0zHy3z3XM+r 23+Mze8UFY5FVQlrkEfPrTG+IoV5CYgjO9WrjdiTjmMGSN/zdZ8FgI/tMkARU2Ys2qtX7OaeIeI GHm5Zarl79Wysqv5dwqdGEsh4hDpYGkcG2D1zHN6cJTtt3Uv3UaaLAf5X+HTc2TRfZuZ60BD6ZU pKTfFDYlglAQQ= X-Google-Smtp-Source: AHgI3IazkALZN19BZc3yLCpKo7zxBT6e9768h2KjivD1EWUydS5WAZxbUG4lEMSNod7RaJFkedyGmw== X-Received: by 2002:a1c:9acd:: with SMTP id c196mr11602234wme.145.1550363458553; Sat, 16 Feb 2019 16:30:58 -0800 (PST) Received: from [10.137.0.17] (ip-86-49-246-90.net.upcbroadband.cz. [86.49.246.90]) by smtp.gmail.com with ESMTPSA id o18sm33321670wrg.40.2019.02.16.16.30.57 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Sat, 16 Feb 2019 16:30:57 -0800 (PST) To: PostgreSQL Hackers From: Tomas Vondra Subject: CPU costs of random_zipfian in pgbench Message-ID: Date: Sun, 17 Feb 2019 01:30:56 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi, I'm trying to use random_zipfian() for benchmarking of skewed data sets, and I ran head-first into an issue with rather excessive CPU costs. Consider an example like this: pgbench -i -s 10000 test pgbench -s 10000 -f zipf.sql -T 30 test where zipf.sql does this: \SET id random_zipfian(1, 100000 * :scale, 0.1) BEGIN; SELECT * FROM pgbench_accounts WHERE aid = :id; END; Unfortunately, this produces something like this: transaction type: zipf.sql scaling factor: 10000 query mode: simple number of clients: 1 number of threads: 1 duration: 30 s number of transactions actually processed: 1 latency average = 43849.143 ms tps = 0.022805 (including connections establishing) tps = 0.022806 (excluding connections establishing) which is somewhat ... not great, I guess. This happens because generalizedHarmonicNumber() does this: for (i = n; i > 1; i--) ans += pow(i, -s); where n happens to be 1000000000 (range passed to random_zipfian), so the loop takes quite a bit of time. So much that we only ever complete a single transaction, because this work happens in the context of the first transction, and so it counts into the 30-second limit. The docs actually do mention performance of this function: The function's performance is poor for parameter values close and above 1.0 and on a small range. But that does not seem to cover the example I just posted, because 0.1 is not particularly close to 1.0 (or above 1.0), and 1e9 values hardly counts as "small range". I see this part of random_zipfian comes from "Quickly Generating Billion-Record Synthetic Databases" which deals with generating data sets, where wasting a bit of time is not a huge deal. But when running benchmarks it matters much more. So maybe there's a disconnect here? Interestingly enough, I don't see this issue on values above 1.0, no matter how close to 1.0 those are. Although the throughput seems lower than with uniform access, so this part of random_zipfian is not quite cheap either. Now, I don't know what to do about this. Ideally, we'd have a faster algorithm to generate zipfian distributions - I don't know if such thing exists though. Or maybe we could precompute the distribution first, not counting it into the benchmark duration. But I guess the very least we can/should do is improving the docs to make it more obvious which cases are expected to be slow. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services