Thread: [PATCH] docs: Document the automatically generated names for indices

[PATCH] docs: Document the automatically generated names for indices

From
jadel@mercury.com
Date:
Previously, the automatically generated names were entirely undocumented. That
is not a very good state of affairs: although it's possibly inconvenient to
rigidly specify what it is since it's an implementation detail, these names are
user-visible, and it would be good to have documentation at all.





[PATCH] docs: Document the automatically generated names for indices

From
jadel@mercury.com
Date:
From: Jade Lovelace <jadel@mercury.com>

I have intentionally been careful to not guarantee that the
automatically generated name *is* what's documented, but instead give
the general idea.

Knowing the format is useful for people writing migrations/ORM tools
which need to name their unique indices and may wish to use the
automatic naming system.

Signed-off-by: Jade Lovelace <jadel@mercury.com>
---
 doc/src/sgml/ref/create_index.sgml |  9 +++++++--
 doc/src/sgml/ref/create_table.sgml | 30 ++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/doc/src/sgml/ref/create_index.sgml b/doc/src/sgml/ref/create_index.sgml
index a5bac9f..7354267 100644
--- a/doc/src/sgml/ref/create_index.sgml
+++ b/doc/src/sgml/ref/create_index.sgml
@@ -206,9 +206,14 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] [ [ IF NOT EXISTS ] <replaceable class=
         table.  The name of the index must be distinct from the name of any
         other relation (table, sequence, index, view, materialized view, or
         foreign table) in that schema.
+       </para>
+       <para>
         If the name is omitted, <productname>PostgreSQL</productname> chooses a
-        suitable name based on the parent table's name and the indexed column
-        name(s).
+        suitable name based on the parent table's name and the indexed
+        column name(s). Generally, teh generated name will be something of the
+        shape <literal>tablename_columnname_idx</literal>, but this may vary in
+        the event of long table names, where it is truncated, or if the index
+        already exists, where a number is appended.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml
index 6bbf15e..bdabd86 100644
--- a/doc/src/sgml/ref/create_table.sgml
+++ b/doc/src/sgml/ref/create_table.sgml
@@ -796,6 +796,36 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       (Double-quotes are needed to specify constraint names that contain spaces.)
       If a constraint name is not specified, the system generates a name.
      </para>
+     <para>
+      Generally, the generated name will be something of the shape
+      <literal>tablename_columnname_constrainttype</literal>, but this may vary in the
+      event of long table names, where it is truncated, or if the index
+      already exists, where a number is appended.
+      <literal>constrainttype</literal> is one of the following:
+
+      <itemizedlist>
+       <listitem>
+        <para><literal>pkey</literal> in the case of primary keys</para>
+       </listitem>
+       <listitem>
+        <para>
+         <literal>excl</literal> in the case of
+         <link linkend="sql-createtable-exclude">
+          <literal>EXCLUDE</literal>
+         </link> constraints.
+        </para>
+       </listitem>
+       <listitem>
+        <para>
+         <literal>key</literal> in the case of
+         <link linkend="sql-createtrigger"><firstterm>constraint triggers</firstterm></link>.
+        </para>
+       </listitem>
+       <listitem>
+        <para><literal>idx</literal> in other cases.</para>
+       </listitem>
+      </itemizedlist>
+     </para>
     </listitem>
    </varlistentry>
 
-- 
2.37.1




Re: [PATCH] docs: Document the automatically generated names for indices

From
Bruce Momjian
Date:
On Thu, Sep  1, 2022 at 05:47:11PM -0700, jadel@mercury.com wrote:
> From: Jade Lovelace <jadel@mercury.com>
> 
> I have intentionally been careful to not guarantee that the
> automatically generated name *is* what's documented, but instead give
> the general idea.
> 
> Knowing the format is useful for people writing migrations/ORM tools
> which need to name their unique indices and may wish to use the
> automatic naming system.

Uh, I always that if people didn't want to specify an index name, that
they didn't care.  I think there are enough concurrency issues listed
below that I don't see the value of documenting this.  If they really
care, they can look at the source code.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Indecision is a decision.  Inaction is an action.  Mark Batterson