Skip to content

[pull] master from postgres:master #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions contrib/pg_overexplain/expected/pg_overexplain.out
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ EXPLAIN (DEBUG) SELECT 1;
Subplans Needing Rewind: none
Relation OIDs: none
Executor Parameter Types: none
Parse Location: 16 for 8 bytes
Parse Location: 0 to end
(11 rows)

EXPLAIN (RANGE_TABLE) SELECT 1;
Expand Down Expand Up @@ -119,7 +119,7 @@ $$);
Subplans Needing Rewind: none
Relation OIDs: NNN...
Executor Parameter Types: none
Parse Location: 41 to end
Parse Location: 0 to end
RTI 1 (relation, inherited, in-from-clause):
Eref: vegetables (id, name, genus)
Relation: vegetables
Expand Down Expand Up @@ -240,7 +240,7 @@ $$);
<Subplans-Needing-Rewind>none</Subplans-Needing-Rewind> +
<Relation-OIDs>NNN...</Relation-OIDs> +
<Executor-Parameter-Types>none</Executor-Parameter-Types> +
<Parse-Location>53 to end</Parse-Location> +
<Parse-Location>0 to end</Parse-Location> +
</PlannedStmt> +
<Range-Table> +
<Range-Table-Entry> +
Expand Down Expand Up @@ -344,7 +344,7 @@ $$);
Subplans Needing Rewind: none
Relation OIDs: NNN...
Executor Parameter Types: none
Parse Location: 28 to end
Parse Location: 0 to end
(37 rows)

SET debug_parallel_query = false;
Expand Down Expand Up @@ -372,7 +372,7 @@ $$);
Subplans Needing Rewind: none
Relation OIDs: NNN...
Executor Parameter Types: 0
Parse Location: 28 to end
Parse Location: 0 to end
(15 rows)

-- Create an index, and then attempt to force a nested loop with inner index
Expand Down Expand Up @@ -436,7 +436,7 @@ $$);
Subplans Needing Rewind: none
Relation OIDs: NNN...
Executor Parameter Types: 23
Parse Location: 75 for 62 bytes
Parse Location: 0 to end
(47 rows)

RESET enable_hashjoin;
Expand Down
182 changes: 93 additions & 89 deletions contrib/pg_stat_statements/expected/level_tracking.out

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions contrib/pg_stat_statements/expected/planning.out
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ SELECT 42;
(1 row)

SELECT plans, calls, rows, query FROM pg_stat_statements
WHERE query NOT LIKE 'SELECT COUNT%' ORDER BY query COLLATE "C";
WHERE query NOT LIKE 'PREPARE%' ORDER BY query COLLATE "C";
plans | calls | rows | query
-------+-------+------+----------------------------------------------------------
0 | 1 | 0 | ALTER TABLE stats_plan_test ADD COLUMN x int
Expand All @@ -72,10 +72,10 @@ SELECT plans, calls, rows, query FROM pg_stat_statements
-- for the prepared statement we expect at least one replan, but cache
-- invalidations could force more
SELECT plans >= 2 AND plans <= calls AS plans_ok, calls, rows, query FROM pg_stat_statements
WHERE query LIKE 'SELECT COUNT%' ORDER BY query COLLATE "C";
plans_ok | calls | rows | query
----------+-------+------+--------------------------------------
t | 4 | 4 | SELECT COUNT(*) FROM stats_plan_test
WHERE query LIKE 'PREPARE%' ORDER BY query COLLATE "C";
plans_ok | calls | rows | query
----------+-------+------+-------------------------------------------------------
t | 4 | 4 | PREPARE prep1 AS SELECT COUNT(*) FROM stats_plan_test
(1 row)

-- Cleanup
Expand Down
2 changes: 1 addition & 1 deletion contrib/pg_stat_statements/expected/select.out
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ DEALLOCATE pgss_test;
SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | rows | query
-------+------+------------------------------------------------------------------------------
1 | 1 | PREPARE pgss_test (int) AS SELECT $1, $2 LIMIT $3
4 | 4 | SELECT $1 +
| | -- but this one will appear +
| | AS "text"
Expand All @@ -221,7 +222,6 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
2 | 2 | SELECT $1 AS "int" ORDER BY 1
1 | 2 | SELECT $1 AS i UNION SELECT $2 ORDER BY i
1 | 1 | SELECT $1 || $2
1 | 1 | SELECT $1, $2 LIMIT $3
2 | 2 | SELECT DISTINCT $1 AS "int"
0 | 0 | SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C"
1 | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
Expand Down
2 changes: 1 addition & 1 deletion contrib/pg_stat_statements/expected/utility.out
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
-------+------+----------------------------------------------------
2 | 0 | DEALLOCATE $1
2 | 0 | DEALLOCATE ALL
2 | 2 | SELECT $1 AS a
2 | 2 | PREPARE stat_select AS SELECT $1 AS a
1 | 1 | SELECT $1 as a
1 | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(5 rows)
Expand Down
4 changes: 2 additions & 2 deletions contrib/pg_stat_statements/sql/planning.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ SELECT 42;
SELECT 42;
SELECT 42;
SELECT plans, calls, rows, query FROM pg_stat_statements
WHERE query NOT LIKE 'SELECT COUNT%' ORDER BY query COLLATE "C";
WHERE query NOT LIKE 'PREPARE%' ORDER BY query COLLATE "C";
-- for the prepared statement we expect at least one replan, but cache
-- invalidations could force more
SELECT plans >= 2 AND plans <= calls AS plans_ok, calls, rows, query FROM pg_stat_statements
WHERE query LIKE 'SELECT COUNT%' ORDER BY query COLLATE "C";
WHERE query LIKE 'PREPARE%' ORDER BY query COLLATE "C";

