Skip to content

Commit 6751b4c

Browse files
committed
update the pg_get_functiondef
1 parent c5f4c83 commit 6751b4c

File tree

4 files changed

+25
-183
lines changed

4 files changed

+25
-183
lines changed

contrib/pg_get_functiondef/expected/pg_get_functiondef.out

Lines changed: 1 addition & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -25,90 +25,6 @@ SELECT * FROM pg_get_functiondef(1, 2, 3);
2525
|
2626
(3 rows)
2727

28-
SELECT pg_get_functiondef('sum');
29-
pg_get_functiondef
30-
-------------------------------------------------------
31-
(sum,"/* Not support aggregate function, oid: 2107 */+
32-
/* Not support aggregate function, oid: 2109 */ +
33-
/* Not support aggregate function, oid: 2108 */ +
34-
/* Not support aggregate function, oid: 2110 */ +
35-
/* Not support aggregate function, oid: 2111 */ +
36-
/* Not support aggregate function, oid: 2112 */ +
37-
/* Not support aggregate function, oid: 2113 */ +
38-
/* Not support aggregate function, oid: 2114 */ +
39-
/* Not support aggregate function, oid: 16133 */ +
40-
")
41-
(1 row)
42-
43-
SELECT * FROM pg_get_functiondef('sum');
44-
name | pg_get_functiondef
45-
------+--------------------------------------------------
46-
sum | /* Not support aggregate function, oid: 2107 */ +
47-
| /* Not support aggregate function, oid: 2109 */ +
48-
| /* Not support aggregate function, oid: 2108 */ +
49-
| /* Not support aggregate function, oid: 2110 */ +
50-
| /* Not support aggregate function, oid: 2111 */ +
51-
| /* Not support aggregate function, oid: 2112 */ +
52-
| /* Not support aggregate function, oid: 2113 */ +
53-
| /* Not support aggregate function, oid: 2114 */ +
54-
| /* Not support aggregate function, oid: 16133 */+
55-
|
56-
(1 row)
57-
58-
SELECT pg_get_functiondef('pg_get_functiondef');
59-
pg_get_functiondef
60-
---------------------------------------------------------------------------
61-
(pg_get_functiondef,"/* oid: 16702 */ +
62-
CREATE OR REPLACE FUNCTION public.pg_get_functiondef() +
63-
RETURNS cstring +
64-
LANGUAGE c +
65-
AS 'pg_get_functiondef', $function$pg_get_functiondef_no_input$function$ +
66-
/* oid: 2098 */ +
67-
CREATE OR REPLACE FUNCTION pg_catalog.pg_get_functiondef(oid) +
68-
RETURNS text +
69-
LANGUAGE internal +
70-
STABLE PARALLEL SAFE STRICT +
71-
AS $function$pg_get_functiondef$function$ +
72-
/* oid: 16704 */ +
73-
CREATE OR REPLACE FUNCTION public.pg_get_functiondef(VARIADIC text[]) +
74-
RETURNS TABLE(name cstring, pg_get_functiondef cstring) +
75-
LANGUAGE c +
76-
AS 'pg_get_functiondef', $function$pg_get_functiondef_extend$function$ +
77-
/* oid: 16703 */ +
78-
CREATE OR REPLACE FUNCTION public.pg_get_functiondef(oid, VARIADIC oid[])+
79-
RETURNS TABLE(oid oid, pg_get_functiondef cstring) +
80-
LANGUAGE c +
81-
AS 'pg_get_functiondef', $function$pg_get_functiondef_mul$function$ +
82-
")
83-
(1 row)
84-
85-
SELECT * FROM pg_get_functiondef('pg_get_functiondef');
86-
name | pg_get_functiondef
87-
--------------------+---------------------------------------------------------------------------
88-
pg_get_functiondef | /* oid: 16702 */ +
89-
| CREATE OR REPLACE FUNCTION public.pg_get_functiondef() +
90-
| RETURNS cstring +
91-
| LANGUAGE c +
92-
| AS 'pg_get_functiondef', $function$pg_get_functiondef_no_input$function$ +
93-
| /* oid: 2098 */ +
94-
| CREATE OR REPLACE FUNCTION pg_catalog.pg_get_functiondef(oid) +
95-
| RETURNS text +
96-
| LANGUAGE internal +
97-
| STABLE PARALLEL SAFE STRICT +
98-
| AS $function$pg_get_functiondef$function$ +
99-
| /* oid: 16704 */ +
100-
| CREATE OR REPLACE FUNCTION public.pg_get_functiondef(VARIADIC text[]) +
101-
| RETURNS TABLE(name cstring, pg_get_functiondef cstring) +
102-
| LANGUAGE c +
103-
| AS 'pg_get_functiondef', $function$pg_get_functiondef_extend$function$ +
104-
| /* oid: 16703 */ +
105-
| CREATE OR REPLACE FUNCTION public.pg_get_functiondef(oid, VARIADIC oid[])+
106-
| RETURNS TABLE(oid oid, pg_get_functiondef cstring) +
107-
| LANGUAGE c +
108-
| AS 'pg_get_functiondef', $function$pg_get_functiondef_mul$function$ +
109-
|
110-
(1 row)
111-
11228
SELECT pg_get_functiondef('char_length', 'substring');
11329
pg_get_functiondef
11430
------------------------------------------------------------------------------
@@ -176,70 +92,4 @@ SELECT pg_get_functiondef('char_length', 'substring');
17692
")
17793
(2 rows)
17894

179-
SELECT * FROM pg_get_functiondef('char_length', 'substring');
180-
name | pg_get_functiondef
181-
-------------+----------------------------------------------------------------------------
182-
char_length | /* oid: 1381 */ +
183-
| CREATE OR REPLACE FUNCTION pg_catalog.char_length(text) +
184-
| RETURNS integer +
185-
| LANGUAGE internal +
186-
| IMMUTABLE PARALLEL SAFE STRICT +
187-
| AS $function$textlen$function$ +
188-
| /* oid: 1372 */ +
189-
| CREATE OR REPLACE FUNCTION pg_catalog.char_length(character) +
190-
| RETURNS integer +
191-
| LANGUAGE internal +
192-
| IMMUTABLE PARALLEL SAFE STRICT +
193-
| AS $function$bpcharlen$function$ +
194-
|
195-
substring | /* oid: 2013 */ +
196-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(bytea, integer) +
197-
| RETURNS bytea +
198-
| LANGUAGE internal +
199-
| IMMUTABLE PARALLEL SAFE STRICT +
200-
| AS $function$bytea_substr_no_len$function$ +
201-
| /* oid: 937 */ +
202-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(text, integer) +
203-
| RETURNS text +
204-
| LANGUAGE internal +
205-
| IMMUTABLE PARALLEL SAFE STRICT +
206-
| AS $function$text_substr_no_len$function$ +
207-
| /* oid: 2073 */ +
208-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(text, text) +
209-
| RETURNS text +
210-
| LANGUAGE internal +
211-
| IMMUTABLE PARALLEL SAFE STRICT +
212-
| AS $function$textregexsubstr$function$ +
213-
| /* oid: 1699 */ +
214-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(bit, integer) +
215-
| RETURNS bit +
216-
| LANGUAGE internal +
217-
| IMMUTABLE PARALLEL SAFE STRICT +
218-
| AS $function$bitsubstr_no_len$function$ +
219-
| /* oid: 2012 */ +
220-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(bytea, integer, integer)+
221-
| RETURNS bytea +
222-
| LANGUAGE internal +
223-
| IMMUTABLE PARALLEL SAFE STRICT +
224-
| AS $function$bytea_substr$function$ +
225-
| /* oid: 936 */ +
226-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(text, integer, integer) +
227-
| RETURNS text +
228-
| LANGUAGE internal +
229-
| IMMUTABLE PARALLEL SAFE STRICT +
230-
| AS $function$text_substr$function$ +
231-
| /* oid: 2074 */ +
232-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(text, text, text) +
233-
| RETURNS text +
234-
| LANGUAGE sql +
235-
| IMMUTABLE PARALLEL SAFE STRICT COST 1 +
236-
| RETURN "substring"($1, similar_to_escape($2, $3)) +
237-
| /* oid: 1680 */ +
238-
| CREATE OR REPLACE FUNCTION pg_catalog."substring"(bit, integer, integer) +
239-
| RETURNS bit +
240-
| LANGUAGE internal +
241-
| IMMUTABLE PARALLEL SAFE STRICT +
242-
| AS $function$bitsubstr$function$ +
243-
|
244-
(2 rows)
245-
DROP EXTENSION pg_get_functiondef;
95+
DROP EXTENSION pg_get_functiondef;

contrib/pg_get_functiondef/pg_get_functiondef_extend.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
pg_get_functiondef_extend.c
3-
The achieve of pg_get_functiondef('function_name', 'function_name', ...)
3+
The implementation of pg_get_functiondef('function_name', 'function_name', ...)
44
*/
55
#include "postgres.h"
66
#include "fmgr.h"
@@ -22,22 +22,22 @@ struct FunctionInfoData
2222
{
2323
char **function_name; /* The user's input */
2424
int cursor; /* The cursor of the function_name[] */
25-
int count_of_arguments; /* How many tuples will return? */
25+
int count_of_tuples; /* How many tuples will return? */
2626
TupleDesc result_desc; /* Describle the tuple */
2727
AttInMetadata *result_tuple_meta; /* Describle the tuple's attribute */
2828
};
2929

3030
/*
31-
DescibleFunctionByName
31+
DescribleFunctionByName
3232
Export the oid based on the function name, the use the oid get the definition
3333
*/
34-
char *DescibleFunctionByName(char *function_name);
35-
char *DescibleFunctionByName(char *function_name)
34+
char *DescribleFunctionByName(char *function_name)
3635
{
37-
CatCList *catlist;
38-
StringInfoData result;
36+
CatCList *catlist = NULL;
37+
StringInfoData result = {};
3938
initStringInfo(&result);
4039
catlist = SearchSysCacheList1(PROCNAMEARGSNSP, CStringGetDatum(function_name));
40+
4141
if (catlist->n_members == 0)
4242
{
4343
appendStringInfo(&result, "/* Not Found */");
@@ -92,7 +92,7 @@ Datum pg_get_functiondef_extend(PG_FUNCTION_ARGS)
9292
}
9393

9494
info->cursor = 0;
95-
info->count_of_arguments = count_of_arguments;
95+
info->count_of_tuples = count_of_arguments;
9696

9797
if (get_call_result_type(fcinfo, NULL, &info->result_desc) != TYPEFUNC_COMPOSITE)
9898
{
@@ -105,7 +105,7 @@ Datum pg_get_functiondef_extend(PG_FUNCTION_ARGS)
105105
funcctx = SRF_PERCALL_SETUP();
106106
info = (FunctionInfo)(funcctx->user_fctx);
107107

108-
if (info->cursor == info->count_of_arguments)
108+
if (info->cursor == info->count_of_tuples)
109109
{
110110
SRF_RETURN_DONE(funcctx);
111111
}
@@ -116,7 +116,7 @@ Datum pg_get_functiondef_extend(PG_FUNCTION_ARGS)
116116
char **values;
117117
values = palloc0(sizeof(char *) * 2);
118118
values[0] = pstrdup(info->function_name[info->cursor]);
119-
values[1] = DescibleFunctionByName(info->function_name[info->cursor]);
119+
values[1] = DescribleFunctionByName(info->function_name[info->cursor]);
120120
info->cursor++;
121121
tuple = BuildTupleFromCStrings(info->result_tuple_meta, values);
122122
tuple_return = HeapTupleGetDatum(tuple);

contrib/pg_get_functiondef/pg_get_functiondef_mul.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
pg_get_functiondef_mul.c
3-
The achieve of pg_get_functiondef(oid, oid, ...)
3+
The implementation of pg_get_functiondef(oid, oid, ...)
44
*/
55
#include "postgres.h"
66
#include "fmgr.h"
@@ -17,22 +17,22 @@
1717
typedef struct FunctionInfoData *FunctionInfo;
1818
struct FunctionInfoData
1919
{
20-
TupleDesc result_desc; /* Describle the tuple */
20+
TupleDesc result_desc; /* the tuple's description */
2121
AttInMetadata *result_tuple_meta; /* the tuple's attribute */
22-
Oid *oids; /* user input */
23-
int cursor; /* the current of the oids[] */
24-
int count; /* How many tuples will return? */
25-
SPITupleTable *spi_table; /* The result of SPI_execute */
22+
Oid *oids; /* user input */
23+
int cursor; /* the current indexof the oids[] */
24+
int count; /* How many tuples will return? */
25+
SPITupleTable *spi_table; /* The result of SPI_execute */
2626
};
2727

2828
PG_FUNCTION_INFO_V1(pg_get_functiondef_mul);
2929

3030
Datum pg_get_functiondef_mul(PG_FUNCTION_ARGS)
3131
{
32-
FuncCallContext *funcctx;
32+
FuncCallContext *funcctx = NULL;
3333
FunctionInfo info = NULL;
3434
/* First Call */
35-
if (SRF_IS_FIRSTCALL())
35+
if (SRF_IS_FIRSTCALL())
3636
{
3737
StringInfoData query;
3838
AnyArrayType *oids_array = PG_GETARG_ANY_ARRAY_P(1);
@@ -42,7 +42,7 @@ Datum pg_get_functiondef_mul(PG_FUNCTION_ARGS)
4242

4343
if (SPI_OK_CONNECT != SPI_connect())
4444
{
45-
ereport(ERROR, errmsg("SPI connect failed!"));
45+
ereport(ERROR, (errmsg("SPI connect failed: %s", SPI_result_code_string(SPI_result))));
4646
PG_RETURN_NULL();
4747
}
4848

@@ -69,13 +69,12 @@ Datum pg_get_functiondef_mul(PG_FUNCTION_ARGS)
6969
info->count = count + 1;
7070
initStringInfo(&query);
7171
/* Build the query statement */
72-
appendStringInfo(&query, "SELECT ");
73-
appendStringInfo(&query, "pg_get_functiondef(%d)", info->oids[0]);
74-
for (int i = 1; i < count; i++)
72+
appendStringInfo(&query, "SELECT pg_get_functiondef(%d)", info->oids[0]);
73+
for (int i = 1; i < info->count; i++)
7574
{
7675
appendStringInfo(&query, ", pg_get_functiondef(%d)", info->oids[i]);
7776
}
78-
appendStringInfo(&query, ", pg_get_functiondef(%d);", info->oids[info->count - 1]);
77+
appendStringInfoChar(&query, ';');
7978
if (SPI_execute(query.data, true, 0) != SPI_OK_SELECT)
8079
{
8180
ereport(ERROR, errmsg("SPI select failed!"));
@@ -88,7 +87,7 @@ Datum pg_get_functiondef_mul(PG_FUNCTION_ARGS)
8887
funcctx = SRF_PERCALL_SETUP();
8988
info = (FunctionInfo)(funcctx->user_fctx);
9089
/* If cursor > count, means that all the tuple returned */
91-
if (info->cursor > info->count)
90+
if (info->cursor > info->count)
9291
{
9392
pfree(info->oids);
9493
FreeTupleDesc(info->result_desc);
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
CREATE EXTENSION pg_get_functiondef;
2-
32
SELECT pg_get_functiondef(1, 2, 3);
43
SELECT * FROM pg_get_functiondef(1, 2, 3);
5-
SELECT pg_get_functiondef('sum');
6-
SELECT * FROM pg_get_functiondef('sum');
7-
SELECT pg_get_functiondef('pg_get_functiondef');
8-
SELECT * FROM pg_get_functiondef('pg_get_functiondef');
94
SELECT pg_get_functiondef('char_length', 'substring');
10-
SELECT * FROM pg_get_functiondef('char_length', 'substring');
11-
12-
DROP EXTENSION pg_get_functiondef;
5+
DROP EXTENSION pg_get_functiondef;

0 commit comments

Comments
 (0)