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 1q4hAX-0006fa-TZ for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Jun 2023 12:12: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 1q4hAW-00028j-Gi for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Jun 2023 12:12: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 1q4hAU-000288-Pi for pgsql-hackers@lists.postgresql.org; Thu, 01 Jun 2023 12:12:40 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q4hAN-0034Rw-1w for pgsql-hackers@postgresql.org; Thu, 01 Jun 2023 12:12:37 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 116AE3200931; Thu, 1 Jun 2023 08:12:25 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Thu, 01 Jun 2023 08:12:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :message-id:mime-version:reply-to:sender:subject:subject:to:to; s=fm2; t=1685621545; x=1685707945; bh=iWxpl48J1GdKCvursVStvcTdL qQ+cBy8aKFjKkGwFcs=; b=YdCr2X01l+/rrvLuGnmrGiOT603itlJ7+o/RKBqHJ fymzP5kzNCXSeQ7XFTB3ytKOVf/O7yncdJ1VWJEn1oHsrxugKJbSM608E1BYdyT3 wHGX9R9b3aOAZrqVFRnjcNAeJNtCGWn89QyGJD8UhJSP6IC28Crtv1ETRxn7Yl1q D/wtnBkV83V6ocgciMJTQBPbzIxvWh+2+bzZAZu8F9VljzFSI8RQXUyvHIn4xhTM FwHpmOQrulpxzWTYbrb3MdMYNvcIManak7UDYaKhYaJvGPPmBsUkXlTj4/LNbZ99 QUPmi2zPlsh8VLzsVATT13aqP8gnLyEevcwZo9u9E86dA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1685621545; x=1685707945; bh=iWxpl48J1GdKCvursVStvcTdLqQ+cBy8aKF jKkGwFcs=; b=XNb1mu7x2bu2UfdACViVaau9Gs6AqfMwaAwxfUTrZGNrLAt+gaV cEIJRF9c9oMq1Ufsc0N6lPEtl4Vh14/Lpp5HZycrnzZZmyC2NZ2Lc6YaFdxILUkA YiEBk/iU7z9wPJv6CEfY+IoFfVhzO515BVYbJGfFSCTLfLhqGYlE3tr0YB/7N1CO 9DJynoKk9YO/RU4T35UhFIy2UJwK20ZAgbkxRFRDuESdbtC4R6DPLoyl6u2Dy4cM 242On4dj/RSolObOSuOLCmHdqTY0c1NYyKZ6zc6c5J1AsyzhxL1E5YjWOjOc5IdF KpCH8U3vFojgtq0VpWo3EfqI/Xl6ho+w2fw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeeluddggeelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkfgggtgesmhdtreertderjeenucfhrhhomhepffgrghhfihhn nhcukfhlmhgrrhhiucforghnnhhsnohkvghruceoihhlmhgrrhhisehilhhmrghrihdroh hrgheqnecuggftrfgrthhtvghrnheptdegjeektddvgeevffefteetfeehudeigeejueeg gefgudehgfduffdttdeugeejnecuffhomhgrihhnpehpohhsthhgrhdrvghsnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepihhlmhgrrhhisehi lhhmrghrihdrohhrgh X-ME-Proxy: Feedback-ID: i1ff147bf:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 1 Jun 2023 08:12:24 -0400 (EDT) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: PostgreSQL Hackers Cc: Peter Eisentraut Subject: [PATCH] Using named captures in Catalog::ParseHeader() Date: Thu, 01 Jun 2023 13:12:22 +0100 Message-ID: <87y1l3s7o9.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain Hi Hackers, Peter's patch set for autogenerating syscache info (https://postgr.es/m/75ae5875-3abc-dafc-8aec-73247ed41cde%40eisentraut.org) touched on one of my least favourite parts of Catalog.pm: the parenthesis-counting nightmare that is the parsing of catalog header directives. However, now that we require Perl 5.14, we can use the named capture feature (introduced in Perl 5.10) to make that a lot clearer, as in the attached patch. While I was rewriting the regexes I noticed that they were inconsistent about whether they accepted whitespace in the parameter lists, so I took the liberty to make them consistently allow whitespace after the opening paren and the commas, which is what most of them already did. I've verified that the generated postgres.bki is identical to before, and all tests pass. - ilmari --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Use-named-captures-in-Catalog-ParseHeader.patch From dd7ed959c93438045f9ff270feab33bb7b965c29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Wed, 31 May 2023 14:00:58 +0100 Subject: [PATCH] Use named captures in Catalog::ParseHeader() Now that we require Perl 5.14 we can use named captures and the %+ hash instead of having to count parenthesis groups manually. --- src/backend/catalog/Catalog.pm | 95 ++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 40 deletions(-) diff --git a/src/backend/catalog/Catalog.pm b/src/backend/catalog/Catalog.pm index 84aaeb002a..b15f513183 100644 --- a/src/backend/catalog/Catalog.pm +++ b/src/backend/catalog/Catalog.pm @@ -91,73 +91,88 @@ sub ParseHeader # Push the data into the appropriate data structure. # Caution: when adding new recognized OID-defining macros, # also update src/include/catalog/renumber_oids.pl. - if (/^DECLARE_TOAST\(\s*(\w+),\s*(\d+),\s*(\d+)\)/) - { - push @{ $catalog{toasting} }, - { parent_table => $1, toast_oid => $2, toast_index_oid => $3 }; - } - elsif ( - /^DECLARE_TOAST_WITH_MACRO\(\s*(\w+),\s*(\d+),\s*(\d+),\s*(\w+),\s*(\w+)\)/ + if (/^DECLARE_TOAST\(\s* + (?\w+),\s* + (?\d+),\s* + (?\d+)\s* + \)/x ) { - push @{ $catalog{toasting} }, - { - parent_table => $1, - toast_oid => $2, - toast_index_oid => $3, - toast_oid_macro => $4, - toast_index_oid_macro => $5 - }; + push @{ $catalog{toasting} }, {%+}; } elsif ( - /^DECLARE_(UNIQUE_)?INDEX(_PKEY)?\(\s*(\w+),\s*(\d+),\s*(\w+),\s*(.+)\)/ + /^DECLARE_TOAST_WITH_MACRO\(\s* + (?\w+),\s* + (?\d+),\s* + (?\d+),\s* + (?\w+),\s* + (?\w+)\s* + \)/x + ) + { + push @{ $catalog{toasting} }, {%+}; + } + elsif ( + /^DECLARE_(UNIQUE_)?INDEX(_PKEY)?\(\s* + (?\w+),\s* + (?\d+),\s* + (?\w+),\s* + (?.+)\s* + \)/x ) { push @{ $catalog{indexing} }, { is_unique => $1 ? 1 : 0, is_pkey => $2 ? 1 : 0, - index_name => $3, - index_oid => $4, - index_oid_macro => $5, - index_decl => $6 - }; - } - elsif (/^DECLARE_OID_DEFINING_MACRO\(\s*(\w+),\s*(\d+)\)/) - { - push @{ $catalog{other_oids} }, - { - other_name => $1, - other_oid => $2 + %+, }; } elsif ( - /^DECLARE_(ARRAY_)?FOREIGN_KEY(_OPT)?\(\s*\(([^)]+)\),\s*(\w+),\s*\(([^)]+)\)\)/ + /^DECLARE_OID_DEFINING_MACRO\(\s* + (?\w+),\s* + (?\d+)\s* + \)/x + ) + { + push @{ $catalog{other_oids} }, {%+}; + } + elsif ( + /^DECLARE_(ARRAY_)?FOREIGN_KEY(_OPT)?\(\s* + \((?[^)]+)\),\s* + (?\w+),\s* + \((?[^)]+)\)\s* + \)/x ) { push @{ $catalog{foreign_keys} }, { is_array => $1 ? 1 : 0, is_opt => $2 ? 1 : 0, - fk_cols => $3, - pk_table => $4, - pk_cols => $5 + %+, }; } - elsif (/^CATALOG\((\w+),(\d+),(\w+)\)/) + elsif ( + /^CATALOG\(\s* + (?\w+),\s* + (?\d+),\s* + (?\w+)\s* + \)/x + ) { - $catalog{catname} = $1; - $catalog{relation_oid} = $2; - $catalog{relation_oid_macro} = $3; + @catalog{ keys %+ } = values %+; $catalog{bootstrap} = /BKI_BOOTSTRAP/ ? ' bootstrap' : ''; $catalog{shared_relation} = /BKI_SHARED_RELATION/ ? ' shared_relation' : ''; - if (/BKI_ROWTYPE_OID\((\d+),(\w+)\)/) + if (/BKI_ROWTYPE_OID\(\s* + (?\d+),\s* + (?\w+)\s* + \)/x + ) { - $catalog{rowtype_oid} = $1; - $catalog{rowtype_oid_clause} = " rowtype_oid $1"; - $catalog{rowtype_oid_macro} = $2; + @catalog{ keys %+ } = values %+; + $catalog{rowtype_oid_clause} = " rowtype_oid $+{rowtype_oid}"; } else { -- 2.39.2 --=-=-=--