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

Temporary view _role_table_grants not dropping after function call #17

Open
rvkulikov opened this issue Nov 14, 2023 · 5 comments · May be fixed by #20
Open

Temporary view _role_table_grants not dropping after function call #17

rvkulikov opened this issue Nov 14, 2023 · 5 comments · May be fixed by #20

Comments

@rvkulikov
Copy link
Owner

Have to wrap function body in transaction;

@oscarfanchin
Copy link

I have made this modification, that seems to work in my case: I have modified your script so that it has temporary tables instead of temporary views, and adding the "ON COMMIT DROP" statement on their creation. Then, I surround my alteration scripts with commits. In this way, it seems to work, albeit being slightly slower than using temporary views.

@AThilenius
Copy link

AThilenius commented Jan 10, 2024

@oscarfanchin Any chance you could share code? My SQL foo is not strong. I can't get past a ERROR: relation "_role_table_grants" already exists error. Wrapping things in transactions doesn't seem to change that outcome.

@AThilenius
Copy link

Unrelated, I love Postgres, but views make me want to take a warm bath with a razor blade. We have to use them for Hasura, and in theory their are a great idea. In practice having to manually drop and recreate an ENTIRE view chain just so I can update a timestamp from timestamp to timestamptz is an infuriating experience. We use Prisma for schema migrations, but they don't yet support views. Even if they did, it would puke out thousands of lines of DDL to drop/create the views every migration...

(╯°□°)╯︵ ┻━┻

@zineanteoh
Copy link

facing the same error as @AThilenius here -- help would be appreciated!

@oraculix
Copy link

oraculix commented Aug 23, 2024

I faced the same issue on Pg 16, even though my alter scripts were encapsulated in a BEGIN; ... COMMIT; block.
If I'm not misinformed, this can be expected within the same session, because temporary views aren't dropped after a transaction but at the end of a session.

Three possible remedies that I see:

  1. Use temp tables instead, as @oscarfanchin suggested. However, view are more lightweight and should be preferred imho.
  2. Use create or replace to work around the issue. I tried this approach first and it worked for me, but it has some overhead, as the whole statement gets parsed and the view created every time.
  3. Emulate an if not exists clause by capturing SQLSTATE '42P07' in an EXCEPTION handler. This seems to be the cleanest and fastest solution to me, so I stuck with this one.

I sent both 2) and 3) as a Pull Request with separate commits. Choose your poison. ;-)

BTW, thanks for sharing your work, @rvkulikov!

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 a pull request may close this issue.

5 participants