From e618345703875a41c93f56a8f5917fc7d8e8a734 Mon Sep 17 00:00:00 2001 From: Gokhan Gulbiz Date: Wed, 29 Mar 2023 15:50:12 +0300 Subject: [PATCH] Handle identity columns properly in the router planner (#6802) DESCRIPTION: Fixes a bug with insert..select queries with identity columns Fixes #6798 --- .../distributed/deparser/citus_ruleutils.c | 9 ++++++++- .../regress/expected/generated_identity.out | 17 +++++++++++++++++ src/test/regress/sql/generated_identity.sql | 7 +++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/backend/distributed/deparser/citus_ruleutils.c b/src/backend/distributed/deparser/citus_ruleutils.c index ada77b098..04591fae0 100644 --- a/src/backend/distributed/deparser/citus_ruleutils.c +++ b/src/backend/distributed/deparser/citus_ruleutils.c @@ -1391,7 +1391,7 @@ convert_aclright_to_string(int aclright) /* * contain_nextval_expression_walker walks over expression tree and returns - * true if it contains call to 'nextval' function. + * true if it contains call to 'nextval' function or it has an identity column. */ bool contain_nextval_expression_walker(Node *node, void *context) @@ -1401,6 +1401,13 @@ contain_nextval_expression_walker(Node *node, void *context) return false; } + /* check if the node contains an identity column */ + if (IsA(node, NextValueExpr)) + { + return true; + } + + /* check if the node contains call to 'nextval' */ if (IsA(node, FuncExpr)) { FuncExpr *funcExpr = (FuncExpr *) node; diff --git a/src/test/regress/expected/generated_identity.out b/src/test/regress/expected/generated_identity.out index 23a87af3d..b484dda1b 100644 --- a/src/test/regress/expected/generated_identity.out +++ b/src/test/regress/expected/generated_identity.out @@ -522,4 +522,21 @@ INSERT INTO color(color_name) VALUES ('Red'); \c - - - :master_port SET search_path TO generated_identities; SET client_min_messages to ERROR; +DROP TABLE IF EXISTS test; +CREATE TABLE test (x int, y int, z bigint generated by default as identity); +SELECT create_distributed_table('test', 'x', colocate_with := 'none'); + create_distributed_table +--------------------------------------------------------------------- + +(1 row) + +INSERT INTO test VALUES (1,2); +INSERT INTO test SELECT x, y FROM test WHERE x = 1; +SELECT * FROM test; + x | y | z +--------------------------------------------------------------------- + 1 | 2 | 1 + 1 | 2 | 2 +(2 rows) + DROP SCHEMA generated_identities CASCADE; diff --git a/src/test/regress/sql/generated_identity.sql b/src/test/regress/sql/generated_identity.sql index 004f45b40..83c16eaa8 100644 --- a/src/test/regress/sql/generated_identity.sql +++ b/src/test/regress/sql/generated_identity.sql @@ -263,4 +263,11 @@ INSERT INTO color(color_name) VALUES ('Red'); SET search_path TO generated_identities; SET client_min_messages to ERROR; +DROP TABLE IF EXISTS test; +CREATE TABLE test (x int, y int, z bigint generated by default as identity); +SELECT create_distributed_table('test', 'x', colocate_with := 'none'); +INSERT INTO test VALUES (1,2); +INSERT INTO test SELECT x, y FROM test WHERE x = 1; +SELECT * FROM test; + DROP SCHEMA generated_identities CASCADE;