It seems tedious but I would create a table for each product in the sum.
CREATE TABLE foo (id uuid PRIMARY KEY);
CREATE TABLE bar (id uuid PRIMARY KEY,
s text NOT NULL);
CREATE TABLE baz (id uuid PRIMARY KEY,
a integer NOT NULL,
b integer NOT NULL,
c integer NOT NULL);
You probably want to store some metadata along with records of each type:
CREATE TABLE envelope (id uuid PRIMARY KEY,
t timestamptz NOT NULL DEFAULT now(),
by text NOT NULL DEFAULT sessions_user);
And this suggests a foreign key constraint:
CREATE TABLE foo (id uuid PRIMARY KEY REFERENCES envelope);
CREATE TABLE bar (id uuid PRIMARY KEY REFERENCES envelope,
s text NOT NULL);
CREATE TABLE baz (id uuid PRIMARY KEY REFERENCES envelope,
a integer NOT NULL,
b integer NOT NULL,
c integer NOT NULL);
And if you are even stricter you could imagine storing a ty
column with
the name of the type and using it to construct a composite foreign key. (As
described under "Where Not to Use Table Inheritance" in the LedgerSMB blog.)