I also noticed that \d on an index doesn't warn about the invisible state
whereas \d on a table does:

[local]:5444 postgres@postgres=# SELECT indexrelid::regclass,
indisvalid, indisvisible FROM pg_index WHERE indexrelid =
'repli_pkey'::regclass \gx
-[ RECORD 1 ]+-----------
indexrelid   | repli_pkey
indisvalid   | f
indisvisible | f

[local]:5444 postgres@postgres=# \d repli_pkey
      Index "public.repli_pkey"
 Column |  Type   | Key? | Definition
--------+---------+------+------------
 i      | integer | yes  | i
primary key, btree, for table "public.repli", invalid

[local]:5444 postgres@postgres=# \d repli
               Table "public.repli"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 i      | integer |           | not null |
 t      | text    |           |          |
Indexes:
    "repli_pkey" PRIMARY KEY, btree (i) INVISIBLE INVALID
Publications:
    "pub"

The attached patch adds the flag.

[local]:5444 postgres@postgres=# \d repli_pkey
      Index "public.repli_pkey"
 Column |  Type   | Key? | Definition
--------+---------+------+------------
 i      | integer | yes  | i
primary key, btree, for table "public.repli", invalid, invisible
From bf3f11e5e88a30a9c1affd9678dadec9bc236351 Mon Sep 17 00:00:00 2001
From: benoit <benoit.lobr...@dalibo.com>
Date: Fri, 24 Jan 2025 16:12:45 +0100
Subject: [PATCH 2/3] Add the invisible tag for indexes in \d

---
 src/bin/psql/describe.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 2ef99971ac0..5d1acbd149d 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -2318,6 +2318,11 @@ describeOneTableDetails(const char *schemaname,
 		else
 			appendPQExpBufferStr(&buf, "false AS indnullsnotdistinct,\n");
 
+		if (pset.sversion >= 180000)
+			appendPQExpBufferStr(&buf, "i.indisvisible,\n");
+		else
+			appendPQExpBufferStr(&buf, "true AS indisvisible,\n");
+
 		appendPQExpBuffer(&buf, "  a.amname, c2.relname, "
 						  "pg_catalog.pg_get_expr(i.indpred, i.indrelid, true)\n"
 						  "FROM pg_catalog.pg_index i, pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_am a\n"
@@ -2343,9 +2348,10 @@ describeOneTableDetails(const char *schemaname,
 			char	   *deferred = PQgetvalue(result, 0, 5);
 			char	   *indisreplident = PQgetvalue(result, 0, 6);
 			char	   *indnullsnotdistinct = PQgetvalue(result, 0, 7);
-			char	   *indamname = PQgetvalue(result, 0, 8);
-			char	   *indtable = PQgetvalue(result, 0, 9);
-			char	   *indpred = PQgetvalue(result, 0, 10);
+			char	   *indisvisible = PQgetvalue(result, 0, 8);
+			char	   *indamname = PQgetvalue(result, 0, 9);
+			char	   *indtable = PQgetvalue(result, 0, 10);
+			char	   *indpred = PQgetvalue(result, 0, 11);
 
 			if (strcmp(indisprimary, "t") == 0)
 				printfPQExpBuffer(&tmpbuf, _("primary key, "));
@@ -2382,6 +2388,9 @@ describeOneTableDetails(const char *schemaname,
 			if (strcmp(indisreplident, "t") == 0)
 				appendPQExpBufferStr(&tmpbuf, _(", replica identity"));
 
+			if (strcmp(indisvisible, "t") != 0)
+				appendPQExpBufferStr(&tmpbuf, _(", invisible"));
+
 			printTableAddFooter(&cont, tmpbuf.data);
 
 			/*
-- 
2.48.1

Reply via email to