-- Follow DSP.
EXPLAIN ANALYZE
WITH RECURSIVE location(product, distribution, sourcepackagename) AS (
VALUES (1306::integer, NULL::integer, NULL::integer)
UNION
SELECT
shared_location.to_product,
shared_location.to_distro,
shared_location.to_spn
FROM
location,
LATERAL (
-- Find DistributionSourcePackage from Product via Packaging.
SELECT
NULL::integer AS to_product,
ds_from_packaging.distribution AS to_distro,
packaging_from_ps.sourcepackagename AS to_spn
FROM
productseries AS ps_from_location
JOIN packaging AS packaging_from_ps
ON packaging_from_ps.productseries = ps_from_location.id
JOIN distroseries AS ds_from_packaging
ON ds_from_packaging.id = packaging_from_ps.distroseries
WHERE ps_from_location.product = location.product
UNION ALL
-- Find Product from DistributionSourcePackage via Packaging.
SELECT
ps_from_packaging.product AS to_product,
NULL::integer AS to_distro,
NULL::integer AS to_spn
FROM
distroseries AS ds_from_location
JOIN packaging AS packaging_from_ds
ON packaging_from_ds.distroseries = ds_from_location.id
JOIN productseries AS ps_from_packaging
ON ps_from_packaging.id = packaging_from_ds.productseries
WHERE
ds_from_location.distribution = location.distribution
AND packaging_from_ds.sourcepackagename = location.sourcepackagename
UNION ALL
-- Find DistributionSourcePackage in parent distributions.
SELECT
NULL::integer AS to_product,
ds_from_dsp.distribution AS to_distro,
location.sourcepackagename AS to_spn
FROM
distroseries AS ds_from_location
JOIN distroseriesparent
ON distroseriesparent.derived_series =
ds_from_location.id
JOIN distroseries AS ds_from_dsp
ON ds_from_dsp.id = distroseriesparent.parent_series
WHERE
ds_from_location.distribution = location.distribution
UNION ALL
-- Find DistributionSourcePackage in child distributions.
SELECT
NULL::integer AS to_product,
ds_from_dsp.distribution AS to_distro,
location.sourcepackagename AS to_spn
FROM
distroseries AS ds_from_location
JOIN distroseriesparent
ON distroseriesparent.parent_series =
ds_from_location.id
JOIN distroseries AS ds_from_dsp
ON ds_from_dsp.id = distroseriesparent.derived_series
WHERE
ds_from_location.distribution = location.distribution
) AS shared_location
WHERE
-- All rows must have a location.
shared_location.to_product IS NOT NULL
OR shared_location.to_distro IS NOT NULL)
SELECT * FROM location;