Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf094e8

Browse files
committed
Add pg_exec_plan() routine
1 parentda0eb86 commitaf094e8

File tree

3 files changed

+65
-41
lines changed

3 files changed

+65
-41
lines changed

‎contrib/pg_execplan/init.sql

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ CREATE OR REPLACE FUNCTION @extschema@.pg_store_query_plan(
1010
RETURNS VOIDAS'pg_execplan'
1111
LANGUAGE C;
1212

13-
CREATEOR REPLACE FUNCTION @extschema@.pg_exec_query_plan(filenameTEXT)
13+
CREATEOR REPLACE FUNCTION @extschema@.pg_exec_plan(queryTEXT,
14+
planTEXT
15+
)
16+
RETURNS BOOLAS'pg_execplan'
17+
LANGUAGE C;
18+
19+
CREATEOR REPLACE FUNCTION @extschema@.pg_exec_stored_plan(filenameTEXT)
1420
RETURNS BOOLAS'pg_execplan'
1521
LANGUAGE C;

‎contrib/pg_execplan/pg_execplan.c

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
PG_MODULE_MAGIC;
2525

2626
PG_FUNCTION_INFO_V1(pg_store_query_plan);
27-
PG_FUNCTION_INFO_V1(pg_exec_query_plan);
27+
PG_FUNCTION_INFO_V1(pg_exec_plan);
28+
PG_FUNCTION_INFO_V1(pg_exec_stored_plan);
2829

2930
void_PG_init(void);
3031

@@ -97,37 +98,8 @@ pg_store_query_plan(PG_FUNCTION_ARGS)
9798
}
9899