-- Cleanup
DROP TABLE stats_plan_test;
Expand Down
90 changes: 5 additions & 85 deletions src/backend/parser/analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,103 +238,24 @@ parse_sub_analyze(Node *parseTree, ParseState *parentParseState,
return query;
}

/*
* setQueryLocationAndLength
* Set query's location and length from statement and ParseState
*
* Some statements, like PreparableStmt, can be located within parentheses.
* For example "(SELECT 1)" or "COPY (UPDATE ...) to x;". For those, we
* cannot use the whole string from the statement's location or the SQL
* string would yield incorrectly. The parser will set stmt_len, reflecting
* the size of the statement within the parentheses. Thus, when stmt_len is
* available, we need to use it for the Query's stmt_len.
*
* For other cases, the parser can't provide the length of individual
* statements. However, we have the statement's location plus the length
* (p_stmt_len) and location (p_stmt_location) of the top level RawStmt,
* stored in pstate. Thus, the statement's length is the RawStmt's length
* minus how much we've advanced in the RawStmt's string. If p_stmt_len
* is 0, the SQL string is used up to its end.
*/
static void
setQueryLocationAndLength(ParseState *pstate, Query *qry, Node *parseTree)
{
ParseLoc stmt_len = 0;

switch (nodeTag(parseTree))
{
case T_InsertStmt:
qry->stmt_location = ((InsertStmt *) parseTree)->stmt_location;
stmt_len = ((InsertStmt *) parseTree)->stmt_len;
break;

case T_DeleteStmt:
qry->stmt_location = ((DeleteStmt *) parseTree)->stmt_location;
stmt_len = ((DeleteStmt *) parseTree)->stmt_len;
break;

case T_UpdateStmt:
qry->stmt_location = ((UpdateStmt *) parseTree)->stmt_location;
stmt_len = ((UpdateStmt *) parseTree)->stmt_len;
break;

case T_MergeStmt:
qry->stmt_location = ((MergeStmt *) parseTree)->stmt_location;
stmt_len = ((MergeStmt *) parseTree)->stmt_len;
break;

case T_SelectStmt:
qry->stmt_location = ((SelectStmt *) parseTree)->stmt_location;
stmt_len = ((SelectStmt *) parseTree)->stmt_len;
break;

case T_PLAssignStmt:
qry->stmt_location = ((PLAssignStmt *) parseTree)->location;
break;

default:
qry->stmt_location = pstate->p_stmt_location;
break;
}

if (stmt_len > 0)
{
/* Statement's length is known, use it */
qry->stmt_len = stmt_len;
}
else if (pstate->p_stmt_len > 0)
{
/*
* The top RawStmt's length is known, so calculate the statement's
* length from the statement's location and the RawStmt's length and
* location.
*/
qry->stmt_len = pstate->p_stmt_len - (qry->stmt_location - pstate->p_stmt_location);
}

/* The calculated statement length should be calculated as positive. */
Assert(qry->stmt_len >= 0);
}

/*
* transformTopLevelStmt -
* transform a Parse tree into a Query tree.
*
* This function is just responsible for storing location data
* from the RawStmt into the ParseState.
* This function is just responsible for transferring statement location data
* from the RawStmt into the finished Query.
*/
Query *
transformTopLevelStmt(ParseState *pstate, RawStmt *parseTree)
{
Query *result;

/* Store RawStmt's length and location in pstate */
pstate->p_stmt_len = parseTree->stmt_len;
pstate->p_stmt_location = parseTree->stmt_location;

/* We're at top level, so allow SELECT INTO */
result = transformOptionalSelectInto(pstate, parseTree->stmt);

result->stmt_location = parseTree->stmt_location;
result->stmt_len = parseTree->stmt_len;

return result;
}

Expand Down Expand Up @@ -503,7 +424,6 @@ transformStmt(ParseState *pstate, Node *parseTree)
/* Mark as original query until we learn differently */
result->querySource = QSRC_ORIGINAL;
result->canSetTag = true;
setQueryLocationAndLength(pstate, result, parseTree);

return result;
}
Expand Down
Loading