Skip to content

Postgres: ALTER TABLE SET ( storage_parameters ) #1947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ pub enum AlterTableOperation {
ValidateConstraint {
name: Ident,
},
/// `SET ( storage_parameter [= value] [, ... ] )`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we include a link to the docs where this syntax comes from?

Also I'm wondering whether storageparameter is right terminology here? From what I can tell with the syntax it looks possible that it accepts arbitrary sql option in parenthesis? If so thinking something generic like SetOptionsParens { options: Vec<SqlOption> } or similar naming as the enum variant

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so Postgres does expect one of a specific set of parameters there: https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS

and will give ERROR: unrecognized parameter for an unexpected parameter. The parameters also have been the same in the last 5 versions, so they seem relatively stable.

Happy to either change to the more generic form, or update the parsing to look for the specific storage parameters instead of arbitrary options. Whatever you think is more appropriate @iffyio

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, indeed I think the generic form would be preferred beyond the postgres dialect in this case

///
/// Note: this is a PostgreSQL-specific operation.
/// Please refer to [PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-altertable.html)
SetOptionsParens {
options: Vec<SqlOption>,
},
}

/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
Expand Down Expand Up @@ -791,6 +798,9 @@ impl fmt::Display for AlterTableOperation {
AlterTableOperation::ValidateConstraint { name } => {
write!(f, "VALIDATE CONSTRAINT {name}")
}
AlterTableOperation::SetOptionsParens { options } => {
write!(f, "SET ({})", display_comma_separated(options))
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,9 @@ impl Spanned for AlterTableOperation {
AlterTableOperation::Lock { .. } => Span::empty(),
AlterTableOperation::ReplicaIdentity { .. } => Span::empty(),
AlterTableOperation::ValidateConstraint { name } => name.span,
AlterTableOperation::SetOptionsParens { options } => {
union_spans(options.iter().map(|i| i.span()))
}
}
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8938,17 +8938,22 @@ impl<'a> Parser<'a> {
let name = self.parse_identifier()?;
AlterTableOperation::ValidateConstraint { name }
} else {
let options: Vec<SqlOption> =
let mut options =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
if !options.is_empty() {
AlterTableOperation::SetTblProperties {
table_properties: options,
}
} else {
return self.expected(
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, or SET TBLPROPERTIES after ALTER TABLE",
options = self.parse_options(Keyword::SET)?;
if !options.is_empty() {
AlterTableOperation::SetOptionsParens { options }
} else {
return self.expected(
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, SET, or SET TBLPROPERTIES after ALTER TABLE",
self.peek_token(),
);
);
}
}
};
Ok(operation)
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4725,6 +4725,34 @@ fn parse_alter_table() {
}
_ => unreachable!(),
}

let set_storage_parameters = "ALTER TABLE tab SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_threshold = 500)";
match alter_table_op(verified_stmt(set_storage_parameters)) {
AlterTableOperation::SetOptionsParens { options } => {
assert_eq!(
options,
[
SqlOption::KeyValue {
key: Ident {
value: "autovacuum_vacuum_scale_factor".to_string(),
quote_style: None,
span: Span::empty(),
},
value: Expr::Value(test_utils::number("0.01").with_empty_span()),
},
SqlOption::KeyValue {
key: Ident {
value: "autovacuum_vacuum_threshold".to_string(),
quote_style: None,
span: Span::empty(),
},
value: Expr::Value(test_utils::number("500").with_empty_span()),
}
],
);
}
_ => unreachable!(),
}
}

#[test]
Expand Down