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 1oEtvQ-0004s2-Kd for pgsql-general@arkaria.postgresql.org; Fri, 22 Jul 2022 14:46:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oEtvP-0007on-GR for pgsql-general@arkaria.postgresql.org; Fri, 22 Jul 2022 14:46:43 +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 1oEtvP-0007ko-5h for pgsql-general@lists.postgresql.org; Fri, 22 Jul 2022 14:46:43 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oEtvM-0004XN-Oy for pgsql-general@postgresql.org; Fri, 22 Jul 2022 14:46:42 +0000 Received: by mail-lf1-x131.google.com with SMTP id p11so3153695lfu.5 for ; Fri, 22 Jul 2022 07:46:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:subject:date:message-id:user-agent:mime-version; bh=WvtKqX04pe/9E+Ss6yQwnciQb/rz4NgXd8OFriru6NM=; b=K5wtYGxwBa28E7eil/UP4l5b4px0dt/o+z30UkF+KGVJ3V/eQhCXk7C2Tpy3NrH0QT zZ9Q6wfL5WUOL/qVhT+p/s18gmydoI3qYTzFrNoyewfLa9OTc8YTbewyFxcQYe91ofdl wmMbsG8HEV+TMb+XPt7L8FtI2cu0mJRPGiREZ+lKoOXpYrRujWUGgHUcFJ0s6ifGXpQu wAZtI7xdbbkYXCbPsxaYTeZkb50L5Fcvqt0jETElWsnFPcvmPpkkpF2/lm3YwrvJq82N iMFjplZptc09JWcSubwAGa+fkqThbUoZsIAtUgSHPxkfC39yAaKpUY+JAXwmXZjjX/50 XdHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:user-agent :mime-version; bh=WvtKqX04pe/9E+Ss6yQwnciQb/rz4NgXd8OFriru6NM=; b=o9OM62XuZqGZmhkLM/OB3P0rYNt4Py4BmyiFbDa0mqt/Q6eGacWDUKSuEvWRXrpvSp QVkF2lq3dtcxoMpAaBf2w5WNmgRPhEDNUgH8+PPAL9nWgewxbQFaaywedGyJkw+7+LPN ryb3XViJAFGZq7qgOHB5/Cc9UNJIFslCyG6NpcybbLj6/DOp4tMlOo74dwm58sKZ3Ge6 QU86NNjArQmoPFmxKicHUvOqPEhMOm4YN6B4mJMPcvPKJ+Lnz99RjsKmNpqv6fSaSRa7 4AIgNNvjD8Fcir5p/2o1SFCvPVJ9r7shiMMa4EgQQ/xLbB3YF3kFUK1snq4LMjrAz2/5 Z7Fw== X-Gm-Message-State: AJIora+HBjZKYV5xv4QMC00YubpfWQTyyt+UXRyCdLven7+Q8x71VcGX wR0Q/z2NH3+q0++y17ii5A0wdKAU//NiXA== X-Google-Smtp-Source: AGRyM1tQEuplJbBcORZC9unAI3TGc6vYZICyHZfgjoq6I7mMhlGZ3ubC+gS5i2EOBhEEccm3SamXdA== X-Received: by 2002:a05:6512:1321:b0:489:e247:39b9 with SMTP id x33-20020a056512132100b00489e24739b9mr151771lfu.513.1658501198877; Fri, 22 Jul 2022 07:46:38 -0700 (PDT) Received: from seb ([77.232.146.50]) by smtp.gmail.com with ESMTPSA id a9-20020a195f49000000b00489ec0d7636sm1087317lfj.110.2022.07.22.07.46.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jul 2022 07:46:38 -0700 (PDT) From: Sergey Burladyan To: pgsql-general@postgresql.org Subject: How does postgres sort large strings? Date: Fri, 22 Jul 2022 17:46:37 +0300 Message-ID: <87h739gq7m.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi! I thought that the sorting values are stored entirely in work_mem, but in fact it works somehow differently. Can anyone suggest how this works? For example, I created this 512MB incompressible file and test table: tr -dcs '[:print:]' '[:print:]' < /dev/urandom | tr -d '"'\' | dd bs=1K count=512K of=asciidump create unlogged table t1 (v text); insert into t1 select pg_read_file('asciidump') from generate_series(1, 10); select pg_column_size(v), octet_length(v) from t1 limit 1; pg_column_size | octet_length ----------------+-------------- 536870912 | 536870912 set work_mem to '64MB'; Now I think that 64MB is not enough to sort such large values and postgres will use temp files, but in fact it does not. select temp_files, temp_bytes from pg_stat_database where datname = current_catalog; temp_files | temp_bytes ------------+------------ 0 | 0 explain (analyze,verbose,buffers) select v from t1 order by v; QUERY PLAN --------------------------------------------------------------------------------------------------------------- Sort (cost=94.38..97.78 rows=1360 width=32) (actual time=6433.138..6433.140 rows=10 loops=1) Output: v Sort Key: t1.v Sort Method: quicksort Memory: 25kB Buffers: shared hit=543881 read=679794 written=118012 -> Seq Scan on public.t1 (cost=0.00..23.60 rows=1360 width=32) (actual time=0.007..0.009 rows=10 loops=1) Output: v Buffers: shared hit=1 Planning Time: 0.035 ms Execution Time: 6433.155 ms > Sort Method: quicksort Memory: 25kB select temp_files, temp_bytes from pg_stat_database where datname = current_catalog; temp_files | temp_bytes ------------+------------ 0 | 0 WOW! How does it work?! :-) -- Sergey Burladyan