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

cargo pgx schema does CREATE OR REPLACE FUNCTION and it probably shouldn't #506

Closed
eeeebbbbrrrr opened this issue Mar 25, 2022 · 5 comments

Comments

@eeeebbbbrrrr
Copy link
Contributor

After some thought, I think our practice of doing CREATE OR REPLACE FUNCTION is incorrect and potentially destructive to a database that might already have an existing foo() function.

We should simply do CREATE FUNCTION, but perhaps we can have an attribute value #[pg_extern(or_replace)] for when the developer thinks they know what they're doing.

@svenklemm
Copy link

In addition to the potentially destructive behaviour using CREATE OR REPLACE FUNCTION in an installation script is also a security vulnerability.

See https://www.postgresql.org/docs/14/extend-extensions.html#EXTEND-EXTENSIONS-SECURITY-SCRIPTS

@JamesGuthrie
Copy link
Contributor

Note that the same security vulnerabilities arise with CREATE ... IF NOT EXISTS.

@workingjubilee
Copy link
Member

Considering the rather sternly worded recommendation to not use CREATE OR REPLACE FUNCTION for functions, and instead use CREATE FUNCTION, what use cases are actually foreseen for the CREATE OR REPLACE example?

@JamesGuthrie
Copy link
Contributor

In general (for SQL functions, not C functions) it's used to update the body of an existing function. If you know that you CREATEd the function, and are the owner of it, then using CREATE OR REPLACE on the same function is safe.

We (the Promscale team at Timescale) have built mechanisms around the way that pgx generates functions in order for it to still be safe, so we would like to continue being able to use CREATE OR REPLACE. We are combining a lot of hand-written SQL with pgx's generated SQL.

For additional context, what we do is the following:

  • mark extension as non-relocatable, and trusted, tell it to be installed in public schema
  • put all functions into a custom namespace (_prom_ext)
  • create the _prom_ext schema (in a bootstrap SQL script which runs only once)
  • use pgx's CREATE OR REPLACE without worry, because we know they're being created into a schema that we created and own

I hope that helps somehow.

@eeeebbbbrrrr
Copy link
Contributor Author

In general (for SQL functions, not C functions) it's used to update the body of an existing function. If you know that you CREATEd the function, and are the owner of it, then using CREATE OR REPLACE on the same function is safe.

I would say for extension upgrade scripts it's fine to use CREATE OR REPLACE, sure. However, pgx doesn't (yet) know how to generate those.

Since we only know how to generate an "initial creation" script, we should stick to CREATE only. Down the road when pgx learns how to make upgrade scripts we can introduce CREATE OR REPLACE where it makes sense as we can be mostly assured that the extension would own that function during ALTER EXTENSION UPDATE.

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

No branches or pull requests

5 participants