postgresql/src/backend/commands
Tom Lane 5861b1f343 Use SnapshotDirty when checking for conflicting index names.
While choosing an autogenerated name for an index, look for
pre-existing relations using a SnapshotDirty snapshot, instead of the
previous behavior that considered only committed-good pg_class rows.
This allows us to detect and avoid conflicts against indexes that are
still being built.

It's still possible to fail due to a race condition, but the window
is now just the amount of time that it takes DefineIndex to validate
all its parameters, call smgrcreate(), and enter the index's pg_class
row.  Formerly the race window covered the entire time needed to
create and fill an index, which could be very long if the table is
large.  Worse, if the conflicting index creation is part of a larger
transaction, it wouldn't be visible till COMMIT.

So this isn't a complete solution, but it should greatly ameliorate
the problem, and the patch is simple enough to be back-patchable.

It might at some point be useful to do the same for pg_constraint
entries (cf. ChooseConstraintName, ConstraintNameExists, and related
functions).  However, in the absence of field complaints, I'll leave
that alone for now.  The relation-name test should be good enough for
index-based constraints, while foreign-key constraints seem to be okay
since they require exclusive locks to create.

Bug: #18959
Reported-by: Maximilian Chrzan <maximilian.chrzan@here.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Dilip Kumar <dilipbalaut@gmail.com>
Discussion: https://postgr.es/m/18959-f63b53b864bb1417@postgresql.org
Backpatch-through: 13
2025-06-20 13:41:11 -04:00
..
aggregatecmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
alter.c Avoid invalidating all RelationSyncCache entries on publication rename. 2025-03-13 09:16:33 +05:30
amcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
analyze.c read_stream: Introduce and use optional batchmode support 2025-03-30 18:36:41 -04:00
async.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
cluster.c Add relallfrozen to pg_class 2025-03-03 11:18:05 -05:00
collationcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
comment.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
constraint.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
conversioncmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
copy.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
copyfrom.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
copyfromparse.c Revert a few small patches that were intended for version 19. 2025-06-11 15:10:12 -07:00
copyto.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
createas.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
dbcommands.c Improve CREATE DATABASE error message for invalid libc locale. 2025-06-06 15:28:51 -07:00
define.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
discard.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
dropcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
event_trigger.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
explain.c Change internal queryid type from uint64 to int64 2025-05-30 22:59:39 +12:00
explain_dr.c Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
explain_format.c Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
explain_state.c Add an additional hook for EXPLAIN option validation. 2025-03-20 13:47:55 -04:00
extension.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
foreigncmds.c Disallow "=" in names of reloptions and foreign-data options. 2025-06-02 15:22:44 -04:00
functioncmds.c Fix erroneous construction of functions' dependencies on transforms. 2025-04-07 13:31:37 -04:00
indexcmds.c Use SnapshotDirty when checking for conflicting index names. 2025-06-20 13:41:11 -04:00
lockcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
Makefile Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
matview.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
meson.build Make it possible for loadable modules to add EXPLAIN options. 2025-03-18 08:41:12 -04:00
opclasscmds.c Add nbtree skip scan optimization. 2025-04-04 12:27:04 -04:00
operatorcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
policy.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
portalcmds.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
prepare.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
proclang.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
publicationcmds.c Use correct variable name in publicationcmds.c. 2025-03-18 14:06:51 +05:30
schemacmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
seclabel.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
sequence.c Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
statscmds.c Virtual generated columns 2025-02-07 09:46:59 +01:00
subscriptioncmds.c Fix a WARNING for data origin discrepancies. 2025-02-21 14:34:40 +05:30
tablecmds.c Avoid bogus scans of partitions when marking FKs enforced 2025-06-05 18:39:06 +02:00
tablespace.c Remove unnecessary (char *) casts [xlog] 2025-02-13 10:57:07 +01:00
trigger.c Revert "Don't lock partitions pruned by initial pruning" 2025-05-22 17:02:35 +09:00
tsearchcmds.c Update copyright for 2025 2025-01-01 11:21:55 -05:00
typecmds.c Fix erroneous construction of functions' dependencies on transforms. 2025-04-07 13:31:37 -04:00
user.c Fix guc_malloc calls for consistency and OOM checks 2025-03-27 22:57:34 +01:00
vacuum.c Avoid ERROR at ON COMMIT DELETE ROWS after relhassubclass=f. 2025-04-20 08:28:48 -07:00
vacuumparallel.c Change internal queryid type from uint64 to int64 2025-05-30 22:59:39 +12:00
variable.c Fix bug allowing io_combine_limit > io_max_combine_combine limit 2025-04-25 13:31:24 -04:00
view.c Update copyright for 2025 2025-01-01 11:21:55 -05:00