File tree 3 files changed +53
-4
lines changed
3 files changed +53
-4
lines changed Original file line number Diff line number Diff line change @@ -10008,6 +10008,34 @@ SELECT COUNT(*) FROM ftable;
10008
10008
2
10009
10009
(1 row)
10010
10010
10011
+ -- Disable batch inserting into foreign tables with BEFORE ROW INSERT triggers
10012
+ -- even if the batch_size option is enabled.
10013
+ ALTER FOREIGN TABLE ftable OPTIONS ( SET batch_size '10' );
10014
+ CREATE TRIGGER trig_row_before BEFORE INSERT ON ftable
10015
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
10016
+ EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable VALUES (3), (4);
10017
+ QUERY PLAN
10018
+ -------------------------------------------------------------
10019
+ Insert on public.ftable
10020
+ Remote SQL: INSERT INTO public.batch_table(x) VALUES ($1)
10021
+ Batch Size: 1
10022
+ -> Values Scan on "*VALUES*"
10023
+ Output: "*VALUES*".column1
10024
+ (5 rows)
10025
+
10026
+ INSERT INTO ftable VALUES (3), (4);
10027
+ NOTICE: trig_row_before(23, skidoo) BEFORE ROW INSERT ON ftable
10028
+ NOTICE: NEW: (3)
10029
+ NOTICE: trig_row_before(23, skidoo) BEFORE ROW INSERT ON ftable
10030
+ NOTICE: NEW: (4)
10031
+ SELECT COUNT(*) FROM ftable;
10032
+ count
10033
+ -------
10034
+ 4
10035
+ (1 row)
10036
+
10037
+ -- Clean up
10038
+ DROP TRIGGER trig_row_before ON ftable;
10011
10039
DROP FOREIGN TABLE ftable;
10012
10040
DROP TABLE batch_table;
10013
10041
-- Use partitioning
Original file line number Diff line number Diff line change @@ -2012,8 +2012,8 @@ postgresExecForeignBatchInsert(EState *estate,
2012
2012
* Determine the maximum number of tuples that can be inserted in bulk
2013
2013
*
2014
2014
* Returns the batch size specified for server or table. When batching is not
2015
- * allowed (e.g. for tables with AFTER ROW triggers or with RETURNING clause),
2016
- * returns 1.
2015
+ * allowed (e.g. for tables with BEFORE/ AFTER ROW triggers or with RETURNING
2016
+ * clause), returns 1.
2017
2017
*/
2018
2018
static int
2019
2019
postgresGetForeignModifyBatchSize (ResultRelInfo * resultRelInfo )
@@ -2042,10 +2042,19 @@ postgresGetForeignModifyBatchSize(ResultRelInfo *resultRelInfo)
2042
2042
else
2043
2043
batch_size = get_batch_size_option (resultRelInfo -> ri_RelationDesc );
2044
2044
2045
- /* Disable batching when we have to use RETURNING. */
2045
+ /*
2046
+ * Disable batching when we have to use RETURNING or there are any
2047
+ * BEFORE/AFTER ROW INSERT triggers on the foreign table.
2048
+ *
2049
+ * When there are any BEFORE ROW INSERT triggers on the table, we can't
2050
+ * support it, because such triggers might query the table we're inserting
2051
+ * into and act differently if the tuples that have already been processed
2052
+ * and prepared for insertion are not there.
2053
+ */
2046
2054
if (resultRelInfo -> ri_projectReturning != NULL ||
2047
2055
(resultRelInfo -> ri_TrigDesc &&
2048
- resultRelInfo -> ri_TrigDesc -> trig_insert_after_row ))
2056
+ (resultRelInfo -> ri_TrigDesc -> trig_insert_before_row ||
2057
+ resultRelInfo -> ri_TrigDesc -> trig_insert_after_row )))
2049
2058
return 1 ;
2050
2059
2051
2060
/*
Original file line number Diff line number Diff line change @@ -3135,6 +3135,18 @@ CREATE FOREIGN TABLE ftable ( x int ) SERVER loopback OPTIONS ( table_name 'batc
3135
3135
EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable VALUES (1 ), (2 );
3136
3136
INSERT INTO ftable VALUES (1 ), (2 );
3137
3137
SELECT COUNT (* ) FROM ftable;
3138
+
3139
+ -- Disable batch inserting into foreign tables with BEFORE ROW INSERT triggers
3140
+ -- even if the batch_size option is enabled.
3141
+ ALTER FOREIGN TABLE ftable OPTIONS ( SET batch_size ' 10' );
3142
+ CREATE TRIGGER trig_row_before BEFORE INSERT ON ftable
3143
+ FOR EACH ROW EXECUTE PROCEDURE trigger_data(23 ,' skidoo' );
3144
+ EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable VALUES (3 ), (4 );
3145
+ INSERT INTO ftable VALUES (3 ), (4 );
3146
+ SELECT COUNT (* ) FROM ftable;
3147
+
3148
+ -- Clean up
3149
+ DROP TRIGGER trig_row_before ON ftable;
3138
3150
DROP FOREIGN TABLE ftable;
3139
3151
DROP TABLE batch_table;
3140
3152
You can’t perform that action at this time.
0 commit comments