-
Notifications
You must be signed in to change notification settings - Fork 67
Hooking into partition management process #22
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
Comments
Hi @1803, Thanks for your feedback! We could possibly add two columns to 'pathman_config' to store regprocedures (basically, function Oids) to be called on partition construction & destruction. For example, one could create plpgsql functions: custom_init(relid regclass);
custom_fini(relid regclass); which would be called whenever partition is attached (constructed) or detached(destructed). Would that suit your case? |
That sounds great. Is it possible to make some variables visible inside those functions? |
I think we could pass all available parameters directly to a callback. It would be fairly easy to implement a callback system with a fixed function signature. Could you provide us with a full list of parameters you would like to be able to use? |
Alternatively we could create a custom type containing everything you might want to observe (kinda reflection), but it would definitely take more time. |
I honestly don't see a need in a new type. In my implementation I just passed some variables as arguments to my custom function as I saw fit. Custom type makes it easier to upgrade to a newer version with more parameters but I honestly don't see many more parameters that are of any use here, so your initial proposal seems good enough. On a quick glance, here's a list of variables:
Should be enough, since these two seem to identify a record in Child info:
|
That sounds reasonable, thanks! We're going to release soon, so this will have to wait. In meantime you're welcome to propose any new ideas (regarding the implementation details) here. |
This feature has been added to our roadmap. |
Hi, I've added support for callbacks on partition creation. It is still in development and probably interface may change slightly but the main idea is the same as described above. It only works for RANGE partitioned relation so far since HASH partitioning doesn't allow to add/remove partitions. So there is a new function set_callback() which enables you to specify your own callback function for handling partition creation events. Your callback function must meet certain requirements particularly:
We decided to use jsonb object to pass all necessary parameters as it allows us to pass additional info in future without breaking existing user callback functions. Here is a simple example: create table log(id serial, message text);
create function abc_on_partition_created_callback(args jsonb)
returns void as
$$
begin
insert into log (message) values (args);
end
$$ language plpgsql;
create table abc(a serial, b int);
select create_range_partitions('abc', 'a', 1, 100, 3);
select set_callback('abc', 'abc_on_partition_created_callback');
insert into abc (a) values (350);
select * from log;
id | message
----+-----------------------------------------------------------------------------------------------------
1 | {"end": 401, "start": 301, "parent": 183672, "part_type": 2, "partition": 183693, "value_type": 23}
(1 row) This feature wasn't merged into master yet. But you may try it from ps: I'll be out of town for the next two weeks and won't have a laptop with me. So if you'll have questions or suggestions I'll answer as soon as I get back. |
This is great. Thanks a lot. |
Split invokes callback, but merge doesn't since it doesn't create a new partition. This is interesting question should the merge function trigger the callback? And should there be a callback on partitions removed? |
Lets assume we merge two tables That being said, I'm perfectly fine with |
I've been testing this new feature for a while now and didn't notice any problems so far. However there was a problem with error handling in the callback function. Whenever there's an exception inside the callback all I get is an error message:
I tried to work this around by wrapping the code of my function with exception block that outputs the error with Other than that it seems to work just fine. Thank you very much for your hard work! |
This is due to the fact that sometimes new partitions may be spawned using the special background worker (which acts as a separate session), and currently we don't transmit exceptions to the parent session that started the worker. It's better to write exception messages to server log. This behavior likely will change. |
We're assembling all of new features in branch |
I tried to go deeper and implement some typical, although very simplified, use-case. Feel free to skip the writing below and proceed to Conclusion section. For the sake of this study lets assume we have legacy software that cannot be changed and we need to make sure it won't screw up our database. Set up: We have a simple table
And we want old values to be logged in a table like this:
To prevalidate the date we'd use a before-insert trigger that would do the trick for us.
and to log values changed we make this other trigger:
Voilà, we're ready to deal with legacy software. Issues I encountered implementing same case trying to partition
We also need to attach our logging trigger to child-tables, since we want to keep our logging;
We have to also run a query that would populate the trigger to already created partitions. Conclusions/TL;DR:
|
Actually there's a way: you just have to call |
@funbringer The error I get seems to be related to some missing libraries, but I checked manually and they are all there.
I will keep trying.. |
Hi,
What platform are you building on? We've encountered the same issue on windows. It seems the problem is that |
@zilder, I was indeed building on windows, but I was using mingw not msvc, since msvc requires modifying the source code and I try to avoid that. The error was present in beta, but the release builds just fine.
Does this mean that plans for copy won't be modified and I have to manually choose which partition to write into? |
Precisely |
In some cases it is required to perform custom actions when a partition related action is performed, such as populate triggers on partition creation or remove certain triggers (or as in my case, include it into logical replication set) when partition is detached.
It seems though event triggers can solve this issue if used correctly, but I feel like a built in solution would fit here.
The text was updated successfully, but these errors were encountered: