From 9588afec281960d6c1bba968ba45711e18236ec2 Mon Sep 17 00:00:00 2001 From: liuxiaoyu Date: Thu, 17 Aug 2023 14:32:17 +0800 Subject: [PATCH 1/5] Adjust the method of loading the external preloaded shared libraries External lib is preloaded. Need to write to theprocess_shared_preload_libraries.h header file. This loads the external lib through the header file. --- src/backend/utils/init/miscinit.c | 27 +++++++++++++++++++ .../utils/process_shared_preload_libraries.h | 0 2 files changed, 27 insertions(+) create mode 100644 src/include/utils/process_shared_preload_libraries.h diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index a4214ad0662..e2bdb1858f0 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -76,6 +76,8 @@ static List *lock_files = NIL; static Latch LocalLatchData; +void load_libraries_array(void); + /* ---------------------------------------------------------------- * ignoring system indexes support stuff * @@ -1761,6 +1763,29 @@ load_libraries(const char *libraries, const char *gucname, bool restricted) pfree(rawstring); } +/* + * process shared preload libraries array. + */ +static const char *process_shared_preload_libraries_array[] = +{ + #include "utils/process_shared_preload_libraries.h" +}; + +/* + * preload load external libraries. + */ +void +load_libraries_array(void) +{ + int shared_preload_libraries_num = sizeof(process_shared_preload_libraries_array) / sizeof(char *); + for (int i = 0; i < shared_preload_libraries_num; i++) + { + load_libraries(process_shared_preload_libraries_array[i], + "shared_preload_libraries", + false); + } +} + /* * process any libraries that should be preloaded at postmaster start */ @@ -1779,6 +1804,8 @@ process_shared_preload_libraries(void) false); #endif + load_libraries_array(); + process_shared_preload_libraries_in_progress = false; } diff --git a/src/include/utils/process_shared_preload_libraries.h b/src/include/utils/process_shared_preload_libraries.h new file mode 100644 index 00000000000..e69de29bb2d From 477c23cbb41364e419fc4fdc580b200aa5745904 Mon Sep 17 00:00:00 2001 From: MisterRaindrop Date: Fri, 25 Aug 2023 20:13:37 +0800 Subject: [PATCH 2/5] expand shared_preload_libraries_string Extended String. To load the dynamic library is deduplicate --- src/backend/nodes/list.c | 52 ++++++++++++++ src/backend/utils/init/miscinit.c | 111 ++++++++++++++++++++++-------- src/include/nodes/pg_list.h | 2 + 3 files changed, 137 insertions(+), 28 deletions(-) diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c index 12847e35ea3..f9d5f2b51c3 100644 --- a/src/backend/nodes/list.c +++ b/src/backend/nodes/list.c @@ -1424,6 +1424,42 @@ list_deduplicate_oid(List *list) check_list_invariants(list); } +/* + * Remove adjacent duplicates in a list of String. + * + * It is caller's responsibility to have sorted the list to bring duplicates + * together, perhaps via list_sort(list, list_string_cmp). + */ +List* +list_deduplicate_string(List *list) +{ + int len; + List *result = NIL; + ListCell *l; + len = list_length(list); + if (len == 0) + return result; + + l = &list->elements[0]; + result = lappend(result, pstrdup(l->ptr_value)); + + if (len > 1) + { + ListCell *elements = list->elements; + int i = 0; + for (int j = 1; j < len; j++) + { + if (strncmp(elements[i].ptr_value, elements[j].ptr_value, + Max(strlen(elements[i].ptr_value), strlen(elements[j].ptr_value))) != 0) + { + result = lappend(result, pstrdup(elements[j].ptr_value)); + } + ++i; + } + } + return result; +} + /* * Free all storage in a list, and optionally the pointed-to elements */ @@ -1603,3 +1639,19 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2) return 1; return 0; } + +/* + * list_sort comparator for sorting a list into ascending string order. + */ +int +list_string_cmp(const ListCell *p1, const ListCell *p2) +{ + char *v1 = lfirst(p1); + char *v2 = lfirst(p2); + int result = strncmp(v1, v2, Max(strlen(v1), strlen(v2))); + if (result < 0) + return -1; + if (result > 0) + return 1; + return 0; +} diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index e2bdb1858f0..db78923789e 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -76,8 +76,6 @@ static List *lock_files = NIL; static Latch LocalLatchData; -void load_libraries_array(void); - /* ---------------------------------------------------------------- * ignoring system indexes support stuff * @@ -1708,6 +1706,88 @@ char *local_preload_libraries_string = NULL; /* Flag telling that we are loading shared_preload_libraries */ bool process_shared_preload_libraries_in_progress = false; +/* + * process shared preload libraries array. + */ +static const char *process_shared_preload_libraries_array[] = +{ + #include "utils/process_shared_preload_libraries.h" +}; + +/* + * expand preload load libraries string. + */ +static char* +expand_shared_preload_libraries_string() +{ + List *elemlist = NIL; + List *deduplicate_elemlist = NIL; + ListCell *l; + char *rawstring; + char *libraries = shared_preload_libraries_string; + + /* Need a modifiable copy of string */ + rawstring = pstrdup(libraries); + if (libraries != NULL && libraries[0] != '\0') + { + /* Parse string into list of filename paths */ + if (!SplitDirectoriesString(rawstring, ',', &elemlist)) + { + /* syntax error in list */ + list_free_deep(elemlist); + pfree(rawstring); + ereport(LOG, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("invalid list syntax in parameter \"%s\"", + libraries))); + return NULL; + } + + } + + /* load expand libraries */ + int shared_preload_libraries_num = sizeof(process_shared_preload_libraries_array) / sizeof(char *); + if (shared_preload_libraries_num > 0) + { + for (int i = 0; i < shared_preload_libraries_num; i++) + { + elemlist = lappend(elemlist, pstrdup((char*)process_shared_preload_libraries_array[i])); + } + + } + + if (list_length(elemlist) == 0) + { + list_free_deep(elemlist); + pfree(rawstring); + return NULL; + } + + + /* deduplicate list string */ + if (list_length(elemlist) > 1) + { + list_sort(elemlist, list_string_cmp); + deduplicate_elemlist = list_deduplicate_string(elemlist); + } + + /* format string delimiter with ',' */ + StringInfoData expand_string; + initStringInfo(&expand_string); + for (int i = 0; i < list_length(deduplicate_elemlist); i++) + { + l = &deduplicate_elemlist->elements[i]; + if (i == 0) + appendStringInfo(&expand_string, "%s", (char*)lfirst(l)); + else + appendStringInfo(&expand_string, ",%s", (char*)lfirst(l)); + } + list_free_deep(elemlist); + list_free_deep(deduplicate_elemlist); + pfree(rawstring); + return expand_string.data; +} + /* * load the shared libraries listed in 'libraries' * @@ -1763,29 +1843,6 @@ load_libraries(const char *libraries, const char *gucname, bool restricted) pfree(rawstring); } -/* - * process shared preload libraries array. - */ -static const char *process_shared_preload_libraries_array[] = -{ - #include "utils/process_shared_preload_libraries.h" -}; - -/* - * preload load external libraries. - */ -void -load_libraries_array(void) -{ - int shared_preload_libraries_num = sizeof(process_shared_preload_libraries_array) / sizeof(char *); - for (int i = 0; i < shared_preload_libraries_num; i++) - { - load_libraries(process_shared_preload_libraries_array[i], - "shared_preload_libraries", - false); - } -} - /* * process any libraries that should be preloaded at postmaster start */ @@ -1794,7 +1851,7 @@ process_shared_preload_libraries(void) { process_shared_preload_libraries_in_progress = true; - load_libraries(shared_preload_libraries_string, + load_libraries(expand_shared_preload_libraries_string(), "shared_preload_libraries", false); @@ -1804,8 +1861,6 @@ process_shared_preload_libraries(void) false); #endif - load_libraries_array(); - process_shared_preload_libraries_in_progress = false; } diff --git a/src/include/nodes/pg_list.h b/src/include/nodes/pg_list.h index ef4a2c24899..92c333497d4 100644 --- a/src/include/nodes/pg_list.h +++ b/src/include/nodes/pg_list.h @@ -616,6 +616,7 @@ extern pg_nodiscard List *list_concat_unique_int(List *list1, const List *list2) extern pg_nodiscard List *list_concat_unique_oid(List *list1, const List *list2); extern void list_deduplicate_oid(List *list); +extern List* list_deduplicate_string(List *list); extern void list_free(List *list); extern void list_free_deep(List *list); @@ -629,5 +630,6 @@ extern void list_sort(List *list, list_sort_comparator cmp); extern int list_int_cmp(const ListCell *p1, const ListCell *p2); extern int list_oid_cmp(const ListCell *p1, const ListCell *p2); +extern int list_string_cmp(const ListCell *p1, const ListCell *p2); #endif /* PG_LIST_H */ From 2c1c92c10f6c77aeb99e1c86dd3f8e08c6166561 Mon Sep 17 00:00:00 2001 From: MisterRaindrop Date: Tue, 12 Sep 2023 13:51:45 +0800 Subject: [PATCH 3/5] update remove duplicates list function --- src/backend/nodes/list.c | 52 ------------------------------- src/backend/utils/init/miscinit.c | 37 ++++++++++++++++++++-- src/include/nodes/pg_list.h | 2 -- 3 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/backend/nodes/list.c b/src/backend/nodes/list.c index f9d5f2b51c3..12847e35ea3 100644 --- a/src/backend/nodes/list.c +++ b/src/backend/nodes/list.c @@ -1424,42 +1424,6 @@ list_deduplicate_oid(List *list) check_list_invariants(list); } -/* - * Remove adjacent duplicates in a list of String. - * - * It is caller's responsibility to have sorted the list to bring duplicates - * together, perhaps via list_sort(list, list_string_cmp). - */ -List* -list_deduplicate_string(List *list) -{ - int len; - List *result = NIL; - ListCell *l; - len = list_length(list); - if (len == 0) - return result; - - l = &list->elements[0]; - result = lappend(result, pstrdup(l->ptr_value)); - - if (len > 1) - { - ListCell *elements = list->elements; - int i = 0; - for (int j = 1; j < len; j++) - { - if (strncmp(elements[i].ptr_value, elements[j].ptr_value, - Max(strlen(elements[i].ptr_value), strlen(elements[j].ptr_value))) != 0) - { - result = lappend(result, pstrdup(elements[j].ptr_value)); - } - ++i; - } - } - return result; -} - /* * Free all storage in a list, and optionally the pointed-to elements */ @@ -1639,19 +1603,3 @@ list_oid_cmp(const ListCell *p1, const ListCell *p2) return 1; return 0; } - -/* - * list_sort comparator for sorting a list into ascending string order. - */ -int -list_string_cmp(const ListCell *p1, const ListCell *p2) -{ - char *v1 = lfirst(p1); - char *v2 = lfirst(p2); - int result = strncmp(v1, v2, Max(strlen(v1), strlen(v2))); - if (result < 0) - return -1; - if (result > 0) - return 1; - return 0; -} diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index db78923789e..7b482265ff8 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -1714,6 +1714,40 @@ static const char *process_shared_preload_libraries_array[] = #include "utils/process_shared_preload_libraries.h" }; +/* + * remove duplicates list. + */ +static List* +removeDuplicates(List* elemlist) +{ + List* unique_arr = NIL; + int i, j; + ListCell *l; + ListCell *l2; + for (i = 0; i < list_length(elemlist); i++) + { + int found = 0; + l = &elemlist->elements[i]; + char* a = (char*)lfirst(l); + for (j = 0; j < list_length(unique_arr); j++) + { + l2 = &unique_arr->elements[j]; + char* b = (char*)lfirst(l2); + if (strncmp(a, b, Max(strlen(a), strlen(b))) == 0) + { + found = 1; + break; + } + } + + if (!found) + { + unique_arr = lappend(unique_arr, pstrdup(a)); + } + } + return unique_arr; +} + /* * expand preload load libraries string. */ @@ -1767,8 +1801,7 @@ expand_shared_preload_libraries_string() /* deduplicate list string */ if (list_length(elemlist) > 1) { - list_sort(elemlist, list_string_cmp); - deduplicate_elemlist = list_deduplicate_string(elemlist); + deduplicate_elemlist = removeDuplicates(elemlist); } /* format string delimiter with ',' */ diff --git a/src/include/nodes/pg_list.h b/src/include/nodes/pg_list.h index 92c333497d4..ef4a2c24899 100644 --- a/src/include/nodes/pg_list.h +++ b/src/include/nodes/pg_list.h @@ -616,7 +616,6 @@ extern pg_nodiscard List *list_concat_unique_int(List *list1, const List *list2) extern pg_nodiscard List *list_concat_unique_oid(List *list1, const List *list2); extern void list_deduplicate_oid(List *list); -extern List* list_deduplicate_string(List *list); extern void list_free(List *list); extern void list_free_deep(List *list); @@ -630,6 +629,5 @@ extern void list_sort(List *list, list_sort_comparator cmp); extern int list_int_cmp(const ListCell *p1, const ListCell *p2); extern int list_oid_cmp(const ListCell *p1, const ListCell *p2); -extern int list_string_cmp(const ListCell *p1, const ListCell *p2); #endif /* PG_LIST_H */ From 097ee50a58a0824c67e9b67fb8a9dc3d0927ac78 Mon Sep 17 00:00:00 2001 From: MisterRaindrop Date: Tue, 12 Sep 2023 14:07:50 +0800 Subject: [PATCH 4/5] remove duplicates list used strcmp --- src/backend/utils/init/miscinit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 7b482265ff8..74255a7a611 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -1733,7 +1733,7 @@ removeDuplicates(List* elemlist) { l2 = &unique_arr->elements[j]; char* b = (char*)lfirst(l2); - if (strncmp(a, b, Max(strlen(a), strlen(b))) == 0) + if (strcmp(a, b) == 0) { found = 1; break; From 001d812d5d010e0cf6c81b7be8f320227a01b894 Mon Sep 17 00:00:00 2001 From: MisterRaindrop Date: Mon, 9 Oct 2023 17:04:54 +0800 Subject: [PATCH 5/5] adjust code --- src/backend/utils/init/miscinit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c index 01f1a325826..7d13b06c058 100644 --- a/src/backend/utils/init/miscinit.c +++ b/src/backend/utils/init/miscinit.c @@ -1727,7 +1727,7 @@ removeDuplicates(List* elemlist) ListCell *l2; for (i = 0; i < list_length(elemlist); i++) { - int found = 0; + bool found = false; l = &elemlist->elements[i]; char* a = (char*)lfirst(l); for (j = 0; j < list_length(unique_arr); j++) @@ -1736,7 +1736,7 @@ removeDuplicates(List* elemlist) char* b = (char*)lfirst(l2); if (strcmp(a, b) == 0) { - found = 1; + found = true; break; } }