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 1qXYYy-006XDs-18 for pgsql-general@arkaria.postgresql.org; Sun, 20 Aug 2023 02:53:12 +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 1qXYYw-0039Mi-0R for pgsql-general@arkaria.postgresql.org; Sun, 20 Aug 2023 02:53:10 +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 1qXYYv-0039Ma-LR for pgsql-general@lists.postgresql.org; Sun, 20 Aug 2023 02:53:09 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qXYYs-00159U-NN for pgsql-general@postgresql.org; Sun, 20 Aug 2023 02:53:08 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1bddac1b7bfso14815185ad.0 for ; Sat, 19 Aug 2023 19:53:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692499985; x=1693104785; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=yIfCaOmqD3xX0ExcF60gA1zcS8JteGgKltNEhTlaM3s=; b=YIkEgtCkm318BpmPPUEaetUDhum/BkKRVYV0OgnS38B7NUvBUNSBjExVbVqCU/hxot fKOSrC+hGqNVnEd4CRwktUmuAORS8P0AG99MxRFrnlSbsGlPo9rriINEjXuvF+ujI0+Y QkLNr9ckM0LdF/Toore6/lApjHhE+ibgIVsV3xOM333VPchtOJWh8IBs8OO+hnw/fI3q qHj4d5HXNxGcQj7RpTIpT/0vbT9PLFp9D8v62MtT+5mcj26yoUlk8SQslfkPPynb84kX WOFUMWlN4tYfDMe0GhoV1vSx2LMGtwmZBixXmh2kwE1C5xr1ebB0/itVPSJa8gpLt8Wz UXFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692499985; x=1693104785; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=yIfCaOmqD3xX0ExcF60gA1zcS8JteGgKltNEhTlaM3s=; b=RUVHWMoxefw4cqXLB1FPDLTfmSiZtH9YxOvOkcXhydWlNWMl6NDm8bjlJEh5hA1hh6 UcRhKsS0BfBDix1pSzYSuAjxmiWdAwumXxftC6LrK+t/00kIdzAKH01rrjBTJje1jHI+ CXA1pPwFS8CD8aUP4JJlpXVyC6f1SbE5L5ENvrHmJ1ATWbHcusIQv90FTr8RO0Y7xCvx MT3cwR3+KT4q13MYLBCarkeuy+9wWpeW1cEa/xwnkHdl9CbDznTuSoNHqBCKFZ87dvQI rG1yyn3BC+WsHA/aQoPgoKViaLDEfZmkP8qp9OrwBBqDJeTWq7Jt1wNrSe4RgVValMOC 5kZA== X-Gm-Message-State: AOJu0YwjyvUudEUi2Fx+Q2odzIoOl03aTD+7+Lpm6BjEUdDLfIPwd58O 52Rs0pFs3Un7J9GanpWWSJs= X-Google-Smtp-Source: AGHT+IGSG0XvBw9JHTmJX2Vl4f2JIujV6kFvHD293aIwYpgddF+s0bnMSLcjvknT4MDh+0IiJs005w== X-Received: by 2002:a17:902:ecc2:b0:1bb:c7bc:ceb8 with SMTP id a2-20020a170902ecc200b001bbc7bcceb8mr5198555plh.22.1692499985375; Sat, 19 Aug 2023 19:53:05 -0700 (PDT) Received: from jrouhaud (2001-b011-1006-5495-d450-20dd-e85a-fad3.dynamic-ip6.hinet.net. [2001:b011:1006:5495:d450:20dd:e85a:fad3]) by smtp.gmail.com with ESMTPSA id e15-20020a17090301cf00b001b8a2edab6asm4293667plh.244.2023.08.19.19.53.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 19 Aug 2023 19:53:04 -0700 (PDT) Date: Sun, 20 Aug 2023 10:53:01 +0800 From: Julien Rouhaud To: Pierre Forstmann Cc: pgsql-general@postgresql.org Subject: Re: how to use query_tree_walker to get all relations used in a query Message-ID: <20230820025301.4sre5chx6jelc2ry@jrouhaud> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On Sat, Aug 19, 2023 at 03:26:06PM +0200, Pierre Forstmann wrote: > > I am trying to get the list of all relations used in a SELECT query using > the post parse analyze hook. > > I can get all relations from top level FROM clause but I cannot get them > for a simple subquery like: > > select * from t1 where x1=(select max(x2) from t2); You're missing at least the sublinks. Since you're looking at AcquireRewriteLocks() as an example, look at how acquireLocksOnSubLinks() is being called. In general, if you're not sure of how some constructs are represented in a Query the easiest way to get a good overview is to simply do something like static void pgds_build_rel_array(Query *query) { [...] elog(WARNING, "found %s", nodeToString(query)); You will get a textual representation of the query (or any specific node if needed) in the client and server log, so you can easily look for a given relid, Var or anything and identify what you were missing and adapt your walker function.