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

fix: remove foreign key, make trigger handle deletes #485

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Askir
Copy link
Contributor

@Askir Askir commented Feb 14, 2025

No description provided.

@Askir Askir force-pushed the jascha/ai-459-possible-bug-in-event-trigger branch 2 times, most recently from 2f3cf22 to b4ccdc1 Compare February 15, 2025 01:34
@Askir Askir force-pushed the jascha/ai-459-possible-bug-in-event-trigger branch from 3e3b196 to 1293d44 Compare February 20, 2025 21:07
@Askir Askir marked this pull request as ready for review February 20, 2025 22:27
@Askir Askir requested a review from a team as a code owner February 20, 2025 22:27
Copy link
Collaborator

@jgpruitt jgpruitt left a comment

Choose a reason for hiding this comment

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

nice!

Comment on lines +466 to +467
-- _build_vectorizer_trigger_definition
create or replace function ai._build_vectorizer_trigger_definition
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd probably call this ai._vectorizer_build_trigger_definition so as to fit the pattern

Comment on lines +510 to +516
if (TG_OP = 'DELETE') then
execute '$DELETE_STATEMENT$' using old;
return old;
elsif (TG_OP = 'UPDATE') then
if $PK_CHANGE_CHECK$ then
execute '$DELETE_STATEMENT$' using old;
end if;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a reason for using EXECUTE here instead of a "plain" DELETE statement like the INSERT statement below?

Comment on lines +494 to +495
quote_ident(attname) operator(pg_catalog.||) ' = $1.' operator(pg_catalog.||) quote_ident(attname),
' and '
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you prefer using the concatenate operator instead of the format function? It's a matter of taste, but I find format a little easier to read and understand what the output is supposed to look like. But this works

insert into $QUEUE_SCHEMA$.$QUEUE_TABLE$ ($PK_COLUMNS$)
values ($PK_VALUES$);
return new;
else
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's go ahead and handle when TG_OP = 'TRUNCATE'. We can just truncate the queue and the target.

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm. Truncate may require a separate trigger. I'm not sure if you can do a for each row trigger on a truncate op. If it requires a separate trigger, I guess let's skip it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

oh. i bet you could have two triggers with one trigger function.

Comment on lines +530 to +535
_func_def := replace(_func_def, '$DELETE_STATEMENT$', _delete_statement);
_func_def := replace(_func_def, '$PK_CHANGE_CHECK$', _pk_change_check);
_func_def := replace(_func_def, '$QUEUE_SCHEMA$', quote_ident(queue_schema));
_func_def := replace(_func_def, '$QUEUE_TABLE$', quote_ident(queue_table));
_func_def := replace(_func_def, '$PK_COLUMNS$', _pk_columns);
_func_def := replace(_func_def, '$PK_VALUES$', _pk_values);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm guessing you chose this over format because format doesn't have named fields? It does have positional fields, but that's still not ideal.

Comment on lines +651 to +653
update ai.vectorizer
set config = jsonb_set(config, '{version}', format('"%s"', _new_version)::jsonb)
where id = _vec.id;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to make sure we don't have any other config upgrades in this release or this may cause the other(s) to fail.

do $upgrade_block$
declare
_vec record;
_version text;
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove unused variable

Comment on lines +602 to +604
_owner name;
_acl aclitem[];
_debug_acl aclitem[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove unused variables

Comment on lines +625 to +628
execute format(
'alter extension ai add function ai.%I()',
_vec.trigger_name
);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you verify that adding it to the extension does not change the ownership of the function?

I probably would have altered the function to switch owner and then switch back, but this seems like it might be better or at least equally good.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nice!

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