From ca5c7043fcf3b5f34f1e71f24cab6d344e2310f8 Mon Sep 17 00:00:00 2001 From: Zoran Cvetkov Date: Wed, 12 Feb 2025 15:24:41 +0200 Subject: [PATCH 1/4] add vid always --- store/postgres/src/relational/dsl.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/store/postgres/src/relational/dsl.rs b/store/postgres/src/relational/dsl.rs index 4307fc7c776..feb615b4ac4 100644 --- a/store/postgres/src/relational/dsl.rs +++ b/store/postgres/src/relational/dsl.rs @@ -256,7 +256,6 @@ impl<'a> Table<'a> { match column_names { AttributeNames::All => { cols.extend(self.meta.columns.iter()); - cols.push(&*VID_COL); } AttributeNames::Select(names) => { let pk = self.meta.primary_key(); @@ -283,8 +282,8 @@ impl<'a> Table<'a> { } } + cols.push(&*VID_COL); if T::WITH_SYSTEM_COLUMNS { - cols.push(&*VID_COL); if self.meta.immutable { cols.push(&*BLOCK_COL); } else { From 2674b53a20aead449dde919dfc295810460bad92 Mon Sep 17 00:00:00 2001 From: Zoran Cvetkov Date: Wed, 12 Feb 2025 17:50:41 +0200 Subject: [PATCH 2/4] comments and renames --- graph/src/schema/entity_type.rs | 7 +++---- graph/src/schema/input/mod.rs | 4 ++++ store/postgres/src/relational/ddl.rs | 2 +- store/postgres/src/relational/prune.rs | 2 +- store/postgres/src/relational_queries.rs | 6 +++--- 5 files changed, 12 insertions(+), 9 deletions(-) diff --git a/graph/src/schema/entity_type.rs b/graph/src/schema/entity_type.rs index f9fe93cc90e..098b48362b9 100644 --- a/graph/src/schema/entity_type.rs +++ b/graph/src/schema/entity_type.rs @@ -151,10 +151,9 @@ impl EntityType { self.schema.is_object_type(self.atom) } - // Changes the way the VID field is generated. It used to be autoincrement. Now its - // based on block number and the order of the entities in a block. The latter - // represents the write order across all entity types in the subgraph. - pub fn strict_vid_order(&self) -> bool { + /// Whether the table for this entity type uses a sequence for the `vid` or whether + /// `graph-node` sets them explicitly. See also [`InputSchema.strict_vid_order()`] + pub fn has_vid_seq(&self) -> bool { // Currently the agregations entities don't have VIDs in insertion order self.schema.strict_vid_order() && self.is_object_type() } diff --git a/graph/src/schema/input/mod.rs b/graph/src/schema/input/mod.rs index 9ca31592c11..5ff8ddcda2f 100644 --- a/graph/src/schema/input/mod.rs +++ b/graph/src/schema/input/mod.rs @@ -1589,6 +1589,10 @@ impl InputSchema { Some(EntityType::new(self.cheap_clone(), obj_type.name)) } + /// How the values for the VID field are generated. + /// When this is `false`, this subgraph uses the old way of autoincrementing `vid` in the database. + /// When it is `true`, `graph-node` sets the `vid` explicitly to a number based on block number + /// and the order in which entities are written, and comparing by `vid` will order entities by that order. pub fn strict_vid_order(&self) -> bool { self.inner.spec_version >= SPEC_VERSION_1_3_0 } diff --git a/store/postgres/src/relational/ddl.rs b/store/postgres/src/relational/ddl.rs index a19972ea268..40a02d6051e 100644 --- a/store/postgres/src/relational/ddl.rs +++ b/store/postgres/src/relational/ddl.rs @@ -116,7 +116,7 @@ impl Table { Ok(cols) } - let vid_type = if self.object.strict_vid_order() { + let vid_type = if self.object.has_vid_seq() { "bigint" } else { "bigserial" diff --git a/store/postgres/src/relational/prune.rs b/store/postgres/src/relational/prune.rs index 0e82c1bfcc7..62632549397 100644 --- a/store/postgres/src/relational/prune.rs +++ b/store/postgres/src/relational/prune.rs @@ -205,7 +205,7 @@ impl TablePair { // Make sure the vid sequence continues from where it was in case // that we use autoincrementing order of the DB - if !self.src.object.strict_vid_order() { + if !self.src.object.has_vid_seq() { writeln!( query, "select setval('{dst_nsp}.{vid_seq}', nextval('{src_nsp}.{vid_seq}'));" diff --git a/store/postgres/src/relational_queries.rs b/store/postgres/src/relational_queries.rs index 35353834fc7..6fe8076bd7f 100644 --- a/store/postgres/src/relational_queries.rs +++ b/store/postgres/src/relational_queries.rs @@ -2331,7 +2331,7 @@ impl<'a> InsertQuery<'a> { if table.has_causality_region { count += 1; } - if table.object.strict_vid_order() { + if table.object.has_vid_seq() { count += 1; } for column in table.columns.iter() { @@ -2355,7 +2355,7 @@ impl<'a> QueryFragment for InsertQuery<'a> { let out = &mut out; out.unsafe_to_cache_prepared(); - let strict_vid_order = self.table.object.strict_vid_order(); + let strict_vid_order = self.table.object.has_vid_seq(); // Construct a query // insert into schema.table(column, ...) @@ -4805,7 +4805,7 @@ impl<'a> QueryFragment for CopyEntityBatchQuery<'a> { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { out.unsafe_to_cache_prepared(); - let strict_vid_order = self.src.object.strict_vid_order(); + let strict_vid_order = self.src.object.has_vid_seq(); // Construct a query // insert into {dst}({columns}) From e8ef14d713556a19b95e660b9984b6154908ccbc Mon Sep 17 00:00:00 2001 From: Zoran Cvetkov Date: Wed, 12 Feb 2025 17:59:11 +0200 Subject: [PATCH 3/4] not to duplicate VID --- graph/src/data/store/mod.rs | 2 ++ store/postgres/src/relational/dsl.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/graph/src/data/store/mod.rs b/graph/src/data/store/mod.rs index 2a4da6ae526..557dc5ad28e 100644 --- a/graph/src/data/store/mod.rs +++ b/graph/src/data/store/mod.rs @@ -735,6 +735,8 @@ where lazy_static! { /// The name of the id attribute, `"id"` pub static ref ID: Word = Word::from("id"); + /// The name of the vid attribute, `"vid"` + pub static ref VID: Word = Word::from("vid"); } /// An entity is represented as a map of attribute names to values. diff --git a/store/postgres/src/relational/dsl.rs b/store/postgres/src/relational/dsl.rs index feb615b4ac4..e804a4d06ca 100644 --- a/store/postgres/src/relational/dsl.rs +++ b/store/postgres/src/relational/dsl.rs @@ -22,7 +22,7 @@ use diesel::sql_types::{ use diesel::{AppearsOnTable, Expression, QueryDsl, QueryResult, SelectableExpression}; use diesel_dynamic_schema::DynamicSelectClause; use graph::components::store::{AttributeNames, BlockNumber, StoreError, BLOCK_NUMBER_MAX}; -use graph::data::store::{Id, IdType, ID}; +use graph::data::store::{Id, IdType, ID, VID}; use graph::data_source::CausalityRegion; use graph::prelude::{lazy_static, ENV_VARS}; @@ -260,7 +260,10 @@ impl<'a> Table<'a> { AttributeNames::Select(names) => { let pk = self.meta.primary_key(); cols.push(pk); - let mut names: Vec<_> = names.iter().filter(|name| *name != &*ID).collect(); + let mut names: Vec<_> = names + .iter() + .filter(|name| *name != &*ID && *name != &*VID) + .collect(); names.sort(); for name in names { let column = self.meta.column_for_field(&name)?; @@ -283,6 +286,7 @@ impl<'a> Table<'a> { } cols.push(&*VID_COL); + if T::WITH_SYSTEM_COLUMNS { if self.meta.immutable { cols.push(&*BLOCK_COL); From cc0ecea499ca97947b28283d7d74cf2d93d096fb Mon Sep 17 00:00:00 2001 From: Zoran Cvetkov Date: Thu, 13 Feb 2025 18:22:10 +0200 Subject: [PATCH 4/4] renames --- store/postgres/src/relational_queries.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/store/postgres/src/relational_queries.rs b/store/postgres/src/relational_queries.rs index 6fe8076bd7f..f4b55e89150 100644 --- a/store/postgres/src/relational_queries.rs +++ b/store/postgres/src/relational_queries.rs @@ -2355,7 +2355,7 @@ impl<'a> QueryFragment for InsertQuery<'a> { let out = &mut out; out.unsafe_to_cache_prepared(); - let strict_vid_order = self.table.object.has_vid_seq(); + let has_vid_seq = self.table.object.has_vid_seq(); // Construct a query // insert into schema.table(column, ...) @@ -2382,7 +2382,7 @@ impl<'a> QueryFragment for InsertQuery<'a> { out.push_sql(CAUSALITY_REGION_COLUMN); }; - if strict_vid_order { + if has_vid_seq { out.push_sql(", vid"); } out.push_sql(") values\n"); @@ -2402,7 +2402,7 @@ impl<'a> QueryFragment for InsertQuery<'a> { out.push_sql(", "); out.push_bind_param::(&row.causality_region)?; }; - if strict_vid_order { + if has_vid_seq { out.push_sql(", "); out.push_bind_param::(&row.vid)?; } @@ -4805,7 +4805,7 @@ impl<'a> QueryFragment for CopyEntityBatchQuery<'a> { fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, Pg>) -> QueryResult<()> { out.unsafe_to_cache_prepared(); - let strict_vid_order = self.src.object.has_vid_seq(); + let has_vid_seq = self.src.object.has_vid_seq(); // Construct a query // insert into {dst}({columns}) @@ -4827,7 +4827,7 @@ impl<'a> QueryFragment for CopyEntityBatchQuery<'a> { out.push_sql(", "); out.push_sql(CAUSALITY_REGION_COLUMN); }; - if strict_vid_order { + if has_vid_seq { out.push_sql(", vid"); } @@ -4895,7 +4895,7 @@ impl<'a> QueryFragment for CopyEntityBatchQuery<'a> { )); } } - if strict_vid_order { + if has_vid_seq { out.push_sql(", vid"); }