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.94.2) (envelope-from ) id 1rl7Vm-000BhO-6T for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Mar 2024 13:22:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rl7Vj-008w59-Qy for pgsql-hackers@arkaria.postgresql.org; Fri, 15 Mar 2024 13:22:12 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rl7Vj-008w3Z-Ep for pgsql-hackers@lists.postgresql.org; Fri, 15 Mar 2024 13:22:12 +0000 Received: from udcm-wwu2.uni-muenster.de ([128.176.118.28]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rl7Vf-004eLa-5q for pgsql-hackers@postgresql.org; Fri, 15 Mar 2024 13:22:10 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uni-muenster.de; i=@uni-muenster.de; q=dns/txt; s=uniout; t=1710508929; x=1742044929; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=4Eg8LHo45znr+MaOujMbdKmmeQmZCtVHzMqFOecEtrU=; b=CWsgmuDGiypE0QqQJ62dsjviVqQqUJLqmDMGvAEfrZY4LnQ4piELiT4j A7MU1avS9RB1BFlHF2218VjInEBrIjRk2WqH8l4VCDdj6fmmBWcv5PUaJ 1ebRd6U5TPPmhHLRo4wDGvYRmfGp4Q8/uaFe9Gm+CKs3Gtf9PR217YSLO +rzofm/QpQuTJ6dbO96VifN6DkPTtLSiwVXHG1F8irhcWaFaJ4T6TmnvN /R4lEDzB+RV/vWRKlaSYgu6dBL7SChfoGZRJvLqSfUvx9Uut/KVLNknR1 2EHBElXr/4VpE23h08xAoWz1WZEv+YdLUrRjN4P/Box4IFE/NgXJ7WLpB g==; X-CSE-ConnectionGUID: Y9MB+wFwSJa3O75i7rUgLw== X-CSE-MsgGUID: nCVwNSKSQ7SJPdaNBjra+A== X-IronPort-AV: E=Sophos;i="6.07,128,1708383600"; d="scan'208";a="312062589" Received: from secmail.uni-muenster.de ([128.176.118.4]) by UDCM-RELAY2.UNI-MUENSTER.DE with ESMTP; 15 Mar 2024 14:22:04 +0100 Received: from [192.168.178.27] (dynamic-093-135-110-194.93.135.pool.telefonica.de [93.135.110.194]) by SECMAIL.UNI-MUENSTER.DE (Postfix) with ESMTPSA id A220520D9B47; Fri, 15 Mar 2024 14:22:01 +0100 (CET) Message-ID: Date: Fri, 15 Mar 2024 14:21:59 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Adding comments to help understand psql hidden queries To: David Christensen , Greg Sabino Mullane Cc: pgsql-hackers References: Content-Language: en-US From: Jim Jones In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Greg, hi David On 01.02.24 23:39, David Christensen wrote: > On Thu, Feb 1, 2024 at 4:34 PM Greg Sabino Mullane wrote: >> The use of the --echo-hidden flag in psql is used to show people the way psql performs its magic for its backslash commands. None of them has more magic than "\d relation", but it suffers from needing a lot of separate queries to gather all of the information it needs. Unfortunately, those queries can get overwhelming and hard to figure out which one does what, especially for those not already very familiar with the system catalogs. Attached is a patch to add a small SQL comment to the top of each SELECT query inside describeOneTableDetail. All other functions use a single query, and thus need no additional context. But "\d mytable" has the potential to run over a dozen SQL queries! The new format looks like this: >> >> /******** QUERY *********/ >> /* Get information about row-level policies */ >> SELECT pol.polname, pol.polpermissive, >> CASE WHEN pol.polroles = '{0}' THEN NULL ELSE pg_catalog.array_to_string(array(select rolname from pg_catalog.pg_roles where oid = any (pol.polroles) order by 1),',') END, >> pg_catalog.pg_get_expr(pol.polqual, pol.polrelid), >> pg_catalog.pg_get_expr(pol.polwithcheck, pol.polrelid), >> CASE pol.polcmd >> WHEN 'r' THEN 'SELECT' >> WHEN 'a' THEN 'INSERT' >> WHEN 'w' THEN 'UPDATE' >> WHEN 'd' THEN 'DELETE' >> END AS cmd >> FROM pg_catalog.pg_policy pol >> WHERE pol.polrelid = '134384' ORDER BY 1; >> /************************/ >> >> Cheers, >> Greg > Thanks, this looks like some helpful information. In the same vein, > I'm including a patch which adds information about the command that > generates the given query as well (atop your commit). This will > modify the query line to include the command itself: > > /******** QUERY (\dRs) *********/ > > Best, > > David Having this kind of information in each query would have saved me a lot of time in the past :) +1 There is a tiny little issue in the last patch (qualifiers): command.c:312:16: warning: assignment discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]   312 |         curcmd = cmd; Thanks -- Jim