1 /*
2  * This file and its contents are licensed under the Apache License 2.0.
3  * Please see the included NOTICE for copyright information and
4  * LICENSE-APACHE for a copy of the license.
5  */
6 #ifndef TIMESCALEDB_CHUNK_INSERT_STATE_H
7 #define TIMESCALEDB_CHUNK_INSERT_STATE_H
8 
9 #include <nodes/execnodes.h>
10 #include <postgres.h>
11 #include <funcapi.h>
12 #include <access/tupconvert.h>
13 
14 #include "chunk.h"
15 #include "cache.h"
16 #include "cross_module_fn.h"
17 
18 typedef struct ChunkInsertState
19 {
20 	Relation rel;
21 	ResultRelInfo *result_relation_info;
22 	/* Per-chunk arbiter indexes for ON CONFLICT handling */
23 	List *arbiter_indexes;
24 
25 	/* When the tuple descriptors for the main hypertable (root) and a chunk
26 	 * differs, it is necessary to convert tuples to chunk format before
27 	 * insertion, ON CONFLICT, or RETURNING handling. In particular, with
28 	 * PG12, the table AM (storage format) can also differ between the
29 	 * hypertable root and each chunk (as well as between each chunk, in
30 	 * theory).
31 	 *
32 	 * Thus, for PG12, which has more mature partitioning support, the
33 	 * ResultRelInfo keeps per-relation slots for these purposes. In that
34 	 * case, the slots here simply points to the per-relation slots in the
35 	 * ResultRelInfo. However, earlier PostgreSQL versions are hardcoded to a
36 	 * single tuple table slot (for the root), and in that case each per-chunk
37 	 * slot here points to a common shared slot and it necessary to set the
38 	 * tuple descriptor on the slots when switching between chunks.
39 	 */
40 
41 	/* Pointer to slot for projected tuple in ON CONFLICT handling */
42 	TupleTableSlot *conflproj_slot;
43 	/* Pointer to slot for tuple replaced in ON CONFLICT DO UPDATE
44 	 * handling. Note that this slot's tuple descriptor is always the same as
45 	 * the chunk rel's. */
46 	TupleTableSlot *existing_slot;
47 
48 	/* Slot for inserted/new tuples going into the chunk */
49 	TupleTableSlot *slot;
50 	/* Map for converting tuple from hypertable (root table) format to chunk format */
51 	TupleConversionMap *hyper_to_chunk_map;
52 	MemoryContext mctx;
53 	EState *estate;
54 	List *chunk_data_nodes; /* List of data nodes for the chunk (ChunkDataNode objects) */
55 	int32 chunk_id;
56 	Oid user_id;
57 
58 	/* for tracking compressed chunks */
59 	Relation compress_rel;
60 	ResultRelInfo *orig_result_relation_info;
61 	CompressSingleRowState *compress_state;
62 } ChunkInsertState;
63 
64 typedef struct ChunkDispatch ChunkDispatch;
65 
66 extern ChunkInsertState *ts_chunk_insert_state_create(const Chunk *chunk, ChunkDispatch *dispatch);
67 extern void ts_chunk_insert_state_destroy(ChunkInsertState *state);
68 
69 #endif /* TIMESCALEDB_CHUNK_INSERT_STATE_H */
70