pub struct SqlxPgGraph { /* private fields */ }Expand description
Async PostgreSQL graph backend over a shared sqlx::PgPool.
Every constructor applies the schema and runs pending migrations on connect,
matching PgGraph. An optional table_prefix namespaces every backing table
and index so several graphs can coexist in one database (identical semantics
to PgGraph’s *_with_prefix constructors). The prefix is validated by
is_identifier_safe before any SQL is emitted, keeping the identifier
interpolation injection-safe.
§Transaction exposure
pool plus append_edges_in_tx and
remove_edges_for_node_in_tx let a caller
coordinate a multi-table transaction (e.g. klr’s prune: delete chunks +
vectors + edges atomically). Begin a tx on pool(), run cross-table DML,
call the *_in_tx helpers with the same &mut PgConnection, then commit or
rollback — mirroring PgVectorIndex::remove_in_tx.
Implementations§
Source§impl SqlxPgGraph
impl SqlxPgGraph
Sourcepub async fn from_pool(pool: PgPool) -> Result<Self>
pub async fn from_pool(pool: PgPool) -> Result<Self>
Adopt a caller-owned PgPool, apply schema + migrations, and return a
ready backend. Uses the default (empty) table prefix.
Sourcepub async fn from_pool_with_prefix(pool: PgPool, prefix: &str) -> Result<Self>
pub async fn from_pool_with_prefix(pool: PgPool, prefix: &str) -> Result<Self>
Like from_pool, but every table/index is namespaced
under prefix. The prefix is validated by is_identifier_safe before
any SQL runs; an unsafe value returns KernelError::Store.
Sourcepub async fn connect(url: &str) -> Result<Self>
pub async fn connect(url: &str) -> Result<Self>
Connect to url (libpq connstring / postgresql://…) with an 8-connection
pool, apply schema + migrations, and return a ready backend. Uses the
default (empty) table prefix.
Sourcepub async fn connect_with_prefix(url: &str, prefix: &str) -> Result<Self>
pub async fn connect_with_prefix(url: &str, prefix: &str) -> Result<Self>
Like connect, but every table/index is namespaced
under prefix.
Sourcepub fn pool(&self) -> &PgPool
pub fn pool(&self) -> &PgPool
The underlying connection pool.
Callers that need cross-table transactional consistency — e.g. pruning a
law’s chunks plus their graph edges atomically — can pool().begin() and
run their own DML, then call the append_edges_in_tx
/ remove_edges_for_node_in_tx
helpers on the same &mut PgConnection.
Sourcepub async fn current_version(&self) -> Result<u32>
pub async fn current_version(&self) -> Result<u32>
Recorded graph schema version from {prefix}_meta, or 0 if unset.
Sourcepub async fn migrate(&self) -> Result<u32>
pub async fn migrate(&self) -> Result<u32>
Apply pending migrations up to GRAPH_SCHEMA_VERSION. No-op when current.
Runs in a single transaction with rollback on failure — matching
pg.rs::migrate. Returns the new version.
Sourcepub async fn upsert_node(&self, node: &GraphNode) -> Result<()>
pub async fn upsert_node(&self, node: &GraphNode) -> Result<()>
Insert or update a node (upsert by id). Mirrors PgGraph::upsert_node.
Sourcepub async fn read_node(&self, id: &str) -> Result<Option<GraphNode>>
pub async fn read_node(&self, id: &str) -> Result<Option<GraphNode>>
Read a node by id, or None if absent.
Sourcepub async fn delete_node(&self, id: &str) -> Result<bool>
pub async fn delete_node(&self, id: &str) -> Result<bool>
Delete a node by id. Returns true if a row was removed.
Sourcepub async fn append_edge(&self, edge: &GraphEdge) -> Result<()>
pub async fn append_edge(&self, edge: &GraphEdge) -> Result<()>
Append a single edge (ON CONFLICT DO NOTHING). Mirrors PgGraph::append_edge.
Sourcepub async fn edges_for_node(&self, node_id: &str) -> Result<Vec<GraphEdge>>
pub async fn edges_for_node(&self, node_id: &str) -> Result<Vec<GraphEdge>>
All edges touching node_id (either endpoint). Mirrors PgGraph::edges_for_node.
Sourcepub async fn delete_edge(&self, id: &str) -> Result<bool>
pub async fn delete_edge(&self, id: &str) -> Result<bool>
Delete a single edge by id. Returns true if a row was removed.
Sourcepub async fn append_edges(&self, edges: &[GraphEdge]) -> Result<()>
pub async fn append_edges(&self, edges: &[GraphEdge]) -> Result<()>
Batch-insert edges in chunks of 5000, each chunk its own transaction
(ON CONFLICT DO NOTHING per row). Mirrors PgGraph::append_edges.
sqlx caches the prepared statement per connection, so the repeated
INSERT reuses one parse/plan per chunk.
Sourcepub async fn append_edges_in_tx(
&self,
tx: &mut PgConnection,
edges: &[GraphEdge],
) -> Result<()>
pub async fn append_edges_in_tx( &self, tx: &mut PgConnection, edges: &[GraphEdge], ) -> Result<()>
Append edges within a caller-provided transaction (commit is the
caller’s responsibility — this method does not commit). Enables atomic
cross-table writes: begin a tx on pool, write chunks /
vectors / other relations, call this with the same &mut PgConnection,
then commit(). A failure anywhere rolls back the whole set.
Sourcepub async fn edges_for_node_dir(
&self,
node_id: &str,
dir: EdgeDirection,
relation: Option<&str>,
) -> Result<Vec<GraphEdge>>
pub async fn edges_for_node_dir( &self, node_id: &str, dir: EdgeDirection, relation: Option<&str>, ) -> Result<Vec<GraphEdge>>
Directed, optionally relation-filtered edge lookup. Mirrors
PgGraph::edges_for_node_dir. dir selects out / in / both; relation
further restricts to a single relationship type when Some.
Sourcepub async fn neighbors_weighted(
&self,
seed_ids: &[String],
dir: EdgeDirection,
relation: Option<&str>,
) -> Result<Vec<(String, f64)>>
pub async fn neighbors_weighted( &self, seed_ids: &[String], dir: EdgeDirection, relation: Option<&str>, ) -> Result<Vec<(String, f64)>>
Aggregate neighbor weights from seed nodes, optionally direction- and
relation-filtered. Mirrors PgGraph::neighbors_weighted: walks the
source/target halves, uses = ANY($1) for the seed set, GROUP BY the
opposite endpoint, and SUM(weight). Seeds themselves are excluded.
Capped at 100 seeds.
Sourcepub async fn remove_edges_for_node(&self, node_id: &str) -> Result<()>
pub async fn remove_edges_for_node(&self, node_id: &str) -> Result<()>
Remove every edge touching node_id (either endpoint).
Mirrors PgGraph::remove_edges_for_node.
Sourcepub async fn remove_edges_for_node_in_tx(
&self,
tx: &mut PgConnection,
node_id: &str,
) -> Result<()>
pub async fn remove_edges_for_node_in_tx( &self, tx: &mut PgConnection, node_id: &str, ) -> Result<()>
Remove edges for node_id within a caller-provided transaction (no
commit here). Same atomic-prune pattern as Self::append_edges_in_tx.
Sourcepub async fn search_nodes(
&self,
query: &str,
limit: usize,
) -> Result<Vec<GraphNode>>
pub async fn search_nodes( &self, query: &str, limit: usize, ) -> Result<Vec<GraphNode>>
ILIKE substring search over (title, body, tags) for each
whitespace-separated term (AND of terms). Mirrors PgGraph::search_nodes
— extension-free vanilla PostgreSQL ILIKE with escaped, wrapped patterns.
Bidirectional BFS up to depth hops from start_id via a recursive CTE.
Mirrors PgGraph::related_nodes. Returns distinct neighbor IDs (the start
node is excluded), capped at 500.
Auto Trait Implementations§
impl !RefUnwindSafe for SqlxPgGraph
impl !UnwindSafe for SqlxPgGraph
impl Freeze for SqlxPgGraph
impl Send for SqlxPgGraph
impl Sync for SqlxPgGraph
impl Unpin for SqlxPgGraph
impl UnsafeUnpin for SqlxPgGraph
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.