1 /*-------------------------------------------------------------------------
3 * Copyright (c) 2016-2020, ludia_funcs Development Group
4 * Copyright (c) 2006-2015, NTT DATA Corporation
9 * Update Ludia functions so that they are available with PostgreSQL9.1.
10 * Author: NTT DATA Corporation
12 *-------------------------------------------------------------------------
17 #include <sys/types.h>
21 #include "catalog/pg_type.h"
24 #include "ludia_funcs.h"
25 #include "mb/pg_wchar.h"
27 #include "storage/fd.h"
28 #include "utils/builtins.h"
29 #include "utils/guc.h"
30 #include "miscadmin.h"
32 #if PG_VERSION_NUM >= 90300
33 #include "access/htup_details.h"
38 /* Last update date of ludia_funcs */
39 #define PGS2_LAST_UPDATE "2019.10.04"
43 typedef enum pgs2_enable_debug_type
45 PGS2_ENABLE_DEBUG_OFF, /* logs no debug log */
46 PGS2_ENABLE_DEBUG_TERSE, /* logs tersely, e.g., just names of
48 PGS2_ENABLE_DEBUG_ON /* logs detailed infomation */
49 } pgs2_enable_debug_type;
51 /* We accept all the likely variants of "on" and "off" */
52 static const struct config_enum_entry pgs2_enable_debug_options[] = {
53 {"off", PGS2_ENABLE_DEBUG_OFF, false},
54 {"terse", PGS2_ENABLE_DEBUG_TERSE, false},
55 {"on", PGS2_ENABLE_DEBUG_ON, false},
56 {"true", PGS2_ENABLE_DEBUG_ON, true},
57 {"false", PGS2_ENABLE_DEBUG_OFF, true},
58 {"yes", PGS2_ENABLE_DEBUG_ON, true},
59 {"no", PGS2_ENABLE_DEBUG_OFF, true},
60 {"1", PGS2_ENABLE_DEBUG_ON, true},
61 {"0", PGS2_ENABLE_DEBUG_OFF, true},
65 static int pgs2_enable_debug = PGS2_ENABLE_DEBUG_OFF;
66 #endif /* PGS2_DEBUG */
68 static char *pgs2_last_update = NULL;
69 static int norm_cache_limit = -1;
70 static bool escape_snippet_keyword = false;
72 #define SEN_NORMALIZE_FLAGS 0
73 #define SEN_MAX_N_EXPRS 32
75 /* upper limit for GUC variables measured in kilobytes of memory */
76 /* note that various places assume the byte size fits in a "long" variable */
77 #if SIZEOF_SIZE_T > 4 && SIZEOF_LONG > 4
78 #define MAX_KILOBYTES INT_MAX
80 #define MAX_KILOBYTES (INT_MAX / 1024)
83 #define ISBACKSLASHCHAR(x) (*(x) == '\\')
84 #define ISDOUBLEQUOTECHAR(x) (*(x) == '"')
85 #define ISSENNAOPSCHAR(x) (*(x) == '+' || *(x) == '-' || *(x) == ' ')
87 PG_FUNCTION_INFO_V1(pgs2snippet1);
88 PG_FUNCTION_INFO_V1(pgs2norm);
89 PG_FUNCTION_INFO_V1(pgs2textporter1);
90 PG_FUNCTION_INFO_V1(pgs2seninfo);
93 * The function prototypes are created as a part of PG_FUNCTION_INFO_V1
94 * macro since 9.4, and hence the declaration of the function prototypes
95 * here is necessary only for 9.3 or before.
97 #if PG_VERSION_NUM < 90400
98 Datum pgs2snippet1(PG_FUNCTION_ARGS);
99 Datum pgs2norm(PG_FUNCTION_ARGS);
100 Datum pgs2textporter1(PG_FUNCTION_ARGS);
101 Datum pgs2seninfo(PG_FUNCTION_ARGS);
104 static sen_encoding GetSennaEncoding(void);
105 static sen_query *GetSennaQuery(char *str, size_t len);
106 static bool EscapeSnippetKeyword(char **s, size_t *slen);
109 #define TEXTPORTER_TMPDIR "/tmp"
110 #define TEXTPORTER_MKSTEMP_UMASK 0177
111 #define TEXTPORTER_GROUPNAME "UTF-8"
112 #define TEXTPORTER_DEFLANGNAME "Japanese"
113 #define TEXTPORTER_BBIGENDIAN 1
114 #define TEXTPORTER_OPTION 0x00000020 /* DMC_GETTEXT_OPT_LF */
115 #define TEXTPORTER_OPTION_STRING "32"
116 #define TEXTPORTER_OPTION1 0x00010000 /* DMC_GETTEXT_OPT1_TXCONV */
117 #define TEXTPORTER_SIZE 0
118 #define TEXTPORTER_CSV_C 0
121 /* GUC variables for pgs2textpoter1 */
122 static int textporter_error = ERROR;
123 static unsigned int textporter_option = TEXTPORTER_OPTION;
126 * This variable is a dummy that doesn't do anything, except in some
127 * cases provides the value for SHOW to display. The real state is
128 * elsewhere and is kept in sync by assign_hooks.
130 static char *textporter_option_string;
132 static const struct config_enum_entry textporter_error_options[] = {
133 {"debug1", DEBUG1, false},
135 {"info", INFO, false},
136 {"notice", NOTICE, false},
137 {"warning", WARNING, false},
138 {"error", ERROR, false},
142 static void CleanupTextPorterTmpFiles(void);
144 static bool check_textporter_option(char **newval, void **extra, GucSource source);
145 static void assign_textporter_option(const char *newval, void *extra);
146 #endif /* TEXTPORTER */
157 /* Define custom GUC variable for debugging */
158 DefineCustomEnumVariable("ludia_funcs.enable_debug",
159 "Emit ludia_funcs debugging output.",
162 PGS2_ENABLE_DEBUG_OFF,
163 pgs2_enable_debug_options,
171 /* Can't be set in postgresql.conf */
172 DefineCustomStringVariable("ludia_funcs.last_update",
173 "Shows the last update date of ludia_funcs.",
178 GUC_REPORT | GUC_NOT_IN_SAMPLE | GUC_DISALLOW_IN_FILE,
184 /* Define custom GUC variables */
185 DefineCustomEnumVariable("ludia_funcs.textporter_error",
186 "Sets the message levels that are emitted "
187 "when textporter fails.",
191 textporter_error_options,
198 DefineCustomStringVariable("ludia_funcs.textporter_option",
199 "Sets the option used to get text data "
202 &textporter_option_string,
203 TEXTPORTER_OPTION_STRING,
206 check_textporter_option,
207 assign_textporter_option,
210 /* Clean up remaining textporter temporary files */
211 CleanupTextPorterTmpFiles();
212 #endif /* TEXTPORTER */
215 * A value of 0 means no limit on the cache size. A value of -1 means
216 * that work_mem is used as the upper size limit of the cache.
218 DefineCustomIntVariable("ludia_funcs.norm_cache_limit",
219 "Sets the maximum memory to be used for caching "
220 "the result of pgs2norm()",
232 DefineCustomBoolVariable("ludia_funcs.escape_snippet_keyword",
233 "Escapes snippet keyword string.",
235 &escape_snippet_keyword,
243 EmitWarningsOnPlaceholders("ludia_funcs");
245 /* Initialize Senna */
247 if (rc != sen_success)
249 (errmsg("sen_init() failed: %d", rc)));
258 #define REMOVE_TMPFILE(path) \
260 if (unlink(path) != 0) \
262 (errcode_for_file_access(), \
263 errmsg("could not remove temporary file \"%s\": %m", path))); \
267 pgs2textporter1(PG_FUNCTION_ARGS)
269 char *appfile = text_to_cstring(PG_GETARG_TEXT_P(0));
270 char txtfile[] = TEXTPORTER_TMPDIR "/ludia_funcs_XXXXXX";
276 bool return_null = false;
279 /* Confirm that database encoding is UTF-8 */
285 * Generate a unique temporary filename where text data gotten
286 * from application file by TextPorter is stored temporarily.
287 * Set the permission of a temporary file to 0600 to ensure that
288 * only the owner of PostgreSQL server can read and write the file.
290 oumask = umask(TEXTPORTER_MKSTEMP_UMASK);
291 tmpfd = mkstemp(txtfile);
296 (errcode_for_file_access(),
297 errmsg("could not generate a unique temporary filename: %m")));
298 if (close(tmpfd) != 0)
300 (errcode_for_file_access(),
301 errmsg("could not close temporary file \"%s\": %m", txtfile)));
304 * Run TextPorter to read text data from application file (appfile)
305 * to temporary file (txtfile).
307 ret = ExecTextPorter((unsigned char *)appfile,
308 (unsigned char *)txtfile,
309 (unsigned char *)TEXTPORTER_GROUPNAME,
310 (unsigned char *)TEXTPORTER_DEFLANGNAME,
311 TEXTPORTER_BBIGENDIAN, textporter_option,
312 TEXTPORTER_OPTION1, TEXTPORTER_SIZE,
316 ereport(textporter_error,
317 (errmsg("could not get text from application file \"%s\"",
319 errdetail("DMC_GetText_V5() failed with errcode %d",
322 /* Return NULL if textporter_error is set to other than ERROR */
327 /* Read text data from temporary file to memory */
328 if (stat(txtfile, &statbuf))
330 (errcode_for_file_access(),
331 errmsg("could not stat file \"%s\": %m", txtfile)));
332 result = (text *) palloc(statbuf.st_size + VARHDRSZ);
334 fp = AllocateFile(txtfile, "r");
337 (errcode_for_file_access(),
338 errmsg("could not open file \"%s\": %m", txtfile)));
340 if (fread(VARDATA(result), 1, statbuf.st_size, fp) != statbuf.st_size ||
343 (errcode_for_file_access(),
344 errmsg("could not read file \"%s\": %m", txtfile)));
349 REMOVE_TMPFILE(txtfile);
354 REMOVE_TMPFILE(txtfile);
362 SET_VARSIZE(result, statbuf.st_size + VARHDRSZ);
364 PG_RETURN_TEXT_P(result);
368 * Clean up remaining textporter temporary files
371 CleanupTextPorterTmpFiles(void)
375 char path[MAXPGPATH];
377 tpdir = AllocateDir(TEXTPORTER_TMPDIR);
380 (errcode_for_file_access(),
381 errmsg("could not open textporter temporary file directory \"%s\": %m",
382 TEXTPORTER_TMPDIR)));
384 while ((tpde = ReadDir(tpdir, TEXTPORTER_TMPDIR)) != NULL)
386 if (strlen(tpde->d_name) == 18 &&
387 strncmp(tpde->d_name, "ludia_funcs_", 12) == 0)
389 snprintf(path, MAXPGPATH, TEXTPORTER_TMPDIR "/%s", tpde->d_name);
390 REMOVE_TMPFILE(path);
398 check_textporter_option(char **newval, void **extra, GucSource source)
402 unsigned int *myextra;
405 val = strtoul(*newval, &endptr, 0);
410 if (errno == ERANGE || val != (unsigned long) ((unsigned int) val))
412 GUC_check_errhint("Value exceeds unsigned integer range.");
416 /* Set up the "extra" struct actually used by assign_textporter_option */
417 myextra = (unsigned int *) malloc(sizeof(unsigned int));
420 GUC_check_errcode(ERRCODE_OUT_OF_MEMORY);
421 GUC_check_errmsg("out of memory");
424 *myextra = (unsigned int) val;
425 *extra = (void *) myextra;
431 assign_textporter_option(const char *newval, void *extra)
433 textporter_option = *((unsigned int *) extra);
436 #else /* TEXTPORTER */
439 pgs2textporter1(PG_FUNCTION_ARGS)
444 #endif /* TEXTPORTER */
447 GetSennaEncoding(void)
449 static sen_encoding encoding = sen_enc_default;
451 if (encoding == sen_enc_default)
453 if (GetDatabaseEncoding() == PG_UTF8)
454 encoding = sen_enc_utf8;
457 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
458 errmsg("does not support database encoding \"%s\"",
459 GetDatabaseEncodingName())));
465 * Escape the backslash and double quote characters in the given string.
467 * Return false if the given string has no character which needs to be
468 * escaped. Otherwise, return true. In this case, **s points the palloc'd
469 * space storing the escaped keyword string and *slen is set to the size
470 * of that string. The caller needs to free the palloc'd space.
473 EscapeSnippetKeyword(char **s, size_t *slen)
480 bool in_doublequote = false;
481 bool in_sennaops = false;
482 bool need_escape = false;
485 * Skip the heading double quote character because it always doesn't
486 * need to be interpreted as a character itself and be escaped.
487 * Note that we must not skip the heading character if it's not a
491 if (ISDOUBLEQUOTECHAR(sp))
495 * Check whether the snippet keyword string has a character which
496 * needs to be escaped.
498 while ((sp - *s) < *slen)
500 mblen = pg_mblen(sp);
503 * Backslash in the keyword always needs to be escaped.
505 if (ISBACKSLASHCHAR(sp))
513 if (ISSENNAOPSCHAR(sp))
516 in_doublequote = false;
521 * Double quote in the keyword needs to be escaped if
522 * any Senna search operators are to neither its right
531 if (ISDOUBLEQUOTECHAR(sp) && !in_sennaops)
532 in_doublequote = true;
533 if (!ISSENNAOPSCHAR(sp))
541 * Quick exit if the keyword has no character which needs to be
548 * Allocate the buffer space to store the escaped snippet keyword string.
549 * The maximum size of escaped string is double the input keyword size.
550 * The size reaches the maximum when every character in the input keyword
551 * needs to be escaped.
553 ep = escaped = (char *) palloc(*slen * 2);
556 * Copy the characters which have been passed through in the above loop
557 * and don't need to be escaped, into the buffer. If in_doublequote is
558 * true, we don't copy the double quote in the previous position into the
559 * buffer because it might still need to be escaped.
561 copylen = sp - *s - ((in_doublequote) ? 1 : 0);
562 memcpy(ep, *s, copylen);
566 * Construct the escaped snippet keyword string.
568 while ((sp - *s) < *slen)
570 mblen = pg_mblen(sp);
575 * dqchar indicates the previous character, that is a double
576 * quote. We assume here that a double quote is single-byte
579 char dqchar = *(sp - 1);
581 if (ISSENNAOPSCHAR(sp))
584 * Don't escape the double quote which is just before Senna
590 in_doublequote = false;
595 * Escape the double quote if no Senna operator is next to it.
600 if (ISDOUBLEQUOTECHAR(sp))
601 in_doublequote = true;
604 if (ISBACKSLASHCHAR(sp))
606 memcpy(ep, sp, mblen);
608 in_doublequote = false;
614 if (ISDOUBLEQUOTECHAR(sp))
617 * Don't escape the double quote which is just after Senna
623 in_doublequote = true;
627 if (ISBACKSLASHCHAR(sp))
630 * We don't check ISSENNAOPSCHAR() here. We handle Senna
631 * operator character as a character itself instead of
632 * an operator if it doesn't follow a double quote.
634 memcpy(ep, sp, mblen);
638 if (!ISSENNAOPSCHAR(sp))
645 /* Add the tailing double quote into the buffer */
653 if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_ON)
655 char *tmp = pnstrdup(*s, *slen);
657 elog(LOG, "escaped snippet keyword: %s", tmp);
660 else if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_TERSE)
661 elog(LOG, "escaped snippet keyword");
668 GetSennaQuery(char *str, size_t len)
670 static sen_query *query_cache = NULL;
671 static char *key_cache = NULL;
672 static size_t len_cache = 0;
673 static bool guc_cache = false;
675 sen_encoding encoding;
679 bool needfree = false;
682 * Return the cached Senna query if the same keyword has
683 * been used the last time.
685 if (key_cache != NULL &&
687 strncmp(key_cache, str, len) == 0 &&
688 escape_snippet_keyword == guc_cache)
691 if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_ON)
693 char *tmp = pnstrdup(str, len);
695 elog(LOG, "GetSennaQuery(): quick exit: %s", tmp);
698 else if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_TERSE)
699 elog(LOG, "GetSennaQuery(): quick exit");
704 encoding = GetSennaEncoding();
709 (errcode(ERRCODE_OUT_OF_MEMORY),
710 errmsg("out of memory")));
713 * We always cache the unescaped keyword. Which enables us
714 * to check whether we can use the cached Senna query before
715 * escaping the keyword.
717 memcpy(key, str, len);
721 * If the keyword has been escaped, 'str' points to the
722 * newly-palloc'd space storing the escaped keyword. This
723 * space needs to be freed later.
725 if (escape_snippet_keyword)
726 needfree = EscapeSnippetKeyword(&str, &len);
728 query = sen_query_open(str, len, sen_sel_or, SEN_MAX_N_EXPRS,
734 (errmsg("sen_query_open() failed")));
737 if ((rest = sen_query_rest(query, NULL)) != 0)
739 (errmsg("too many expressions (%d)", rest)));
741 if (query_cache != NULL)
743 sen_query_close(query_cache);
750 guc_cache = escape_snippet_keyword;
759 pgs2snippet1(PG_FUNCTION_ARGS)
761 int flags = PG_GETARG_INT32(0);
762 uint32 width = PG_GETARG_UINT32(1);
763 uint32 max_results = PG_GETARG_UINT32(2);
764 text *opentags = PG_GETARG_TEXT_P(3);
765 text *closetags = PG_GETARG_TEXT_P(4);
766 int mapping = PG_GETARG_INT32(5);
767 text *keywords = PG_GETARG_TEXT_P(6);
768 text *document = PG_GETARG_TEXT_P(7);
770 sen_snip *snip = NULL;
771 const char *opentags_str = VARDATA_ANY(opentags);
772 const char *closetags_str = VARDATA_ANY(closetags);
773 char *keywords_str = VARDATA_ANY(keywords);
774 char *document_str = VARDATA_ANY(document);
775 uint32 opentags_len = VARSIZE_ANY_EXHDR(opentags);
776 uint32 closetags_len = VARSIZE_ANY_EXHDR(closetags);
777 uint32 keywords_len = VARSIZE_ANY_EXHDR(keywords);
778 uint32 document_len = VARSIZE_ANY_EXHDR(document);
780 uint32 max_tagged_len = 0;
783 uint32 result_len = 0;
784 bool return_null = false;
786 query = GetSennaQuery(keywords_str, keywords_len);
788 snip = sen_query_snip(query, flags, width, max_results, 1,
789 &opentags_str, &opentags_len,
790 &closetags_str, &closetags_len,
791 mapping == 0 ? NULL : (sen_snip_mapping *)-1);
794 (errmsg("sen_query_snip() failed")));
798 rc = sen_snip_exec(snip, document_str, document_len,
799 &nresults, &max_tagged_len);
800 if (rc != sen_success)
802 (errmsg("sen_snip_exec() failed: %d", rc)));
804 result = (text *) palloc(max_tagged_len + VARHDRSZ);
806 rc = sen_snip_get_result(snip, 0, VARDATA(result), &result_len);
807 if (rc == sen_invalid_argument)
809 else if (rc != sen_success)
811 (errmsg("sen_snip_get_result() failed: %d", rc)));
815 sen_snip_close(snip);
820 sen_snip_close(snip);
825 SET_VARSIZE(result, max_tagged_len + VARHDRSZ);
827 PG_RETURN_TEXT_P(result);
831 * Make sure there is enough space for 'needed' more bytes.
833 * Sets **buf to the allocated space which can store the needed bytes if OK,
834 * NULL if failed to enlarge the space because 'needed' is larger than 'maxlen'.
837 pgs2malloc(void **buf, long *buflen, long needed, long maxlen)
840 if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_ON)
841 elog(LOG, "pgs2malloc(): buflen %ld, needed %ld, maxlen %ld",
842 *buflen, needed, maxlen);
843 else if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_TERSE)
844 elog(LOG, "pgs2malloc()");
847 if (*buf != NULL && *buflen >= needed && (*buflen <= maxlen || maxlen == 0))
848 return; /* got enough space already */
851 * Release the already-allocated space since it's too small to
852 * store the needed bytes or larger than the upper limit.
862 * Don't allocate any space if the needed space is larger than
865 if (needed > maxlen && maxlen != 0)
869 * Allocate the space for the needed bytes.
871 * We don't want to allocate just a little more space with each enlarge;
872 * for efficiency, double the buffer size each time it overflows.
873 * Actually, we might need to more than double it if 'needed' is big...
875 * We check whether '*buflen' overflows each cycle to avoid infinite loop.
878 while (*buflen < needed && *buflen != 0)
882 * Clamp to maxlen in case we went past it. Note we are assuming
883 * here that maxlen <= LONG_MAX/2, else the above loop could
884 * overflow. We will still have *buflen >= needed.
886 if (*buflen > maxlen && maxlen != 0)
889 /* Guard against out-of-range '*buflen' value */
892 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
893 errmsg("out of memory"),
894 errdetail("Cannot enlarge buffer by %ld more bytes.",
897 *buf = (void *) malloc(*buflen);
900 (errcode(ERRCODE_OUT_OF_MEMORY),
901 errmsg("out of memory")));
905 pgs2norm(PG_FUNCTION_ARGS)
907 text *str = PG_GETARG_TEXT_PP(0);
908 char *s = VARDATA_ANY(str);
909 long slen = VARSIZE_ANY_EXHDR(str);
917 * norm_cache is the cache memory storing both input and normalized strings
918 * as the result of pgs2norm(). norm_cache_size is the size of norm_cache
919 * and its upper limit is specified by norm_cache_limit parameter. norm_result
920 * is the pointer to the normalized string with the verlena header (i.e.,
921 * text type) stored in the latter half of the cache. norm_reslen is the size
922 * of norm_result. norm_slen is the size of the input string which is stored
923 * in the first half of the cache.
925 static char *norm_cache = NULL;
926 static long norm_cache_size = 0;
927 static long norm_slen = 0;
928 static char *norm_result = NULL;
929 static long norm_reslen = 0;
932 * Return the cached normalization result if the same string of
933 * the given one has been normalized the last time.
935 if (norm_cache != NULL &&
937 strncmp(norm_cache, s, slen) == 0)
940 if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_ON)
942 char *tmp = text_to_cstring(str);
944 elog(LOG, "pgs2norm(): quick exit: %s", tmp);
947 else if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_TERSE)
948 elog(LOG, "pgs2norm(): quick exit");
951 result = (text *) palloc(norm_reslen);
952 memcpy(result, norm_result, norm_reslen);
953 PG_RETURN_TEXT_P(result);
956 /* Confirm that database encoding is UTF-8 */
960 * Allocate the result buffer to store the normalized string. Since the size of
961 * normalized string can be larger than that of input one, the result buffer needs
962 * extra space. Problem is that, before calling sen_str_normalize, we need to
963 * allocate the result buffer but cannot know how large extra space is required.
964 * So we use RESULT_EXTRA_SIZE as the estimated size of extra space here.
966 #define RESULT_EXTRA_SIZE 64
967 buflen = slen + RESULT_EXTRA_SIZE;
970 result = (text *) palloc(buflen + VARHDRSZ);
972 #if defined(FAST_SENNA)
973 reslen = fast_sen_str_normalize(s, slen, VARDATA(result), buflen);
975 reslen = sen_str_normalize(s, slen, sen_enc_utf8,
977 VARDATA(result), buflen);
982 (errmsg("could not normalize the string")));
985 * If the result buffer size is too short to store the normalized string,
986 * we enlarge the buffer and retry the string normalization.
988 if (buflen <= reslen)
995 SET_VARSIZE(result, reslen + VARHDRSZ);
998 * Cache both input and normalized strings to accelerate the subsequent
999 * calls of pgs2norm() with the same input string. But we don't do that
1000 * if the maximum allowed size of the cache is too small to store them.
1002 needed = slen + reslen + VARHDRSZ;
1003 maxlen = ((norm_cache_limit >= 0) ? norm_cache_limit : work_mem) * 1024L;
1005 pgs2malloc((void **) &norm_cache, &norm_cache_size, needed, maxlen);
1006 if (norm_cache != NULL)
1008 /* Store the input string into the first half of the cache */
1010 memcpy(norm_cache, s, slen);
1013 * Store the normalized string with the varlena header (i.e., text type)
1014 * into the latter half of the cache.
1016 norm_result = norm_cache + slen;
1017 norm_reslen = reslen + VARHDRSZ;
1018 memcpy(norm_result, result, norm_reslen);
1022 if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_ON)
1024 char *tmp = text_to_cstring(str);
1026 elog(LOG, "pgs2norm(): complete (%s result cache): %s",
1027 (norm_cache == NULL) ? "unset" : "set", tmp);
1030 else if (pgs2_enable_debug == PGS2_ENABLE_DEBUG_TERSE)
1031 elog(LOG, "pgs2norm(): complete");
1034 PG_RETURN_TEXT_P(result);
1038 * Report the version and configure options of Senna which
1039 * ludia_funcs depends on.
1042 pgs2seninfo(PG_FUNCTION_ARGS)
1044 char *version[MAXPGPATH];
1045 char *coptions[MAXPGPATH];
1052 * Get the version and configure options of Senna. Ignore the
1053 * return value of sen_info() because it always returns a success.
1055 sen_info((char **)&version, (char **)&coptions, NULL, NULL, NULL, NULL);
1058 * Construct a tuple descriptor for the result row. This must
1059 * match this function's ludia_funcs--x.x.sql entry.
1061 #if PG_VERSION_NUM >= 120000
1062 tupdesc = CreateTemplateTupleDesc(2);
1064 tupdesc = CreateTemplateTupleDesc(2, false);
1066 TupleDescInitEntry(tupdesc, (AttrNumber) 1,
1067 "version", TEXTOID, -1, 0);
1068 TupleDescInitEntry(tupdesc, (AttrNumber) 2,
1069 "configure_options", TEXTOID, -1, 0);
1070 tupdesc = BlessTupleDesc(tupdesc);
1073 values[0] = CStringGetTextDatum(*version);
1076 /* configure option */
1077 values[1] = CStringGetTextDatum(*coptions);
1080 tuple = heap_form_tuple(tupdesc, values, isnull);
1081 PG_RETURN_DATUM(HeapTupleGetDatum(tuple));