Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0w0x-0002Ke-Ia for pgsql-general@arkaria.postgresql.org; Fri, 14 Sep 2018 21:52:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g0w0v-0005Id-UX for pgsql-general@arkaria.postgresql.org; Fri, 14 Sep 2018 21:52:33 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0w0v-0005Hb-JH for pgsql-general@lists.postgresql.org; Fri, 14 Sep 2018 21:52:33 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g0w0s-0005hY-Ul for pgsql-general@lists.postgresql.org; Fri, 14 Sep 2018 21:52:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8ELqS1Z002047; Fri, 14 Sep 2018 17:52:28 -0400 From: Tom Lane To: "Sebastian P. Luque" cc: pgsql-general@lists.postgresql.org Subject: Re: column information from view In-reply-to: <87in373gpd.fsf@gmail.com> References: <87in373gpd.fsf@gmail.com> Comments: In-reply-to "Sebastian P. Luque" message dated "Fri, 14 Sep 2018 16:35:42 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2045.1536961948.1@sss.pgh.pa.us> Date: Fri, 14 Sep 2018 17:52:28 -0400 Message-ID: <2046.1536961948@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Sebastian P. Luque" writes: > Here's my attempt at listing the temporary view's columns and respective > descriptions: > SELECT cols.ordinal_position, cols.column_name, > col_description(cl.oid, cols.ordinal_position::INT) > FROM pg_class cl, information_schema.columns cols > WHERE cols.table_catalog='dbname' AND cols.table_schema='some_schema' AND > cols.table_name = 'persistent_view' AND cols.table_name = cl.relname > ORDER BY cols.ordinal_position::INT; > The problem, of course, is that it lists columns from the persistent > view, instead of the subset of them in the temporary view. Is there a > better way to do that? Hopefully this makes sense. Umm ... why are you doing cols.table_name = 'persistent_view' and not cols.table_name = 'temporary_view' ? It seems rather odd to write a query that involves both pg_class and the information_schema --- by involving pg_class, you've already given up hope of making the query portable to non-PG DBMSes. Personally, I'd probably write it something like this: select pa.attnum, pa.attname, col_description(pa.attrelid, pa.attnum) from pg_attribute pa, pg_attribute ta where pa.attrelid = 'persistent_view'::regclass and ta.attrelid = 'temporary_view'::regclass and pa.attname = ta.attname order by pa.attnum; If you were dealing with tables, it'd also be wise to add "pa.attnum > 0 and not pa.attisdropped", but I think neither of those conditions can fail for views. regards, tom lane