@@ -64,13 +64,10 @@ BEGIN
64
64
IF NEW .initial_node != (SELECT shardman .get_node_id ()) THEN
65
65
EXECUTE format (' DROP TABLE IF EXISTS %I CASCADE;' , NEW .relation );
66
66
EXECUTE format(' %s' , NEW .create_sql );
67
- -- We are adding '_tmp' to default names, because
68
- -- these partitions will be immediately replaced with foreign tables
69
- -- having conventional names.
70
67
EXECUTE format(' select create_hash_partitions(%L, %L, %L, true, %L);' ,
71
68
NEW .relation , NEW .expr , NEW .partitions_count ,
72
69
(SELECT ARRAY(SELECT part_name FROM shardman .gen_part_names (
73
- NEW .relation , NEW .partitions_count , ' _tmp ' ))));
70
+ NEW .relation , NEW .partitions_count ))));
74
71
END IF;
75
72
RETURN NULL ;
76
73
END
@@ -110,6 +107,7 @@ DECLARE
110
107
um_opts text default ' ' ;
111
108
server_opts_first_time_through bool DEFAULT true;
112
109
um_opts_first_time_through bool DEFAULT true;
110
+ fdw_part_name text ;
113
111
BEGIN
114
112
IF NEW .owner != (SELECT shardman .get_node_id ()) THEN
115
113
raise info ' creating foreign table' ;
@@ -163,7 +161,10 @@ BEGIN
163
161
NEW .part_name );
164
162
EXECUTE format(' CREATE USER MAPPING FOR CURRENT_USER SERVER %I
165
163
%s;' , NEW .part_name , um_opts);
166
- EXECUTE format(' DROP FOREIGN TABLE IF EXISTS %I;' , NEW .part_name );
164
+ -- We use _fdw suffix for foreign tables to avoid interleaving with real
165
+ -- ones.
166
+ SELECT format(' %s_fdw' , NEW .part_name ) INTO fdw_part_name;
167
+ EXECUTE format(' DROP FOREIGN TABLE IF EXISTS %I;' , fdw_part_name);
167
168
168
169
-- Generate and execute CREATE FOREIGN TABLE sql statement which will
169
170
-- clone the existing local table schema. In constrast to
@@ -176,14 +177,18 @@ BEGIN
176
177
-- unneccessary involves network (we already have this schema locally)
177
178
-- and dangerous: what if table was created and dropped before this
178
179
-- change reached us?
180
+
179
181
EXECUTE format(' CREATE FOREIGN TABLE %I %s SERVER %I' ,
180
- NEW .part_name ,
181
- (select
182
- shardman .reconstruct_table_attrs (' partitioned_table_0_tmp' )),
182
+ fdw_part_name,
183
+ (SELECT
184
+ shardman .reconstruct_table_attrs (
185
+ format(' %I' , NEW .part_name ))),
183
186
NEW .part_name );
184
187
-- Finally, replace empty local tmp partition with foreign table
185
188
EXECUTE format(' SELECT replace_hash_partition(%L, %L)' ,
186
- format(' %s_tmp' , NEW .part_name ), NEW .part_name );
189
+ NEW .part_name , fdw_part_name);
190
+ -- And drop old empty table
191
+ EXECUTE format(' DROP TABLE %I' , NEW .part_name );
187
192
END IF;
188
193
RETURN NULL ;
189
194
END
0 commit comments