Skip to content
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

Remove the pg_sql_graph_magic!() macro. #1591

Conversation

eeeebbbbrrrr
Copy link
Contributor

@eeeebbbbrrrr eeeebbbbrrrr commented Feb 29, 2024

The pg_sql_graph_magic!() did some shenanigans related to loading the extension ".control" file at compile-time and embedded it in the compiled artifact. This was convoluted and actually unnecessary as the location where that information was used (in schema.rs) can easily access the ".control" file at runtime.

This PR removes that macro and its usages.

The fn __pgrx__marker() function is still required, however. It's necessary to ensure that the rustc build we do for the pgrx_embed binary for schema generation properly links so that the other (dynamically referenced) __pgrx_internals_XXX symbols can be found. The function, however, is now a noop.

This is related to #1546, but is not a solution. It should just make solving that request easier.

The `pg_sql_graph_magic!()` did some shenanigans related to loading the extension ".control" file at compile-time and embedded it in the compiled artifact.  This was convoluted and actually unnecessary as the location where that information was used (in `schema.rs`) can easily
access the ".control" file at runtime.

This PR removes that macro and its usages.

The `fn __pgrx__marker()` function is still required, however.  It's necessary to ensure that the `rustc` build we do for the `pgrx_embed` binary for schema generation properly links so that the other (dynamically referenced) `__pgrx_internals_XXX` symbols can be found.  The function, however, is now a noop.
@eeeebbbbrrrr eeeebbbbrrrr marked this pull request as draft February 29, 2024 17:53
@workingjubilee
Copy link
Member

For the audience's benefit: This cannot be landed as-is because it breaks cargo pgrx schema, which is somewhat necessary for diagnostic purposes.

… be in the ".control" file, we need to pluck the version number out of the manifest and pass it through to the `pgrx_embed` binary as an envvar.

Also add a CI test for running `cargo pgrx schema`, which is how I manually discovered this problem.
@eeeebbbbrrrr
Copy link
Contributor Author

For the audience's benefit: This cannot be landed as-is because it breaks cargo pgrx schema, which is somewhat necessary for diagnostic purposes.

ee63bf9 fixes that. ;)

@eeeebbbbrrrr eeeebbbbrrrr marked this pull request as ready for review February 29, 2024 18:53
@eeeebbbbrrrr
Copy link
Contributor Author

It’s green!

@workingjubilee
Copy link
Member

Holy shit!

Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

Just some comments for future reference if I ever have to come back to this PR. It looks good! Moving build complexity out of the library is great.

Comment on lines +75 to +83
// endeavor to not require external values if they're not used by the input
if input.contains(CARGO_VERSION) {
input = input.replace(
CARGO_VERSION,
&std::env::var("CARGO_PKG_VERSION").map_err(|_| {
ControlFileError::MissingEnvvar("CARGO_PKG_VERSION".to_string())
})?,
);
}
Copy link
Member

Choose a reason for hiding this comment

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

This does introduce a slight behavior divergence, but that seems fine, since the main validation effort is done via cargo-pgrx.

Comment on lines +407 to +409
// call the marker. Primarily this ensures that rustc will actually link to the library
// during the "pgrx_embed" build initiated by `cargo-pgrx schema` generation
#lib_name_ident::__pgrx_marker();
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps we should use pub extern "C" fn on this marker? rustc is more reluctant to play around with obviously-for-external-consumption interfaces. It's not something we need to fix now, however, just musing aloud.

let mut entities = Vec::new();
let control_file_entity = ::pgrx::pgrx_sql_entity_graph::SqlGraphEntity::ExtensionRoot(::#lib_name_ident::__pgrx_marker());
let control_file_path = std::path::PathBuf::from(#control_file_path);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if there's some cursed set of cases that results in this being a malformed UTF-8 string? ...but if someone seriously asks for supporting non-UTF-8 paths in our greenfield extension build system, I'm probably going to tell them exactly where they can shove that request. Maybe we should be simplifying our code by using camino.

Comment on lines +64 to +65
/// # // arrays.control chosen because it does **NOT** use the @CARGO_VERSION@ variable
/// let context = include_str!("../../pgrx-examples/arrays/arrays.control");
Copy link
Member

Choose a reason for hiding this comment

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

Yeah this seems fair. We have other tests to guarantee this works.

Comment on lines -271 to -293
#[macro_export]
macro_rules! pg_sql_graph_magic {
() => {
// A marker which must exist in the root of the extension.
#[doc(hidden)]
pub fn __pgrx_marker() -> $crate::pgrx_sql_entity_graph::ControlFile {
use ::core::convert::TryFrom;
let package_version = env!("CARGO_PKG_VERSION");
let context = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/",
env!("CARGO_CRATE_NAME"),
".control"
))
.replace("@CARGO_VERSION@", package_version);

let control_file =
$crate::pgrx_sql_entity_graph::ControlFile::try_from(context.as_str())
.expect("Could not parse control file, is it valid?");
control_file
}
};
}
Copy link
Member

