I want to continuously update a table with all the unique combinations encountered of three different IDs. These IDs can be product/group/region IDs etc. I've abstracted that away here. I want to update this table by calling create_combinations_if_needed('{{1, 10, 100}, {2, 11, 101}}')
, which will create the combinations {1, 10, 100}
and {1, 10, 100}
if they don't already exist.
Here is my attempt at doing that below.
CREATE TABLE combinations (
id serial PRIMARY KEY,
some_id1 integer NOT NULL,
some_id2 integer NOT NULL,
some_id3 integer NOT NULL
);
CREATE UNIQUE INDEX ON combinations(some_id1, some_id2, some_id3);
CREATE OR REPLACE function create_combinations_if_needed(p_combinations integer[][]) RETURNS boolean
LANGUAGE sql AS
$$
INSERT INTO combinations (some_id1, some_id2, some_id3)
SELECT some_id1, some_id2, some_id3
FROM UNNEST(p_combinations) AS comb(some_id1, some_id3, some_id3)
ON CONFLICT (some_id1, some_id2, some_id3)
DO NOTHING
RETURNING TRUE;
$$;
However, if I try to create this function I get the following error:
ERROR: table "comb" has 1 columns available but 3 columns specified
CONTEXT: SQL function "create_combinations_if_needed"
What am I doing wrong?