postgresql/contrib/seg
Tom Lane 181b6185c7 Improve the names generated for indexes on expressions.
If the user doesn't specify a name for an index, it's generated
based on the names chosen for the index columns (which the user
has no direct control over).  For index columns that are just
columns of the base relation, the index column name is the same as
the base column name; but for index columns that are expressions,
it's less clear what to do.  Up to now, what we have done is
equivalent to the heuristics used to choose SELECT output column
names, except that we fall back to "expr" not "?column?" in the
numerous cases that FigureColname doesn't know what to do with.
This is not tremendously helpful.  More, it frequently leads to
collisions of generated index names, which we can handle but
only at the cost of user confusion; also there's some risk of
concurrent index creations trying to use the same name.
Let's try to do better.

Messing with the FigureColname heuristics would have a very
large blast radius, since that affects the column headings
that applications see.  That doesn't seem wise, but fortunately
SQL queries are seldom directly concerned with index names.
So we should be able to change the index-name generation rules
as long as we decouple them from FigureColname.

The method used in this patch is to dig through the expression,
extract the names of Vars, the string representations of Consts,
and the names of functions, and run those together with underscores
between.  Other expression node types are ignored but descended
through.  We could work harder by handling more node types, but
it seems like this is likely to be sufficient to arrive at unique
index names in many cases.

Notably, this rule ignores the names of operators, for example
both "a + b" and "a * b" will be rendered as "a_b".  This choice
was made to reduce the probability of having to double-quote
the index name.

I've also chosen to strip Const representations down to only
alphanumeric characters (plus non-ASCII characters, which our
parser treats as alphabetic anyway).  So for example "x + 1.0"
would be represented as "x_10".  This likewise avoids possible
quoting problems.  I also considered limiting how many characters
we'd take from each Const, but didn't do that here.

We might tweak these rules some more after we get some experience
with this patch.  It's being committed at the start of a
development cycle to provide as much time as possible to gather
feedback.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Discussion: https://postgr.es/m/876799.1757987810@sss.pgh.pa.us
Discussion: https://postgr.es/m/18959-f63b53b864bb1417@postgresql.org
2026-07-01 11:33:52 -04:00
..
data Modify the float4 datatype to be pass-by-val. Along the way, remove the last 2008-04-18 18:43:09 +00:00
expected Improve the names generated for indexes on expressions. 2026-07-01 11:33:52 -04:00
sql seg: Fix seg_out() to preserve the upper boundary's certainty indicator 2026-06-11 12:33:48 +03:00
.gitignore Build all Flex files standalone 2022-09-04 12:09:01 +07:00
Makefile Use generateClonedIndexStmt to propagate CREATE INDEX to partitions. 2024-10-05 14:46:44 -04:00
meson.build Update copyright for 2026 2026-01-01 13:24:10 -05:00
seg--1.0--1.1.sql Make contrib modules' installation scripts more secure. 2020-08-10 10:44:42 -04:00
seg--1.1--1.2.sql Update contrib/seg for new scalarlesel/scalargesel selectivity functions. 2017-09-13 11:54:55 -04:00
seg--1.1.sql Update extensions with GIN/GIST support for parallel query. 2016-06-14 13:34:37 -04:00
seg--1.2--1.3.sql Make contrib modules' installation scripts more secure. 2020-08-10 10:44:42 -04:00
seg--1.3--1.4.sql Remove deprecated containment operators for contrib types. 2021-03-05 10:45:41 -05:00
seg-validate.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00
seg.c seg: Fix seg_out() to preserve the upper boundary's certainty indicator 2026-06-11 12:33:48 +03:00
seg.control Remove deprecated containment operators for contrib types. 2021-03-05 10:45:41 -05:00
segdata.h Allow redeclaration of typedef yyscan_t 2025-09-12 08:16:00 +02:00
segparse.y seg: pure parser and reentrant scanner 2024-12-18 08:47:53 +01:00
segscan.l Partial pgindent of .l and .y files 2024-12-25 17:55:42 +01:00
sort-segments.pl Update copyright for 2026 2026-01-01 13:24:10 -05:00