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 1mbOeP-0007GX-Cy for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Oct 2021 14:57:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mbOeM-0003cB-Se for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Oct 2021 14:57:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mbOeM-0003c1-Ao for pgsql-hackers@lists.postgresql.org; Fri, 15 Oct 2021 14:57:34 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mbOeI-0007xp-RC for pgsql-hackers@postgresql.org; Fri, 15 Oct 2021 14:57:33 +0000 Received: by mail-ed1-x536.google.com with SMTP id 5so9062463edw.7 for ; Fri, 15 Oct 2021 07:57:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=IyY3ax50SyCEXVxAmtTBCmWdUUgSDpI7hl1L3xr4jY0=; b=fmYwRkRomKfEaJOSXvFqhmbMSa9zYkUVMq/pzvPnVDDQOLq5bRu4SDas0h+9msww/i oVCRpIKApsPT2JTYd7Dgd1/DX1Kk3yIomjimwjRa8IONwwm5Ceqwdpd+EzbpWjceOV2V LjAeeaUtb1Y3QGJ26cY1AHWHkMEPyrt12W8Xo5dvhV4kGKVMXa6EL+DnJphlC5yEIz8Y lYZjSeDmKZduG9mVXrRtfBmyTSryb6qSArd8BRtN211/J8SPfpptqofPtfLJazhU0U0c 7gAhnEzWgsOmoVKwysEg8ecqkD6HrOMxoDyhoKs/HIuAHGiGq70Bl3KtM8NltxIYf5ol aOXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=IyY3ax50SyCEXVxAmtTBCmWdUUgSDpI7hl1L3xr4jY0=; b=Bv4xS/tUMCwH483RTVjHH/uorZ7yGNMv/DPQ/nGmH3Pd/ZhCIwMe+wyH/lGdZ5rv+g JeAexDSVG6/mo0tW4ggHlrnFkskStN9kpIr1x72P+fhxhXO+a2XdweJhWa77xKF5gFBx YYz1MG4qpA3NC/uBQQes/hP6rb0bKtvnbIjwsI8IqgKRTN2uuMbudZ/VYpabKPIIuvoP qNmMEYACeUjuY+5+VXhkVSG/YTAQGUh0BdDdbCnNTR9Qkpa6FeefiIaEP1FhPOs3hYFw VzyIMbe97G9bkHJgH+R2+njw4Vq89n7qUAR1BjHYI2stPdfH97WS9qP4VeSMnlmT+jkO 0a0A== X-Gm-Message-State: AOAM531IxCaYSdgjrh3u6hsUPFoHd2vIM4PHIzVt96ITFR53iKx1vxyd 043oIeFpLtyeM2pWydCtR3En0gQoNytmPH72b18rJQAz+A+Qpa5z0ivIHWP2opx/hPgImSLxLyy I4eYpF81TYKopmNmvUAecFXyC0tp2+8YRaatwl50PqU6i/tqp8RSPS/32Wai5Ed+tMvt3qxl1bd bTJe/8p6dOJaXkmrEH9eeS00VCjsWhuynxBC8i9FxaKg== X-Google-Smtp-Source: ABdhPJz+OzCcND1irSeMtsAp86qu1ILVOShrJHBKum5h2o++y0U5doBp+Rxm83o23i6ddLpTBNbA3Q== X-Received: by 2002:a17:906:82cc:: with SMTP id a12mr7332363ejy.215.1634309789404; Fri, 15 Oct 2021 07:56:29 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-251-104.net.upcbroadband.cz. [86.49.251.104]) by smtp.gmail.com with ESMTPSA id m23sm4315893eja.6.2021.10.15.07.56.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Oct 2021 07:56:28 -0700 (PDT) Message-ID: Date: Fri, 15 Oct 2021 16:56:27 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0 Subject: Re: Partial aggregates pushdown Content-Language: en-US To: Alexander Pyhalov , pgsql-hackers@postgresql.org References: From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Alexander, On 10/15/21 15:15, Alexander Pyhalov wrote: > Hi. > > One of the issues when we try to use sharding in PostgreSQL is absence > of partial aggregates pushdown. > > I see several opportunities to alleviate this issue. > If we look at Citus, it implements aggregate, calculating internal state > of an arbitrary agregate function and exporting it as text. So we could > calculate internal states independently on all data sources and then > finalize it, which allows to compute arbitrary aggregate. > > But, as mentioned in [1] thread, for some functions (like > count/max/min/sum) we can just push down them. It seems easy and covers > a lot of cases. > For now there are still issues - for example you can't handle functions > as avg() as we should somehow get its internal state or sum() variants, > which need aggserialfn/aggdeserialfn. Preliminary version is attached. > > Is someone else working on the issue? Does suggested approach make sense? > I think a couple people worked on this (or something similar/related) in the past, but I don't recall any recent patches. IMHO being able to push-down parts of an aggregation to other nodes is a very desirable feature, that might result in huge improvements for some analytical workloads. As for the proposed approach, it's probably good enough for the first version to restrict this to aggregates where the aggregate result is sufficient, i.e. we don't need any new export/import procedures. But it's very unlikely we'd want to restrict it the way the patch does it, i.e. based on aggregate name. That's both fragile (people can create new aggregates with such name) and against the PostgreSQL extensibility (people may implement custom aggregates, but won't be able to benefit from this just because of name). So for v0 maybe, but I think there neeeds to be a way to relax this in some way, for example we could add a new flag to pg_aggregate to mark aggregates supporting this. And then we should extend this for aggregates with more complex internal states (e.g. avg), by supporting a function that "exports" the aggregate state - similar to serial/deserial functions, but needs to be portable. I think the trickiest thing here is rewriting the remote query to call this export function, but maybe we could simply instruct the remote node to use a different final function for the top-level node? regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company