-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: auto-generate type alignment (#[pgrx(alignment = "on")])
Postgres allows for types to specify their alignment in the `CREATE TYPE` statement. This change adds the ability to derive Postgres' alignment configuration from the type's Rust alignment (`std::mem::align_of::<T>()`). This functionality is opt-in through the `#[pgrx(alignment = "on")]` attribute: ``` #[derive(PostgresType)] #[pgrx(alignment = "on")] struct AlignedTo8Bytes { v1: u64, v2: [u64; 3] } ```
- Loading branch information
1 parent
7b68a18
commit 71b591e
Showing
6 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc. | ||
//LICENSE | ||
//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <[email protected]> | ||
//LICENSE | ||
//LICENSE All rights reserved. | ||
//LICENSE | ||
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file. | ||
use pgrx::prelude::*; | ||
use serde::*; | ||
|
||
#[derive(PostgresType, Serialize, Deserialize)] | ||
#[pgrx(alignment = "on")] | ||
pub struct AlignedTo4Bytes { | ||
v1: u32, | ||
v2: [u32; 3], | ||
} | ||
|
||
#[derive(PostgresType, Serialize, Deserialize)] | ||
#[pgrx(alignment = "on")] | ||
pub struct AlignedTo8Bytes { | ||
v1: u64, | ||
v2: [u64; 3], | ||
} | ||
|
||
#[derive(PostgresType, Serialize, Deserialize)] | ||
#[pgrx(alignment = "off")] | ||
pub struct NotAlignedTo8Bytes { | ||
v1: u64, | ||
v2: [u64; 3], | ||
} | ||
|
||
#[cfg(any(test, feature = "pg_test"))] | ||
#[pg_schema] | ||
mod tests { | ||
use pgrx::prelude::*; | ||
|
||
#[cfg(not(feature = "no-schema-generation"))] | ||
#[pg_test] | ||
fn test_alignment_is_correct() { | ||
let val = Spi::get_one::<String>(r#"SELECT typalign::text FROM pg_type WHERE typname = 'alignedto4bytes'"#).unwrap().unwrap(); | ||
|
||
assert!(val == "i"); | ||
|
||
let val = Spi::get_one::<String>(r#"SELECT typalign::text FROM pg_type WHERE typname = 'alignedto8bytes'"#).unwrap().unwrap(); | ||
|
||
assert!(val == "d"); | ||
|
||
let val = Spi::get_one::<String>(r#"SELECT typalign::text FROM pg_type WHERE typname = 'notalignedto8bytes'"#).unwrap().unwrap(); | ||
|
||
assert!(val == "i"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters