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 1q8KO5-0002ji-Tu for pgsql-hackers@arkaria.postgresql.org; Sun, 11 Jun 2023 12:41:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q8KO4-0000v3-IH for pgsql-hackers@arkaria.postgresql.org; Sun, 11 Jun 2023 12:41:40 +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 1q8KO4-0000ut-7e for pgsql-hackers@lists.postgresql.org; Sun, 11 Jun 2023 12:41:40 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q8KNw-001bxq-IX for pgsql-hackers@lists.postgresql.org; Sun, 11 Jun 2023 12:41:38 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5149e65c244so4734246a12.3 for ; Sun, 11 Jun 2023 05:41:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1686487290; x=1689079290; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=QN3kF3AOJ81HVkcYDRi1x2Pxzh76e9+Pgs6SXJsfGz8=; b=VPxts4/rfwoblupzqSwpVOmO1OWJ4269Z4B7lLNT9zpv6p5TNbWcEejk9vUPdKzOwB RDuKoeHQ5t35g4y9q/CLdWu0OsNYX6iUvLutehiBt7qgFlyEUxqQKb3X+UZr4Vuh6LRR gk/eRCIkM+V5JEoabg51IFTnVDQcIFo2Z6WykuuL0ee9uFBAUfhwjRMPuSy22P1fGUgO 9zZXVs9hSCsSSjo4ztB6zIRJ26FCyg2mTgp93jEgQJP+Y8xtfVhd+aTjgCrVGgpAwFtm TnPBg+jI3EAfWA7qwrnnX2cGcF7fC8QowZFbmwVsBaHjouFNQ/WWNMAhg3pmyDSx3VRu r8Jg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686487290; x=1689079290; h=subject:from:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QN3kF3AOJ81HVkcYDRi1x2Pxzh76e9+Pgs6SXJsfGz8=; b=k9RQaznBvzDbSSknFytqmMNiTnoRjMbfb6IihdBK+hHH5RT82vFtEtehYARY1rYDql VKk2AsexwHgD4rW8S8PXRP1xPUiV8J34HdmVySewJyxxfpXl/0HK8iU5CO7Z1Yn7zBOg HhqfmkFmnX889g7pAk0ALhe3te84IVw8tXmk8ts9OALKOZ23QJgp2hE4p15zig3NwAuW RxvxBXHh4mOBV47wtMGBG5u4IRdbhXyG7gILGUx3ZQZt66o49lpypufTEaJRKgMNz7CG PCMMgFI8x1XlI1S57ydrWD3b5ZPb3+dJ5qXWcdoFR4bGcpyrFQSnQ/sP1vlNsT9DLdrE QLwg== X-Gm-Message-State: AC+VfDxV6ea+n2QAibfo/Ub7kRnLCRFnlzyhz32OJkBrT5J+jg8BNdYs ulZbdgZzJR0KRgLRcH7xpuAemXXK7d6jqFifCMLa0vjWjk5gC02EnqodEyLx/VDhRBBadncu0i1 5JacYeLN8t5rYrtKSrdDfRhopozXiMg2mqd6RZBkTj8epug4D318WBsAKJYandq8MmDApNB31i0 6G/KoBz2H/rtNuqPDPE8giqzDd5A== X-Google-Smtp-Source: ACHHUZ7blEL7L201bgXcVVFhb97uD5J3e3lCuDPQ5+ibjhuz5ji2ngk9Xnivk0uqffC651QqfHkjhw== X-Received: by 2002:a05:6402:550:b0:514:9563:a32b with SMTP id i16-20020a056402055000b005149563a32bmr3046339edx.16.1686487289636; Sun, 11 Jun 2023 05:41:29 -0700 (PDT) Received: from [10.137.0.17] (ip-86-49-228-220.bb.vodafone.cz. [86.49.228.220]) by smtp.gmail.com with ESMTPSA id l4-20020a056402124400b00510de087302sm3859367edw.47.2023.06.11.05.41.28 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 11 Jun 2023 05:41:29 -0700 (PDT) Content-Type: multipart/mixed; boundary="------------swM0TUTpjOHUAFQI22cq7r0X" Message-ID: Date: Sun, 11 Jun 2023 14:41:27 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: en-US To: PostgreSQL Hackers From: Tomas Vondra Subject: Should heapam_estimate_rel_size consider fillfactor? List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------swM0TUTpjOHUAFQI22cq7r0X Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Hi, While testing some stuff, I noticed heapam_estimate_rel_size (or rather table_block_relation_estimate_size it calls) ignores fillfactor, so that for a table without statistics it ends up with reltuple estimate much higher than reality. For example, with fillfactor=10 the estimate is about 10x higher. I ran into this while doing some tests with hash indexes, where I use fillfactor to make the table look bigger (as if the tuples were wider), and I ran into this: drop table hash_test; create table hash_test (a int, b text) with (fillfactor=10); insert into hash_test select 1 + ((i - 1) / 10000), md5(i::text) from generate_series(1, 1000000) s(i); -- analyze hash_test; create index hash_test_idx on hash_test using hash (a); select pg_size_pretty(pg_relation_size('hash_test_idx')); If you run it like this (without the analyze), the index will be 339MB. With the analyze, it's 47MB. This only happens if there are no relpages/reltuples statistics yet, in which case table_block_relation_estimate_size estimates density from tuple width etc. So it seems the easiest "fix" is to do ANALYZE before creating the index (and not after it, as I had in my scripts). But I wonder how many people fail to realize this - it sure took me a while to realize the indexes are too large and even longer what is causing it. I wouldn't be very surprised if many people had bloated hash indexes after bulk loads. So maybe we should make table_block_relation_estimate_size smarter to also consider the fillfactor in the "no statistics" branch, per the attached patch. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company --------------swM0TUTpjOHUAFQI22cq7r0X Content-Type: text/x-patch; charset=UTF-8; name="relsize-fillfactor-fix.patch" Content-Disposition: attachment; filename="relsize-fillfactor-fix.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy90YWJsZS90YWJsZWFtLmMgYi9zcmMv YmFja2VuZC9hY2Nlc3MvdGFibGUvdGFibGVhbS5jCmluZGV4IGE1ZTZjOTJmMzUuLjNhMjY4 NDZmMDEgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL2FjY2Vzcy90YWJsZS90YWJsZWFtLmMK KysrIGIvc3JjL2JhY2tlbmQvYWNjZXNzL3RhYmxlL3RhYmxlYW0uYwpAQCAtNzM3LDExICs3 MzcsMTkgQEAgdGFibGVfYmxvY2tfcmVsYXRpb25fZXN0aW1hdGVfc2l6ZShSZWxhdGlvbiBy ZWwsIGludDMyICphdHRyX3dpZHRocywKIAkJICogYW5kIChjKSBkaWZmZXJlbnQgdGFibGUg QU1zIG1pZ2h0IHVzZSBkaWZmZXJlbnQgcGFkZGluZyBzY2hlbWVzLgogCQkgKi8KIAkJaW50 MzIJCXR1cGxlX3dpZHRoOworCQlpbnQJCQlmaWxsZmFjdG9yOworCisJCS8qCisJCSAqIFdp dGhvdXQgcmVsdHVwbGVzL3JlbHBhZ2VzLCB3ZSBhbHNvIG5lZWQgdG8gY29uc2lkZXIgZmls bGZhY3Rvci4KKwkJICogVGhlIG90aGVyIGJyYW5jaCBjb25zaWRlcnMgaXQgaW1wbGljaXRs eSBieSBjYWxjdWxhdGluZyBkZW5zaXR5CisJCSAqIGZyb20gYWN0dWFsIHJlbHBhZ2VzL3Jl bHR1cGxlcyBzdGF0aXN0aW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