99100
staticvoid
100-
LoadPlanFromFile(constchar*filename,char**query_string,char**plan_string)
101-
{
102-
FILE*fin;
103-
size_tstring_len;
104-
intnelems;
105-
106-
fin=fopen(filename,"rb");
107-
Assert(fin!=NULL);
108-
109-
nelems=fread(&string_len,sizeof(size_t),1,fin);
110-
Assert(nelems==1);
111-
*query_string=palloc0(string_len+1);
112-
nelems=fread(*query_string,sizeof(char),string_len,fin);
113-
Assert(nelems==string_len);
114-
115-
nelems=fread(&string_len,sizeof(size_t),1,fin);
116-
Assert(nelems==1);
117-
*plan_string=palloc0(string_len+1);
118-
nelems=fread(*plan_string,sizeof(char),string_len,fin);
119-
Assert(nelems==string_len);
120-
121-
fclose(fin);
122-
123-
}
124-
125-
Datum
126-
pg_exec_query_plan(PG_FUNCTION_ARGS)
101+
exec_plan(char*query_string,char*plan_string)
127102
{
128-
char*filename=TextDatumGetCString(PG_GETARG_DATUM(0)),
129-
*query_string=NULL,
130-
*plan_string=NULL;
131103
PlannedStmt*pstmt;
132104
ParamListInfoparamLI=NULL;
133105
CachedPlanSource*psrc;
@@ -137,8 +109,6 @@ pg_exec_query_plan(PG_FUNCTION_ARGS)
137109
int16format=0;
138110
inteflags=0;
139111

140-
LoadPlanFromFile(filename,&query_string,&plan_string);
141-
142112
PG_TRY();
143113
{
144114
set_portable_input(true);
@@ -187,7 +157,7 @@ pg_exec_query_plan(PG_FUNCTION_ARGS)
187157
PG_CATCH();
188158
{
189159
elog(INFO,"BAD QUERY: %s",query_string);
190-
PG_RETURN_BOOL(false);
160+
PG_RE_THROW();
191161
}
192162
PG_END_TRY();
193163

@@ -197,6 +167,54 @@ pg_exec_query_plan(PG_FUNCTION_ARGS)
197167

198168
if (EXPLAN_DEBUG_LEVEL>0)
199169
elog(INFO,"query execution finished.\n");
170+
}
171+
172+
Datum
173+
pg_exec_plan(PG_FUNCTION_ARGS)
174+
{
175+
char*query_string=TextDatumGetCString(PG_GETARG_DATUM(0));
176+
char*plan_string=TextDatumGetCString(PG_GETARG_DATUM(1));
200177

178+
Assert(query_string!=NULL);
179+
Assert(plan_string!=NULL);
180+
exec_plan(query_string,plan_string);
181+
PG_RETURN_BOOL(true);
182+
}
183+
184+
staticvoid
185+
LoadPlanFromFile(constchar*filename,char**query_string,char**plan_string)
186+
{
187+
FILE*fin;
188+
size_tstring_len;
189+
intnelems;
190+
191+
fin=fopen(filename,"rb");
192+
Assert(fin!=NULL);
193+
194+
nelems=fread(&string_len,sizeof(size_t),1,fin);
195+
Assert(nelems==1);
196+
*query_string=palloc0(string_len+1);
197+
nelems=fread(*query_string,sizeof(char),string_len,fin);
198+
Assert(nelems==string_len);
199+
200+
nelems=fread(&string_len,sizeof(size_t),1,fin);
201+
Assert(nelems==1);
202+
*plan_string=palloc0(string_len+1);
203+
nelems=fread(*plan_string,sizeof(char),string_len,fin);
204+
Assert(nelems==string_len);
205+
206+
fclose(fin);
207+
208+
}
209+
210+
Datum
211+
pg_exec_stored_plan(PG_FUNCTION_ARGS)
212+
{
213+
char*filename=TextDatumGetCString(PG_GETARG_DATUM(0)),
214+
*query_string=NULL,
215+
*plan_string=NULL;
216+
217+
LoadPlanFromFile(filename,&query_string,&plan_string);
218+
exec_plan(query_string,plan_string);
201219
PG_RETURN_BOOL(true);
202220
}

‎contrib/pg_execplan/tests/rpl.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ psql -p 5433 -c "SELECT current_schemas(true);"
5353

5454
# TEST ON RELOID and TYPEOID objects.
5555
psql -p 5432 -c"SELECT pg_store_query_plan('../test.txt', 'SELECT * FROM tests.t1;');"
56-
psql -p 5433 -c"SELECTpg_exec_query_plan('../test.txt');"
56+
psql -p 5433 -c"SELECTpg_exec_stored_plan('../test.txt');"
5757

5858
psql -p 5432 -c"SELECT pg_store_query_plan('../test.txt', 'SELECT tests.select1(42);');"
59-
psql -p 5433 -c"SELECTpg_exec_query_plan('../test.txt');"
60-
psql -p 5432 -c"SELECTpg_exec_query_plan('../test.txt');"
59+
psql -p 5433 -c"SELECTpg_exec_stored_plan('../test.txt');"
60+
psql -p 5432 -c"SELECTpg_exec_stored_plan('../test.txt');"
6161

6262
psql -p 5432 -c"SELECT * FROM tests.t2;"
6363
psql -p 5433 -c"SELECT * FROM tests.t2;"
@@ -68,7 +68,7 @@ psql -p 5432 -c "SELECT oid, * FROM pg_collation WHERE collname LIKE 'test%';"
6868
psql -p 5433 -c"SELECT oid, * FROM pg_collation WHERE collname LIKE 'test%';"
6969

7070
psql -p 5432 -c"SELECT pg_store_query_plan('../test.txt', 'SELECT max(id) FROM tests.ttest1 WHERE a < b COLLATE tests.test1');"
71-
psql -p 5433 -c"SELECTpg_exec_query_plan('../test.txt');"
71+
psql -p 5433 -c"SELECTpg_exec_stored_plan('../test.txt');"
7272

7373
# OPEROID ----------------------------------------------------------------------
7474
# Check on different oids
@@ -77,11 +77,11 @@ psql -p 5433 -c "SELECT oid, oprname, oprnamespace FROM pg_operator WHERE oprnam
7777

7878
# Test
7979
psql -p 5432 -c"SELECT pg_store_query_plan('../test.txt', 'SELECT id ### 1 FROM tests.ttest1;');"
80-
psql -p 5433 -c"SELECTpg_exec_query_plan('../test.txt');"
80+
psql -p 5433 -c"SELECTpg_exec_stored_plan('../test.txt');"
8181

8282
psql -p 5433 -c"SELECT pg_store_query_plan('../test.txt', 'SELECT collname, nspname
8383
FROM pg_collation JOIN pg_namespace ON (collnamespace = pg_namespace.oid)
8484
WHERE collname LIKE ''test%''
8585
ORDER BY 1;');"
86-
psql -p 5433 -c"SELECTpg_exec_query_plan('../test.txt');"
86+
psql -p 5433 -c"SELECTpg_exec_stored_plan('../test.txt');"
8787

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp