Skip to content

Commit a322a63

Browse files
committed
store: Construct vid sequence names in accordance with PG's rules
1 parent 30047e1 commit a322a63

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

store/postgres/src/catalog.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,3 +1002,42 @@ pub(crate) fn histogram_bounds(
10021002
.map(|bounds| bounds.map(|b| b.bounds).unwrap_or_default())
10031003
.map_err(StoreError::from)
10041004
}
1005+
1006+
/// Return the name of the sequence that Postgres uses to handle
1007+
/// auto-incrementing columns. This takes Postgres' way of dealing with long
1008+
/// table and sequence names into account.
1009+
pub(crate) fn seq_name(table_name: &str, column_name: &str) -> String {
1010+
// Postgres limits all identifiers to 63 characters. When it
1011+
// constructs the name of a sequence for a column in a table, it
1012+
// truncates the table name so that appending '_{column}_seq' to
1013+
// it is at most 63 characters
1014+
let len = 63 - (5 + column_name.len());
1015+
let len = len.min(table_name.len());
1016+
format!("{}_{column_name}_seq", &table_name[0..len])
1017+
}
1018+
1019+
#[cfg(test)]
1020+
mod test {
1021+
use super::seq_name;
1022+
1023+
#[test]
1024+
fn seq_name_works() {
1025+
// Pairs of (table_name, vid_seq_name)
1026+
const DATA: &[(&str, &str)] = &[
1027+
("token", "token_vid_seq"),
1028+
(
1029+
"frax_vst_curve_strategy_total_reward_token_collected_event",
1030+
"frax_vst_curve_strategy_total_reward_token_collected_ev_vid_seq",
1031+
),
1032+
(
1033+
"rolling_asset_sent_for_last_24_hours_per_chain_and_token",
1034+
"rolling_asset_sent_for_last_24_hours_per_chain_and_toke_vid_seq",
1035+
),
1036+
];
1037+
1038+
for (tbl, exp) in DATA {
1039+
let act = seq_name(tbl, "vid");
1040+
assert_eq!(exp, &act);
1041+
}
1042+
}
1043+
}

store/postgres/src/relational/prune.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,6 @@ impl TablePair {
215215
let src_nsp = &self.src_nsp;
216216
let dst_nsp = &self.dst_nsp;
217217

218-
let vid_seq = format!("{}_{VID_COLUMN}_seq", self.src.name);
219-
220218
let mut query = String::new();
221219

222220
// What we are about to do would get blocked by autovacuum on our
@@ -229,6 +227,8 @@ impl TablePair {
229227
// Make sure the vid sequence continues from where it was in case
230228
// that we use autoincrementing order of the DB
231229
if !self.src.object.has_vid_seq() {
230+
let vid_seq = catalog::seq_name(&self.src.name, VID_COLUMN);
231+
232232
writeln!(
233233
query,
234234
"select setval('{dst_nsp}.{vid_seq}', nextval('{src_nsp}.{vid_seq}'));"

0 commit comments

Comments
 (0)