Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1whVbO-000Gkq-37 for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Jul 2026 16:58:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1whVbN-008DTx-1X for pgsql-hackers@arkaria.postgresql.org; Wed, 08 Jul 2026 16:58:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1whVbN-008DTp-0T for pgsql-hackers@lists.postgresql.org; Wed, 08 Jul 2026 16:58:25 +0000 Received: from mail-ej1-x62d.google.com ([2a00:1450:4864:20::62d]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1whVbL-00000000J4v-0f7t for pgsql-hackers@lists.postgresql.org; Wed, 08 Jul 2026 16:58:25 +0000 Received: by mail-ej1-x62d.google.com with SMTP id a640c23a62f3a-c15b1da6b82so105972566b.1 for ; Wed, 08 Jul 2026 09:58:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1783529902; cv=none; d=google.com; s=arc-20260327; b=HmqUwumjjoOva1xYowabLRidaRsg3OUo88t6nFN/ofsMJYtPtqYnzA/YJIX67gfj/3 cQGYMEmjnkj9HhoqYKU2RIXyFtP2sio/Ws8S7ie9ycPuOJjcJ2/TLdoJ6FEl9165dSNw MUpfDAi/xjZOWDW8oqjvYfaBXXomNcGSFaJH9eaviSYfyCz/uryZ1jzvXjdLEwpFXWIO jeP2D5221/To4yvNHAvOzp+Zgr4n0UKQFtzCd+s0iZBOEgEhHr9E4DAPLrW5Xn6GCoBf B946eWIZ8M2sgIm1ZmIsYpGmOtGllttzNDvAPwVDW5Cv6Y8b4xs14Q2RgQyTuPdAAY4o oRcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=AutziIhlFrXBcyMfJHpmr2CB9CQY6yfuLjY53DH92g4=; fh=1NuSWQODtMWkBcED+KXIePMmiumTqz8zn/hkTQJKP40=; b=B/UWqVGESJAcVJgNAIgkotzraKfvJzZn/dyQ2DdimohZ4DJ/Z2UZrh6RmTM+wEmak4 pbtFkbHHauvOaY1Y+qGLLxzdIzfrq6TaOxCHnTR+nKyoHcRwMlKuFCX8mfDB4j+5C2j6 Qdttj9RNBdy/9fdpyJJzaPwtKWBCsEzkXdcoWPsqy5bka8CE1SRedz9iQ8yeiMt9bTCB bS4PfgLT367g/JM5xoOzX9QqYx2S6ruLYQIiFbpkl9AA1yArOWBmWzPTzUvGXtZRt9dh 1H9Hyv6t4ANvaDl12/j92ZY6W/c25x93f+vSDLZSOaozY6gouFbDCWR16P4n3ExB3nOI ILHw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783529902; x=1784134702; darn=lists.postgresql.org; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:from:to:cc:subject:date:message-id:reply-to :content-type; bh=AutziIhlFrXBcyMfJHpmr2CB9CQY6yfuLjY53DH92g4=; b=KV4oGgiLa47rXsDgRa6tUNZBTDIBncAZvtN84fOhoSOvEL0gcYDocBeoWmSsJ6khB0 LOVir+X0IsHbMtUgLGZtk5RCWK8QcVfzp9JY6J8tPuSwuYtWfmQBsALkbSR86iFKrArh i71HU1Za0nOI5nztNByNj93vNdY7sXf+bx5uFHfl7ugvHNbXLn+oN8EsS2Pa1TqPW6vT N4EOAdQQ7NQbPRCQW2rFUyZFk1g95j2lizxUxfg7fZ1aGoLU4lBE0eIUFHHYLGP4pO4u MP5ulyGHY5BvsyHVcBZpsprcLp8hIG2lFJ/GdXkvRVZ+XVRlFKPMyhENhxEWTYoX+Q3N 6mdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783529902; x=1784134702; h=content-type:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to:content-type; bh=AutziIhlFrXBcyMfJHpmr2CB9CQY6yfuLjY53DH92g4=; b=KLYe6KEb0jAFtHqKtEulOk06QnGOv213TM/axrBAym19r7QAGkBCSl4dODAYoR+mYB HITUH9Zhin8wy4qLRV1TAt24MhRiQrJ1IwdfvGwRYX/yhIBN/8iIqFx3A84UXrHjzZN+ shlXzpAIj8GWHMnO7+uuUxjrtJhlsQkyD0HMlu00LIjEItYWyNPlSQgMzdFXUlRtq/LH bcC9oXZj2j6yeAMMlU5MPPJL/H5hqlbyqFgzCZjh/Fko6lZNOmNdQyqfqof+6tY2PfJ5 gmU9ng7eQLqN5k+V2XU3Ef3wMY7TSH08CkYXEgsBZ9jS+FSK5PNHbOp3oLDf5sXH7nXC hBwg== X-Forwarded-Encrypted: i=1; AHgh+Rqaahta6UngZthJl++bf8VVOB6J6y5+LN+uRiAO7mDaLB9xraI4/VcOYjqUwwWfXDV8RyMCb9dCbCFDM+TO@lists.postgresql.org X-Gm-Message-State: AOJu0YwpIJcQ/40SSc+OHiHd6ItJAC54zkwZE4+h8veZfH8DpkwWQSeV EfGSVzshu+lmM3ZFiXdJQdpZouLtEbCiB6MDiRYJTGhRfqgyVWlzttWZllWw8jS7KQll9+bIMDe kv+vtsQjEnXLxlbMDHnmFU/SgNH8VvYU= X-Gm-Gg: AfdE7ckmBlcryB1cZQ1kTm0z37tKrFjavA7zxt9+JEzj2KKwoDDTmFTm2bOU3UJPohs l5umVu7J6P7xs3TqJMQkQ/XSFnpa73VeijXMolsizBVdtK8PTkRAYc4Efi0FK5cCv1yQJCBu4a8 JzL8AhvNE8hkcJSAP6q2ONnbjisSlrIJiscLkbSCrGBIcv2L3eP28IbkUJHz6Sg7TRlsj+QUq4W 60AAWVF0j0W1xc+QUENjftk0vNhwsy9SARTp24fULn/gMjis8NZ2YXVszvE/ZjN3Ldll3gBXaJ0 flcZlwE9sbYLmsYCIPVM17cV/Jr50YTUMTyJIL0= X-Received: by 2002:a17:907:9620:b0:c15:b2f7:9857 with SMTP id a640c23a62f3a-c15ce162547mr152807766b.59.1783529902160; Wed, 08 Jul 2026 09:58:22 -0700 (PDT) MIME-Version: 1.0 References: <177997571870.313758.10720313850275742354.pgcf@coridan.postgresql.org> In-Reply-To: From: Rui Zhao Date: Thu, 9 Jul 2026 00:58:10 +0800 X-Gm-Features: AVVi8CeN1-zBFHIxt1F8ZhcnlYMicwoJW8PWiQ5-gRC0xwLiMqM5AMS8Tq3WHh0 Message-ID: Subject: Re: [PATCH] Add pg_get_policy_ddl() function to reconstruct CREATE POLICY statement To: Akshay Joshi Cc: solai v , Ilmar Y , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Akshay, I tested v15 on current master (57f93af36f): builds, make check passes, and basic reconstruction, pretty mode, NULL handling, and default-clause omission all look good. Two things. 1) Back in the v3/v4 discussion you decided the ON name should always be schema-qualified for safety (the pg_get_triggerdef_worker thread with jian he), and that pretty here only controls formatting, not schema (Phil's point). That reasoning didn't reach the USING / WITH CHECK expressions, though: object references inside them are still qualified only by the caller's search_path, so they can lose their schema and rebind to a different object when the DDL is replayed elsewhere: CREATE FUNCTION s1.f(int) RETURNS bool LANGUAGE sql AS 'SELECT $1 > 0'; CREATE FUNCTION s2.f(int) RETURNS bool LANGUAGE sql AS 'SELECT $1 < 0'; CREATE POLICY pf ON t2 USING (s1.f(a)); SET search_path = public, s1; SELECT ddl FROM pg_get_policy_ddl('t2', 'pf') AS ddl; -- CREATE POLICY pf ON public.t2 USING (f(a)); -- s1. dropped SET search_path = public, s2; CREATE POLICY pf ON t2 USING (f(a)); -- now s2.f, opposite meaning So within one statement the ON clause is always qualified but the expression body isn't -- and as Phil noted, the pretty flag can't fix this, since pg_get_expr qualifies by search_path visibility regardless of pretty. Worth settling the contract, given where this function sits. The old pg_get_viewdef / ruledef / indexdef functions are search_path-aware and leave qualification to the caller (pg_dump sets search_path empty around them so the output is portable). The new pg_get_*_ddl functions committed so far (role, database, tablespace) are on global, schemaless objects, so the question never came up -- pg_get_policy_ddl is the first of the family whose output embeds schema-qualifiable references. Your own pg_get_table_ddl patch already hit this and deparses under a controlled search_path (narrowed to pg_catalog), so the most consistent fix is to do the same here (NewGUCNestLevel + set_config); an empty search_path already yields the fully-qualified s1.f(a). If instead the intent is to follow the caller's search_path, that's fine too, but it should be documented (and SET search_path = '' noted as the way to get portable DDL). 2) The doc calls the second parameter policy_name, but the actual argument is policyname, so the documented named-argument call fails: SELECT * FROM pg_get_policy_ddl("table" => 't'::regclass, policy_name => 'p_all'); ERROR: function pg_get_policy_ddl(table => regclass, policy_name => unknown) does not exist Regards, Rui