Skip to content

Commit 064ca4c

Browse files
committed
store: update namespace handling in table creation and index loading
1 parent c4ad728 commit 064ca4c

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

store/postgres/src/relational.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ impl Table {
17151715
pub fn new_like(&self, namespace: &Namespace, name: &SqlName) -> Arc<Table> {
17161716
let other = Table {
17171717
object: self.object.clone(),
1718-
nsp: self.nsp.clone(),
1718+
nsp: namespace.clone(),
17191719
name: name.clone(),
17201720
qualified_name: SqlName::qualified_name(namespace, name),
17211721
columns: self.columns.clone(),

store/postgres/src/relational/ddl.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,13 +403,7 @@ impl Table {
403403
if index_def.is_some() && ENV_VARS.postpone_attribute_index_creation {
404404
let arr = index_def
405405
.unwrap()
406-
.indexes_for_table(
407-
&catalog.site.namespace,
408-
&self.name.to_string(),
409-
&self,
410-
false,
411-
false,
412-
)
406+
.indexes_for_table(&self.nsp, &self.name.to_string(), &self, false, false)
413407
.map_err(|_| fmt::Error)?;
414408
for (_, sql) in arr {
415409
writeln!(out, "{};", sql).expect("properly formated index statements")

store/postgres/src/relational/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ impl CreateIndex {
440440
}
441441
}
442442

443-
fn with_nsp(&self, nsp2: String) -> Result<Self, Error> {
443+
pub fn with_nsp(&self, nsp2: String) -> Result<Self, Error> {
444444
let s = self.clone();
445445
match s {
446446
CreateIndex::Unknown { defn: _ } => Err(anyhow!("Failed to parse the index")),

store/postgres/src/relational/prune.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,47 @@ use crate::{
2323
vid_batcher::{VidBatcher, VidRange},
2424
};
2525

26-
use super::{Catalog, Layout, Namespace};
26+
use super::{
27+
index::{load_indexes_from_table, CreateIndex, IndexList},
28+
Catalog, Layout, Namespace,
29+
};
30+
31+
// Additions to `Table` that are useful for pruning
32+
impl Table {
33+
/// Return the first and last vid of any entity that is visible in the
34+
/// block range from `first_block` (inclusive) to `last_block`
35+
/// (exclusive)
36+
fn vid_range(
37+
&self,
38+
conn: &mut PgConnection,
39+
first_block: BlockNumber,
40+
last_block: BlockNumber,
41+
) -> Result<(i64, i64), StoreError> {
42+
#[derive(QueryableByName)]
43+
struct VidRange {
44+
#[diesel(sql_type = BigInt)]
45+
min_vid: i64,
46+
#[diesel(sql_type = BigInt)]
47+
max_vid: i64,
48+
}
49+
50+
// Determine the last vid that we need to copy
51+
let VidRange { min_vid, max_vid } = sql_query(format!(
52+
"/* controller=prune,first={first_block},last={last_block} */ \
53+
select coalesce(min(vid), 0) as min_vid, \
54+
coalesce(max(vid), -1) as max_vid from {src} \
55+
where lower(block_range) <= $2 \
56+
and coalesce(upper(block_range), 2147483647) > $1 \
57+
and coalesce(upper(block_range), 2147483647) <= $2 \
58+
and block_range && int4range($1, $2)",
59+
src = self.qualified_name,
60+
))
61+
.bind::<Integer, _>(first_block)
62+
.bind::<Integer, _>(last_block)
63+
.get_result::<VidRange>(conn)?;
64+
Ok((min_vid, max_vid))
65+
}
66+
}
2767

2868
/// Utility to copy relevant data out of a source table and into a new
2969
/// destination table and replace the source table with the destination
@@ -59,7 +99,10 @@ impl TablePair {
5999
let mut list = IndexList {
60100
indexes: HashMap::new(),
61101
};
62-
let indexes = load_indexes_from_table(conn, &src, src_nsp.as_str())?;
102+
let indexes = load_indexes_from_table(conn, &src, src_nsp.as_str())?
103+
.into_iter()
104+
.map(|index| index.with_nsp(dst_nsp.to_string()))
105+
.collect::<Result<Vec<CreateIndex>, _>>()?;
63106
list.indexes.insert(src.name.to_string(), indexes);
64107

65108
// In case of pruning we don't do delayed creation of indexes,

0 commit comments

Comments
 (0)