layers_clearUnload
Take a graph out of the database without losing it: package it as a canonical file plus a manifest that anyone can verify offline, then remove it. Typical cases: dropping a large source once you've carved your slice, archiving a finished result, or handing a graph to someone else.
How you run it
pgrdf.export_graph(graph_id BIGINT) → SETOF TEXT
pgrdf.graph_manifest(graph_id BIGINT) → JSONB
pgrdf.drop_graph(iri TEXT | id BIGINT, cascade BOOLEAN DEFAULT true) → BIGINT
pgrdf.clear_graph(iri TEXT | id BIGINT) → BIGINTexport_graphreturns the graph's asserted triples as canonical N-Triples, one triple per row, byte-sorted.graph_manifestdescribes the graph and its export in one JSON document, including the SHA-256 of the exported bytes.drop_graphremoves the graph and its triples.clear_graphremoves the triples but keeps the graph, empty, under the same IRI.
Step 1 — package
Write the content and the manifest to files. These commands use the Docker container from the install page; with a local server, drop the docker exec pgrdf prefix.
Q="pgrdf.graph_id('http://example.org/products')"
docker exec pgrdf psql -U postgres -Atc "SELECT * FROM pgrdf.export_graph($Q)" > products.nt
docker exec pgrdf psql -U postgres -Atc "SELECT pgrdf.graph_manifest($Q)" > products.manifest.jsonFor the four-triple products graph from Seal, products.nt holds:
<http://example.org/gadget> <http://example.org/label> "Gadget" .
<http://example.org/gadget> <http://example.org/price> "25"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://example.org/widget> <http://example.org/label> "Widget" .
<http://example.org/widget> <http://example.org/price> "10"^^<http://www.w3.org/2001/XMLSchema#integer> .Step 2 — verify, without a database
Anyone holding the two files can check that the content is intact:
sha256sum products.nt
# b397118cdba33a34831c8f376308f78a38a0b6336e206f3e60e09494d9b5d2de products.nt
jq -r .digests.bytes.value products.manifest.json
# b397118cdba33a34831c8f376308f78a38a0b6336e206f3e60e09494d9b5d2deThe two hashes must match. On macOS without GNU coreutils, use shasum -a 256.
Step 3 — remove
SELECT pgrdf.drop_graph('http://example.org/products');
-- → 4 (triples removed)A sealed graph refuses to be dropped:
ERROR: 55P03: pgrdf: graph 1 is locked (release review): drop_graph refused.
Unlock with pgrdf.unlock_graph(1, '<reason>').Unlock it first, giving the reason:
SELECT pgrdf.unlock_graph(pgrdf.graph_id('http://example.org/products'), 'packaged');
SELECT pgrdf.drop_graph('http://example.org/products');Check it's gone:
SELECT iri FROM pgrdf.graph_inventory();
-- iri
-- -------------------
-- urn:pgrdf:graph:0Details:
- Drop by IRI refuses an unknown IRI (
42704), so a typo can't silently drop nothing. cascade => falserefuses when the graph holds inferred triples (2BP01). The default,cascade => true, removes them with it.- The default graph (
0) can't be dropped (22023). Load data into named graphs you create withadd_graph, so there's always a graph you can drop. - To empty a graph but keep its name, use
clear_graphinstead. It returns the number of triples removed and leaves an empty graph in the inventory.
Restoring
N-Triples is valid Turtle, so any loader reads the export back. Load it into a new graph and compare its identity digest with the manifest:
SELECT pgrdf.load_turtle('/tmp/products.nt',
pgrdf.add_graph('http://example.org/products-restored')); -- → 4
SELECT pgrdf.graph_digest(pgrdf.graph_id('http://example.org/products-restored'));
-- → b397118cdba33a34831c8f376308f78a38a0b6336e206f3e60e09494d9b5d2de
-- equals digests.identity.value in products.manifest.jsonThe file has to be on the database server (docker cp products.nt pgrdf:/tmp/products.nt). Inferred triples, locks and materialization state are not carried: the manifest's not_carried list says so. Re-run materialize on the restored graph if you need its inferred triples. The full recipe is in Identity and export.
Where it sits in a chain
Last. Unload a graph once nothing in the chain needs it in the database: the source after Carve, or a result after Seal.
See also
- Identity and export — digests, export and the manifest in detail.
- Clear, copy, move, drop — the graph operations reference.
- Pattern: Ingest → Carve → Reason — unloading the source after carving.