Choose a reason for hiding this comment

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

Excellent.

@workingjubilee workingjubilee merged commit 5f0db58 into pgcentralfoundation:develop Feb 29, 2024
11 checks passed
workingjubilee added a commit that referenced this pull request Mar 1, 2024
Welcome to pgrx 0.12.0-alpha.1!

Say the magic words with me!

```shell
cargo install cargo-pgrx --locked --version 0.12.0-alpha.1
```

# Breaking Changes

## No more dlopen!

Perhaps the most exciting change this round is @usamoi's contribution in
#1468 which means that
we no longer perform a `dlopen` in order to generate the schema. The
cost, such as it is, is that your pgrx extensions now require a
`src/bin/pgrx_embed.rs`, which will be used to generate the schema. This
has much less cross-platform issues and will enable supporting things
like `cargo binstall` down the line.

It may be a bit touchy on first-time setup for transitioning older
repos. If necessary, you may have to directly add a
`src/bin/pgrx_embed.rs` and add the following code (which should be the
only code in the file, though you can add comments if you like?):

```rust
::pgrx::pgrx_embed!();
```

Your Cargo.toml will also want to update its crate-type key for the
library:
```toml
[lib]
crate-type = ["cdylib", "lib"]
```

## Library Code

- pgrx-pg-sys will now use `ManuallyDropUnion` thanks to @NotGyro in
#1547
- VARHDRSZ `const`s are no longer `fn`, thanks to @workingjubilee in
#1584
- We no longer have `Interval::is_finite` since
#1594
- We translate more `*_tree_walker` functions to the same signature
their `*_impl` version in Postgres 16 has:
#1596
- Thanks to @eeeebbbbrrrr in
#1591 we no longer have
the `pg_sql_graph_magic!()` macro, which should help with more things in
the future!

# What's New

We have quite a lot of useful additions to our API:

- `SpiClient::prepare_mut` was added thanks to @XeniaLu in
#1275
- @usamoi also contributed bindings subscripting code in
#1562
- For `#[pg_test]`, you have been able to use `#[should_panic(expected =
"string")]` to anticipate a panic that contains that string in that
test. For various reasons, `#[pg_test(error = "string")]` is much the
same. Now, you can also use `#[pg_test(expected = "string")]`, in the
hopes that is easier to stumble across, as of
#1570

## `Result<composite_type!("..."), E>` support

- In #1560 @NotGyro
contributed support for using `Result<composite_type!("Name"), E>`, as a
case that had not been handled before.

## Significantly expanded docs
Thanks to @rjuju, @NotGyro, and @workingjubilee, we now have
significantly expanded docs for cargo-pgrx and pgrx in general. Some of
these are in the API docs on https://docs.rs or the READMEs, but there's
also a guide, now! It's not currently published, but is available as an
[mdbook](https://github.com/rust-lang/mdBook) in the repo.

Some diagnostic information that is also arguably documentation, like
comments and the suggestion to `cargo install`, have also been improved,
thanks to @workingjubilee in
- #1579
- #1573

## `#[pg_cast]`

An experimental macro for a `CREATE CAST` was contributed by @xwkuang5
in #1445!

## Legal Stuff

Thanks to @the-kenny in
#1490 and
@workingjubilee in
#1504, it was brought to
our attention that some dependencies had unusual legal requirements. So
we fixed this with CI! We now check our code included into pgrx-using
binaries is MIT/Apache 2.0 licensed, as is common across crates.io,
using `cargo deny`!. The build tools will have more flexible legal
requirements (partly due to the use of Mozilla Public License code in
rustls).

# Internal Changes
Many internal cleanups were done thanks to
- @workingjubilee in too many PRs to count!
- @thomcc found a needless condition in
#1501
- @nyurik in too many PRs to count!

In particular:
- we now actually `pfree` our `Array`s we detoasted as-of
#1571
- creating a `RawArray` is now low-overhead due to
#1587

## Soundness Fixes
We had a number of soundness issues uncovered or have added more tests
to catch them.
- Bounds-checking debug assertions for array access by @NotGyro in
#1514
- Fix unsound `&` and `&mut` in `fcinfo.rs` by @workingjubilee in
#1595

## Less Deps
Part of the cleanup by @workingjubilee was reducing the number of deps
we compile:
* cargo-pgrx: reduce trivial dep usages in
#1499
* Update 2 syn in #1557

Hopefully it will reduce compile time and disk usage!

## New Contributors
* @the-kenny made their first contribution in
#1490
* @xwkuang5 made their first contribution in
#1445
* @rjuju made their first contribution in
#1516
* @nyurik made their first contribution in
#1533
* @NotGyro made their first contribution in
#1514
* @XeniaLu made their first contribution in
#1275

**Full Changelog**:
v0.12.0-alpha.0...v0.12.0-alpha.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants