1010#include "debug_point.h"
1111#include "hypercube.h"
1212#include "nodes/chunk_dispatch/chunk_insert_state.h"
13+ #include "nodes/modify_hypertable.h"
1314#include "subspace_store.h"
1415
1516static ChunkInsertState * chunk_insert_state_create (Oid chunk_relid , ChunkTupleRouting * ctr );
1617
1718ChunkTupleRouting *
18- ts_chunk_tuple_routing_create (EState * estate , Relation rel )
19+ ts_chunk_tuple_routing_create (EState * estate , ResultRelInfo * rri )
1920{
2021 ChunkTupleRouting * ctr ;
2122
@@ -27,14 +28,16 @@ ts_chunk_tuple_routing_create(EState *estate, Relation rel)
2728 * single tuple into a partitioned table and this must be fast.
2829 */
2930 ctr = (ChunkTupleRouting * ) palloc0 (sizeof (ChunkTupleRouting ));
30- ctr -> partition_root = rel ;
31+ ctr -> hypertable_rri = rri ;
32+ ctr -> partition_root = rri -> ri_RelationDesc ;
3133 ctr -> memcxt = CurrentMemoryContext ;
3234 ctr -> estate = estate ;
3335 ctr -> counters = palloc0 (sizeof (SharedCounters ));
3436
35- ctr -> hypertable = ts_hypertable_cache_get_cache_and_entry (RelationGetRelid (rel ),
36- CACHE_FLAG_NONE ,
37- & ctr -> hypertable_cache );
37+ ctr -> hypertable =
38+ ts_hypertable_cache_get_cache_and_entry (RelationGetRelid (rri -> ri_RelationDesc ),
39+ CACHE_FLAG_NONE ,
40+ & ctr -> hypertable_cache );
3841 ctr -> subspace = ts_subspace_store_init (ctr -> hypertable -> space ,
3942 estate -> es_query_cxt ,
4043 ts_guc_max_open_chunks_per_insert );
@@ -248,3 +251,42 @@ chunk_insert_state_create(Oid chunk_relid, ChunkTupleRouting *ctr)
248251
249252 return state ;
250253}
254+
255+ extern void
256+ ts_chunk_tuple_routing_decompress_for_insert (ChunkInsertState * cis , TupleTableSlot * slot ,
257+ EState * estate , bool update_counter )
258+ {
259+ if (!cis -> chunk_compressed || (cis -> cached_decompression_state &&
260+ !cis -> cached_decompression_state -> has_primary_or_unique_index ))
261+ return ;
262+
263+ /*
264+ * If this is an INSERT into a compressed chunk with UNIQUE or
265+ * PRIMARY KEY constraints we need to make sure any batches that could
266+ * potentially lead to a conflict are in the decompressed chunk so
267+ * postgres can do proper constraint checking.
268+ */
269+
270+ ts_cm_functions -> init_decompress_state_for_insert (cis , slot );
271+ ts_cm_functions -> decompress_batches_for_insert (cis , slot );
272+
273+ /* mark rows visible */
274+ if (update_counter )
275+ estate -> es_output_cid = GetCurrentCommandId (true);
276+
277+ if (ts_guc_max_tuples_decompressed_per_dml > 0 )
278+ {
279+ if (cis -> counters -> tuples_decompressed > ts_guc_max_tuples_decompressed_per_dml )
280+ {
281+ ereport (ERROR ,
282+ (errcode (ERRCODE_CONFIGURATION_LIMIT_EXCEEDED ),
283+ errmsg ("tuple decompression limit exceeded by operation" ),
284+ errdetail ("current limit: %d, tuples decompressed: %lld" ,
285+ ts_guc_max_tuples_decompressed_per_dml ,
286+ (long long int ) cis -> counters -> tuples_decompressed ),
287+ errhint ("Consider increasing "
288+ "timescaledb.max_tuples_decompressed_per_dml_transaction or set "
289+ "to 0 (unlimited)." )));
290+ }
291+ }
292+ }
0 commit comments