\d is not showing global(normal) table info if we create temporarytable with same name as global table - Mailing list pgsql-hackers

From Mahendra Singh
Subject \d is not showing global(normal) table info if we create temporarytable with same name as global table
Date
Msg-id CAKYtNAqPky_rLzPWTLG76ePhT0mC-fZq-j35rmVuSAuPzfZAqQ@mail.gmail.com
Whole thread Raw
Responses Re: \d is not showing global(normal) table info if we createtemporary table with same name as global table  (Robert Haas <robertmhaas@gmail.com>)
List pgsql-hackers
Hi hackers,

While reading code and doing some testing, I found that if we create a temporary table with same name as we created a normal(global) table, then \d is showing only temporary table info.  I think, ideally we should display info of both the tables. Below is the example:

postgres=# create table t (a int);
CREATE TABLE
postgres=# create temporary table t (a int);
CREATE TABLE
postgres=# \d
          List of relations
  Schema   | Name | Type  |  Owner  
-----------+------+-------+----------
 pg_temp_2 | t    | table | mahendra
(1 row)


Expected behavior:
postgres=# \d
          List of relations
  Schema   | Name | Type  |  Owner  
-----------+------+-------+----------
 pg_temp_2 | t    | table | mahendra
 public    | t    | table | mahendra
(2 rows)


For me, It looks like a bug.

I debugged and found that due to below code, we are showing only temp table information.

        /*
         * If it is in the path, it might still not be visible; it could be
         * hidden by another relation of the same name earlier in the path. So
         * we must do a slow check for conflicting relations.
         */
        char       *relname = NameStr(relform->relname);
        ListCell   *l;

        visible = false;
        foreach(l, activeSearchPath)
        {
            Oid            namespaceId = lfirst_oid(l);

            if (namespaceId == relnamespace)
            {
                /* Found it first in path */
                visible = true;
                break;
            }
            if (OidIsValid(get_relname_relid(relname, namespaceId)))
            {
                /* Found something else first in path */
                break;
            }

        }

postgres=# select oid, relname , relnamespace , reltype from pg_class where relname = 't';
  oid  | relname | relnamespace | reltype
-------+---------+--------------+---------
 16384 | t       |         2200 |   16386
 16389 | t       |        16387 |   16391
(2 rows)

For able example, we have 3 namespaceId in the activeSearchPath list (16387->temporary_table, 11, 2200->noraml-table). As I can see that 16387 is the 1st oid in list, that is corresponds to temp table, we are displaying info of temp table but when we are checking visibility of normal table, then we exiting from list after comparing with 1st oid because 16387 is the 1st in list and that oid is valid.

If this is a bug, then please let me know. I will be happy to fix this.

Thanks and Regards
Mahendra Singh Thalor
EnterpriseDB: http://www.enterprisedb.com

pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: Clarifying/rationalizing Vars' varno/varattno/varnoold/varoattno
Next
From: David Fetter
Date:
Subject: Re: backup manifests