Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write PostgreSQL logic decoding plug-in

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to write PostgreSQL logic decoding plug-in", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to write PostgreSQL logic decoding plug-in"!

1. Take a logical replication plug-in as an example to see how it works:

Create a logical slot and use the test_decoding plug-in by default:

Pg_recvlogical-create-slot-S test_slot-d flying

You can specify other decoding plug-ins as needed, parameter-P,-- plugin=PLUGIN, and more information pg_recvlogical-- help.

Check it out

Flying=# select * from pg_replication_slots Slot_name | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn-+-+- +-test_slot | test_decoding | logical | 12635 | flying | f | f | 926 | 0/F05CA448 | 0/F05CA480

Start logical copy, here print directly to standard output

Pg_recvlogical-start-S test_slot-d flying-f-

Insert a piece of data into table a (col1 int): insert into a values; the receiver starts to output:

Quanzl-mac:bin quanzl$. / pg_recvlogical-- start-S test_slot-d flying-f-BEGIN 926table public.a: INSERT: col1 [integer]: 100COMMIT 926

This plug-in is more of a demonstration function, and it is no problem for someone to parse its results and use it in a production environment. The new version also has a referenced plug-in pgoutput, the source code is in src/backend/replication/pgoutput.

2. by reading and understanding the code, we can know how to implement each part.

(1) set the callback function _ PG_output_plugin_init during initialization

Combined with the documentation, it is easy to understand what the members of the structure OutputPluginCallbacks represent, even without comments.

(2) copy startup parameters

Pg_recvlogical allows you to pass parameters to the plug-in through-o, and you can learn from startup_cb how to define the options we need.

(3), each action decoding, four kinds: begin, change, truncate, commit

In particular, change actions, how to distinguish between INSERT/UPDATE/DELETE, including how to correctly reference object names, identify null and TOAST, identify deleted fields, and so on.

(4) there are also some functions that test_decoding does not use, such as how to find the replication key, which is the primary key by default, or you can set it through the SQL command: ALTER TABLE. REPLICA IDENTITY...

What should I do with this? Open the header file related to this, under include/replication, according to the comments and names, you can find that it is the RelationGetReplicaIndex function.

Thank you for your reading, the above is the content of "how to write PostgreSQL logic decoding plug-in", after the study of this article, I believe you have a deeper understanding of how to write PostgreSQL logic decoding plug-in, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report