Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c0b0076

Browse files
committed
Rearrange snapshot handling to make rule expansion more consistent.
With this patch, portals, SQL functions, and SPI all agree that there should be only a CommandCounterIncrement between the queries that are generated from a single SQL command by rule expansion. Fetching a whole new snapshot now happens only between original queries. This is equivalent to the existing behavior of EXPLAIN ANALYZE, and it was judged to be the best choice since it eliminates one source of concurrency hazards for rules. The patch should also make things marginally faster by reducing the number of snapshot push/pop operations. The patch removes pg_parse_and_rewrite(), which is no longer used anywhere. There was considerable discussion about more aggressive refactoring of the query-processing functions exported by postgres.c, but for the moment nothing more has been done there. I also took the opportunity to refactor snapmgr.c's API slightly: the former PushUpdatedSnapshot() has been split into two functions. Marko Tiikkaja, reviewed by Steve Singer and Tom Lane
1 parent 57e9bda commit c0b0076

File tree

10 files changed

+389
-217
lines changed

10 files changed

+389
-217
lines changed

src/backend/catalog/pg_proc.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,9 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
764764
Oid funcoid = PG_GETARG_OID(0);
765765
HeapTuple tuple;
766766
Form_pg_proc proc;
767+
List *raw_parsetree_list;
767768
List *querytree_list;
769+
ListCell *lc;
768770
bool isnull;
769771
Datum tmp;
770772
char *prosrc;
@@ -835,17 +837,32 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
835837
* We can run the text through the raw parser though; this will at
836838
* least catch silly syntactic errors.
837839
*/
840+
raw_parsetree_list = pg_parse_query(prosrc);
841+
838842
if (!haspolyarg)
839843
{
840-
querytree_list = pg_parse_and_rewrite(prosrc,
841-
proc->proargtypes.values,
842-
proc->pronargs);
844+
/*
845+
* OK to do full precheck: analyze and rewrite the queries,
846+
* then verify the result type.
847+
*/
848+
querytree_list = NIL;
849+
foreach(lc, raw_parsetree_list)
850+
{
851+
Node *parsetree = (Node *) lfirst(lc);
852+
List *querytree_sublist;
853+
854+
querytree_sublist = pg_analyze_and_rewrite(parsetree,
855+
prosrc,
856+
proc->proargtypes.values,
857+
proc->pronargs);
858+
querytree_list = list_concat(querytree_list,
859+
querytree_sublist);
860+
}
861+
843862
(void) check_sql_fn_retval(funcoid, proc->prorettype,
844863
querytree_list,
845864
NULL, NULL);
846865
}
847-
else
848-
querytree_list = pg_parse_query(prosrc);
849866

850867
error_context_stack = sqlerrcontext.previous;
851868
}

src/backend/commands/copy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,8 @@ BeginCopy(bool is_from,
12161216
* Use a snapshot with an updated command ID to ensure this query sees
12171217
* results of any previously executed queries.
12181218
*/
1219-
PushUpdatedSnapshot(GetActiveSnapshot());
1219+
PushCopiedSnapshot(GetActiveSnapshot());
1220+
UpdateActiveSnapshotCommandId();
12201221

12211222
/* Create dest receiver for COPY OUT */
12221223
dest = CreateDestReceiver(DestCopyOut);

src/backend/commands/explain.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,8 @@ ExplainOnePlan(PlannedStmt *plannedstmt, ExplainState *es,
366366
* Use a snapshot with an updated command ID to ensure this query sees
367367
* results of any previously executed queries.
368368
*/
369-
PushUpdatedSnapshot(GetActiveSnapshot());
369+
PushCopiedSnapshot(GetActiveSnapshot());
370+
UpdateActiveSnapshotCommandId();
370371

371372
/* Create a QueryDesc requesting no output */
372373
queryDesc = CreateQueryDesc(plannedstmt, queryString,

0 commit comments

Comments
 (0)