U
U
uuushka2015-07-31 14:34:46
PostgreSQL
uuushka, 2015-07-31 14:34:46

JPA + partitions in PostgreSQL. How to implement an insert only in a partition?

There is a table TABLE in PostgreSQL which has n partitions (TABLE_1, TABLE_2, ..., TABLE_n). There is a trigger that, when inserted into a TABLE table, inserts data into a partition. Trigger:

IF (NEW.program_id=1) THEN INSERT INTO st.table_1 VALUES (NEW.*);
        ELSIF (NEW.program_id=2) THEN INSERT INTO st.table_2 VALUES (NEW.*);
 ....
return NEW;

There is an object
@Entity
@Table(name = "TABLE", schema = "st")
public class TABLE implements Serializable {


@Getter
@Setter
@Column(name = "id", nullable = false, insertable = false, columnDefinition = "integer auto_increment")
private Integer id;


@Getter
@Setter
@Column(name = "program_id", nullable = false)
private Integer programId;

.... }

There is also a JpaRepository
public interface TableRepository extends JpaRepository<TABLE, Integer> {}

So, when saving the TABLE object, the
TableRepository.save(myTable);
record is duplicated ... How to make it so that the record was only in the partition?? What exactly is wrong? in code or in trigger?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Neonailol, 2015-08-19
@Neonailol

The trigger must be defined as

BEFORE INSERT ON ТАБЛИЦА
FOR EACH ROW EXECUTE PROCEDURE ТРИГГЕР

to get rid of duplication, the trigger function must return NULL
More information about table partitioning: Manual

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question