I have a large list of subnets in a network stored in the following layout. This is used as a master table to store assets that will be used to automatically probe for status by a Python-script at regular intervals.
CREATE TEMP TABLE tmp_networks (
network cidr PRIMARY KEY
);
Lets assume its filled with these values for the sake of demonstration:
- 10.0.0.0/8
- 10.0.1.0/24
- 192.168.0.0/24
When I run the script, the Python-script will execute the following query to remove any overlaps:
SELECT network
FROM tmp_networks
WHERE NOT EXISTS (
SELECT network
FROM tmp_networks n
WHERE n.network >> tmp_networks.network
);
This works great, except for one tiny issue; I also have a list of individual addresses that should be excluded from the job. This is also a table in the database:
CREATE TEMP TABLE tmp_except (
address inet PRIMARY KEY
);
Lets assume this contains the following addresses:
- 10.0.0.100
- 192.168.0.10
Now, I have failed to find a good method to remove these spesific addresses from the database output. In my thoughts, the solution would be something like:
- select all subnets
- if any exception address is found within the subnet, split the subnet into smaller pieces until the single exception address can be removed and all other addresses remain
I have tried to investigate whether something like this is possible to do in pure PostgreSQL, but have failed to find any way to solve this. Any pointers as to how this should be solved?