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 1m2sHn-0001aD-9i for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 09:31:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m2sHl-0000b5-3q for pgsql-hackers@arkaria.postgresql.org; Mon, 12 Jul 2021 09:31:33 +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 1m2sHk-0000ax-S6 for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 09:31:32 +0000 Received: from mail-ed1-x534.google.com ([2a00:1450:4864:20::534]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m2sHh-0006D4-NA for pgsql-hackers@lists.postgresql.org; Mon, 12 Jul 2021 09:31:31 +0000 Received: by mail-ed1-x534.google.com with SMTP id s15so26953673edt.13 for ; Mon, 12 Jul 2021 02:31:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=qCH1vaOur2cpgbZ5t+5gxn9hh26rSyqObkbZhey00Es=; b=tNdKT+Emh3V3tu3S5GxcYGzrNlJNB7MThtA+cczs1HUVGwIpBCk/o/aLzPXCCgnU8/ JoGnKMTmgnb/JQW2Zx3vKenpqkaSknzdG6P8hF7yTsWXuS3MFkrZinZjDkJ1L456HQhI 6W5LFsUzFGumlv6C3I3EZJsIEfgm/jIUBn4vs0wK8OABP8gcFfWAdRwjktKIbE1jd0NP outC+nXq5Lbg4XHgjk7WqO/C8xXWlGDCNwwMOMneeSMxS6WQLrKohxSnV/3GAXgDyeyP jLdrRAdOKgYw61odhDlenlIRY+sYyZU/7ZsvNt0vflIDkKu/+OewQmsukop1KDiAZOAk ir2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=qCH1vaOur2cpgbZ5t+5gxn9hh26rSyqObkbZhey00Es=; b=rFc+7MxOtUeX7VR+m8fyZdBTYnnbJWNN3dU/OmY8ZjBskbr+3XvEnG6Rzh827mpVTx 0yyhecq/43IsqR4oJSSb2cb+UNafvjAFUZmkUXTdBN9gEqFLX9UiS5hUfNmNW0vzZW39 dsSNT591qtEYYfMWy1N9cSWNwj1y0HUlAMVwPnSgCFC5ObtbSRozpcAvbYG9HN5tQv3D iFzWs15z+2OYBE32fz8JCHOjw0fAEeshiHRdMRu6aQsMuAiW2saXuYvmgPfFZPsAZ0VF FkfYJ/dFt8Vo5QQHv9ItMCFbU96ZkQaZA47T9JReC+PXNtZto4hR3ecwKgMh3ThtOTUJ hrpg== X-Gm-Message-State: AOAM533lVZaPpol5/MYOM/oRZbhQAbRtL/P676GICBdnjMwhmoy/MDOC X4HatQtH+nFrNClyxiAEaD1Ti+VP/PWdwMLRQ/Rou4lDxoJr3bTK0hrge2jG61qZqkcNPQVqjEW Fkccxye43vdzftv5/blO5e3NBtXBCst6GVuZlvsTWWQ8tnV2QxuOoe3Ay+tbml/V0J0tTQCzh4m 5GdzQPNMX31L4zKyp/Ea1PNnnquYOJdpqSWTHMayUFTW7klnhGm7IpycGgE/ViWxjmYnym5xxm5 amWmZcDu1fTl67vY+9CRWbFBiycHF7IFvXU+HbAeM2mmaTwd5rgww== X-Google-Smtp-Source: ABdhPJzcCrOk3wMi1gXvPlJfgt5Q8eaDe/XGnvV9wK7037QqFbS99NjMFc09SQX06AuHU2LhbdT92g== X-Received: by 2002:a05:6402:5193:: with SMTP id q19mr65209673edd.258.1626082287589; Mon, 12 Jul 2021 02:31:27 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id k1sm3286872eji.84.2021.07.12.02.31.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 12 Jul 2021 02:31:27 -0700 (PDT) Subject: Re: row filtering for logical replication To: Amit Kapila , Alvaro Herrera Cc: Euler Taveira , Greg Nancarrow , Peter Smith , Rahila Syed , Peter Eisentraut , =?UTF-8?B?w5ZuZGVyIEthbGFjxLE=?= , japin , Michael Paquier , David Steele , Craig Ringer , Tomas Vondra , Amit Langote , PostgreSQL Hackers References: <849ee491-bba3-c0ae-cc25-4fce1c03f105@enterprisedb.com> <202107120148.l4nueeibgynb@alvherre.pgsql> From: Tomas Vondra Message-ID: <33c033f7-be44-e241-5fdf-da1b328c288d@enterprisedb.com> Date: Mon, 12 Jul 2021 11:31:23 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US 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 On 7/12/21 6:46 AM, Amit Kapila wrote: > On Mon, Jul 12, 2021 at 7:19 AM Alvaro Herrera wrote: >> >> Hi >> >> Andres complained about the safety of doing general expression >> evaluation in pgoutput; that was first in >> >> https://postgr.es/m/20210128022032.eq2qqc6zxkqn5syt@alap3.anarazel.de >> where he described a possible approach to handle it by restricting >> expressions to have limited shape; and later in >> http://postgr.es/m/20210331191710.kqbiwe73lur7jo2e@alap3.anarazel.de >> >> I was just scanning the patch trying to see if some sort of protection >> had been added for this, but I couldn't find anything. (Some functions >> are under-commented, though). So, is it there already, and if so what >> is it? >> > > I think the patch is trying to prohibit arbitrary expressions in the > WHERE clause via > transformWhereClause(..EXPR_KIND_PUBLICATION_WHERE..). You can notice > that at various places the expressions are prohibited via > EXPR_KIND_PUBLICATION_WHERE. I am not sure that the checks are correct > and sufficient but I think there is some attempt to do it. For > example, the below sort of ad-hoc check for func_call doesn't seem to > be good idea. > > @@ -119,6 +119,13 @@ transformExprRecurse(ParseState *pstate, Node *expr) > /* Guard against stack overflow due to overly complex expressions */ > check_stack_depth(); > > + /* Functions are not allowed in publication WHERE clauses */ > + if (pstate->p_expr_kind == EXPR_KIND_PUBLICATION_WHERE && > nodeTag(expr) == T_FuncCall) > + ereport(ERROR, > + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), > + errmsg("functions are not allowed in publication WHERE expressions"), > + parser_errposition(pstate, exprLocation(expr)))); > Yes, I mentioned this bit of code in my review, although I was mostly wondering if this is the wrong place to make this check. > Now, the other idea I had in mind was to traverse the WHERE clause > expression in publication_add_relation and identify if it contains > anything other than the ANDed list of 'foo.bar op constant' > expressions. OTOH, for index where clause expressions or policy check > expressions, we use a technique similar to what we have in the patch > to prohibit certain kinds of expressions. > > Do you have any preference on how this should be addressed? > I don't think this is sufficient, because who knows where "op" comes from? It might be from an extension, in which case the problem pointed out by Petr Jelinek [1] would apply. OTOH I suppose we could allow expressions like (Var op Var), i.e. "a < b" or something like that. And then why not allow (a+b < c-10) and similar "more complex" expressions, as long as all the operators are built-in? In terms of implementation, I think there are two basic options - either we can define a new "expression" type in gram.y, which would be a subset of a_expr etc. Or we can do it as some sort of expression walker, kinda like what the transform* functions do now. regards [1] https://www.postgresql.org/message-id/92e5587d-28b8-5849-2374-5ca3863256f1%402ndquadrant.com -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company