postgresql/src/backend/parser
Tom Lane fbbf68094c Disallow non-default collation in ADD PRIMARY KEY/UNIQUE USING INDEX.
When creating a uniqueness constraint using a pre-existing index,
we have always required that the index have the same properties you'd
get if you just let a new index get built.  However, when collations
were added, we forgot to add the index's collation to that check.

It's hard to trip over this without intentionally trying to break it:
you'd have to explicitly specify a different collation in CREATE
INDEX, then convert it to a pkey or unique constraint.  Still, if you
did that, pg_dump would emit a script that fails to reproduce the
index's collation.  The main practical problem is that after a
pg_upgrade the index would be corrupt, because its actual physical
order wouldn't match what pg_index says.  A more theoretical issue,
which is new as of v12, is that if you create the index with a
nondeterministic collation then it wouldn't be enforcing the normal
notion of uniqueness, causing the constraint to mean something
different from a normally-created constraint.

To fix, just add collation to the conditions checked for index
acceptability in ADD PRIMARY KEY/UNIQUE USING INDEX.  We won't try
to clean up after anybody who's already created such a situation;
it seems improbable enough to not be worth the effort involved.
(If you do get into trouble, a REINDEX should be enough to fix it.)

In principle this is a long-standing bug, but I chose not to
back-patch --- the odds of causing trouble seem about as great
as the odds of preventing it, and both risks are very low anyway.

Per report from Alexey Bashtanov, though this is not his preferred
fix.

Discussion: https://postgr.es/m/b05ce36a-cefb-ca5e-b386-a400535b1c0b@imap.cc
2019-12-06 11:25:09 -05:00
..
.gitignore
Makefile Split all OBJS style lines in makefiles into one-line-per-entry style. 2019-11-05 14:41:07 -08:00
README
analyze.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
check_keywords.pl Update copyright for 2019 2019-01-02 12:44:25 -05:00
gram.y Allow ALTER VIEW command to rename the column in the view. 2019-11-21 19:55:13 +09:00
parse_agg.c Rationalize use of list_concat + list_copy combinations. 2019-08-12 11:20:18 -04:00
parse_clause.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_coerce.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
parse_collate.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
parse_cte.c Represent Lists as expansible arrays, not chains of cons-cells. 2019-07-15 13:41:58 -04:00
parse_enr.c Update copyright for 2019 2019-01-02 12:44:25 -05:00
parse_expr.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_func.c Finish reverting commit 0a52d378b. 2019-11-12 16:58:08 -05:00
parse_node.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_oper.c Fix inconsistencies and typos in the tree, take 9 2019-08-05 12:14:58 +09:00
parse_param.c Phase 2 pgindent run for v12. 2019-05-22 13:04:48 -04:00
parse_relation.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_target.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_type.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30
parse_utilcmd.c Disallow non-default collation in ADD PRIMARY KEY/UNIQUE USING INDEX. 2019-12-06 11:25:09 -05:00
parser.c Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
scan.l Replace the data structure used for keyword lookup. 2019-01-06 17:02:57 -05:00
scansup.c Make the order of the header file includes consistent in backend modules. 2019-11-12 08:30:16 +05:30

README

src/backend/parser/README

Parser
======

This directory does more than tokenize and parse SQL queries.  It also
creates Query structures for the various complex queries that are passed
to the optimizer and then executor.

parser.c	things start here
scan.l		break query into tokens
scansup.c	handle escapes in input strings
gram.y		parse the tokens and produce a "raw" parse tree
analyze.c	top level of parse analysis for optimizable queries
parse_agg.c	handle aggregates, like SUM(col1),  AVG(col2), ...
parse_clause.c	handle clauses like WHERE, ORDER BY, GROUP BY, ...
parse_coerce.c	handle coercing expressions to different data types
parse_collate.c	assign collation information in completed expressions
parse_cte.c	handle Common Table Expressions (WITH clauses)
parse_expr.c	handle expressions like col, col + 3, x = 3 or x = 4
parse_func.c	handle functions, table.column and column identifiers
parse_node.c	create nodes for various structures
parse_oper.c	handle operators in expressions
parse_param.c	handle Params (for the cases used in the core backend)
parse_relation.c support routines for tables and column handling
parse_target.c	handle the result list of the query
parse_type.c	support routines for data type handling
parse_utilcmd.c	parse analysis for utility commands (done at execution time)

See also src/common/keywords.c, which contains the table of standard
keywords and the keyword lookup function.  We separated that out because
various frontend code wants to use it too.