Skip to content

Commit

Permalink
postgres-new-audit.sql: notes about partition "inheritance" + docs link
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Feb 20, 2023
1 parent 0da6f9a commit 69f9788
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config/sql/native-new/postgres-new-audit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ CREATE TABLE ma_audit_event_default PARTITION OF ma_audit_event DEFAULT;
CREATE TABLE ma_audit_delta_default PARTITION OF ma_audit_delta DEFAULT;
CREATE TABLE ma_audit_ref_default PARTITION OF ma_audit_ref DEFAULT;

/*
For info about what is and is not automatically created on the partition, see:
https://www.postgresql.org/docs/13/sql-createtable.html (search for "PARTITION OF parent_table")
In short, for our case PK and constraints are created automatically, but FK are not.
*/
ALTER TABLE ma_audit_delta_default ADD CONSTRAINT ma_audit_delta_default_fk
FOREIGN KEY (recordId, timestamp) REFERENCES ma_audit_event_default (id, timestamp)
ON DELETE CASCADE;
Expand Down Expand Up @@ -287,6 +292,11 @@ BEGIN
'CREATE TABLE %I PARTITION OF ma_audit_ref FOR VALUES FROM (%L) TO (%L);',
'ma_audit_ref_' || tableSuffix, dateFrom, dateTo);

/*
For info about what is and is not automatically created on the partition, see:
https://www.postgresql.org/docs/13/sql-createtable.html (search for "PARTITION OF parent_table")
In short, for our case PK and constraints are created automatically, but FK are not.
*/
EXECUTE format(
'ALTER TABLE %I ADD CONSTRAINT %I FOREIGN KEY (recordId, timestamp)' ||
' REFERENCES %I (id, timestamp) ON DELETE CASCADE',
Expand Down

0 comments on commit 69f9788

Please sign in to comment.