Skip to content

[pull] master from postgres:master #136

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 2 commits into from
Jul 2, 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
141 changes: 139 additions & 2 deletions contrib/pg_stat_statements/expected/cursors.out
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
1 | 0 | COMMIT
1 | 0 | DECLARE cursor_stats_1 CURSOR WITH HOLD FOR SELECT $1
1 | 0 | DECLARE cursor_stats_2 CURSOR WITH HOLD FOR SELECT $1
1 | 1 | FETCH 1 IN cursor_stats_1
1 | 1 | FETCH 1 IN cursor_stats_2
1 | 1 | FETCH $1 IN cursor_stats_1
1 | 1 | FETCH $1 IN cursor_stats_2
1 | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(9 rows)

Expand All @@ -68,3 +68,140 @@ SELECT pg_stat_statements_reset() IS NOT NULL AS t;
t
(1 row)

-- Normalization of FETCH statements
BEGIN;
DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series(1, 10);
-- implicit directions
FETCH pgss_cursor;
--
(1 row)

FETCH 1 pgss_cursor;
--
(1 row)

FETCH 2 pgss_cursor;
--
(2 rows)

FETCH -1 pgss_cursor;
--
(1 row)

-- explicit NEXT
FETCH NEXT pgss_cursor;
--
(1 row)

-- explicit PRIOR
FETCH PRIOR pgss_cursor;
--
(1 row)

-- explicit FIRST
FETCH FIRST pgss_cursor;
--
(1 row)

-- explicit LAST
FETCH LAST pgss_cursor;
--
(1 row)

-- explicit ABSOLUTE
FETCH ABSOLUTE 1 pgss_cursor;
--
(1 row)

FETCH ABSOLUTE 2 pgss_cursor;
--
(1 row)

FETCH ABSOLUTE -1 pgss_cursor;
--
(1 row)

-- explicit RELATIVE
FETCH RELATIVE 1 pgss_cursor;
--
(0 rows)

FETCH RELATIVE 2 pgss_cursor;
--
(0 rows)

FETCH RELATIVE -1 pgss_cursor;
--
(1 row)

-- explicit FORWARD
FETCH ALL pgss_cursor;
--
(0 rows)

-- explicit FORWARD ALL
FETCH FORWARD ALL pgss_cursor;
--
(0 rows)

-- explicit FETCH FORWARD
FETCH FORWARD pgss_cursor;
--
(0 rows)

FETCH FORWARD 1 pgss_cursor;
--
(0 rows)

FETCH FORWARD 2 pgss_cursor;
--
(0 rows)

FETCH FORWARD -1 pgss_cursor;
--
(1 row)

-- explicit FETCH BACKWARD
FETCH BACKWARD pgss_cursor;
--
(1 row)

FETCH BACKWARD 1 pgss_cursor;
--
(1 row)

FETCH BACKWARD 2 pgss_cursor;
--
(2 rows)

FETCH BACKWARD -1 pgss_cursor;
--
(1 row)

-- explicit BACKWARD ALL
FETCH BACKWARD ALL pgss_cursor;
--
(6 rows)

COMMIT;
SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";
calls | query
-------+--------------------------------------------------------------------
1 | BEGIN
1 | COMMIT
1 | DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series($1, $2)
3 | FETCH ABSOLUTE $1 pgss_cursor
1 | FETCH ALL pgss_cursor
1 | FETCH BACKWARD ALL pgss_cursor
4 | FETCH BACKWARD pgss_cursor
1 | FETCH FIRST pgss_cursor
1 | FETCH FORWARD ALL pgss_cursor
4 | FETCH FORWARD pgss_cursor
1 | FETCH LAST pgss_cursor
1 | FETCH NEXT pgss_cursor
1 | FETCH PRIOR pgss_cursor
3 | FETCH RELATIVE $1 pgss_cursor
4 | FETCH pgss_cursor
1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(16 rows)

4 changes: 2 additions & 2 deletions contrib/pg_stat_statements/expected/level_tracking.out
Original file line number Diff line number Diff line change
Expand Up @@ -1147,7 +1147,7 @@ SELECT toplevel, calls, query FROM pg_stat_statements
t | 1 | COMMIT
t | 1 | DECLARE FOOCUR CURSOR FOR SELECT * from stats_track_tab
f | 1 | DECLARE FOOCUR CURSOR FOR SELECT * from stats_track_tab;
t | 1 | FETCH FORWARD 1 FROM foocur
t | 1 | FETCH FORWARD $1 FROM foocur
t | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(7 rows)

Expand Down Expand Up @@ -1176,7 +1176,7 @@ SELECT toplevel, calls, query FROM pg_stat_statements
t | 1 | CLOSE foocur
t | 1 | COMMIT
t | 1 | DECLARE FOOCUR CURSOR FOR SELECT * FROM stats_track_tab
t | 1 | FETCH FORWARD 1 FROM foocur
t | 1 | FETCH FORWARD $1 FROM foocur
t | 1 | SELECT pg_stat_statements_reset() IS NOT NULL AS t
(6 rows)

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 @@ -702,7 +702,7 @@ SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
1 | 13 | CREATE MATERIALIZED VIEW pgss_matv AS SELECT * FROM pgss_ctas
1 | 10 | CREATE TABLE pgss_ctas AS SELECT a, $1 b FROM generate_series($2, $3) a
1 | 0 | DECLARE pgss_cursor CURSOR FOR SELECT * FROM pgss_matv
1 | 5 | FETCH FORWARD 5 pgss_cursor
1 | 5 | FETCH FORWARD $1 pgss_cursor
1 | 7 | FETCH FORWARD ALL pgss_cursor
1 | 1 | FETCH NEXT pgss_cursor
1 | 13 | REFRESH MATERIALIZED VIEW pgss_matv
Expand Down
43 changes: 43 additions & 0 deletions contrib/pg_stat_statements/sql/cursors.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,46 @@ COMMIT;

SELECT calls, rows, query FROM pg_stat_statements ORDER BY query COLLATE "C";
SELECT pg_stat_statements_reset() IS NOT NULL AS t;

-- Normalization of FETCH statements
BEGIN;
DECLARE pgss_cursor CURSOR FOR SELECT FROM generate_series(1, 10);
-- implicit directions
FETCH pgss_cursor;
FETCH 1 pgss_cursor;
FETCH 2 pgss_cursor;
FETCH -1 pgss_cursor;
-- explicit NEXT
FETCH NEXT pgss_cursor;
-- explicit PRIOR
FETCH PRIOR pgss_cursor;
-- explicit FIRST
FETCH FIRST pgss_cursor;
-- explicit LAST
FETCH LAST pgss_cursor;
-- explicit ABSOLUTE
FETCH ABSOLUTE 1 pgss_cursor;
FETCH ABSOLUTE 2 pgss_cursor;
FETCH ABSOLUTE -1 pgss_cursor;
-- explicit RELATIVE
FETCH RELATIVE 1 pgss_cursor;
FETCH RELATIVE 2 pgss_cursor;
FETCH RELATIVE -1 pgss_cursor;
-- explicit FORWARD
FETCH ALL pgss_cursor;
-- explicit FORWARD ALL
FETCH FORWARD ALL pgss_cursor;
-- explicit FETCH FORWARD
FETCH FORWARD pgss_cursor;
FETCH FORWARD 1 pgss_cursor;
FETCH FORWARD 2 pgss_cursor;
FETCH FORWARD -1 pgss_cursor;
-- explicit FETCH BACKWARD
FETCH BACKWARD pgss_cursor;
FETCH BACKWARD 1 pgss_cursor;
FETCH BACKWARD 2 pgss_cursor;
FETCH BACKWARD -1 pgss_cursor;
-- explicit BACKWARD ALL
FETCH BACKWARD ALL pgss_cursor;
COMMIT;
SELECT calls, query FROM pg_stat_statements ORDER BY query COLLATE "C";
50 changes: 41 additions & 9 deletions src/backend/parser/gram.y
Original file line number Diff line number Diff line change
Expand Up @@ -7477,6 +7477,8 @@ fetch_args: cursor_name
n->portalname = $1;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| from_in cursor_name
Expand All @@ -7486,6 +7488,19 @@ fetch_args: cursor_name
n->portalname = $2;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);

n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = $1;
n->location = @1;
n->direction_keyword = FETCH_KEYWORD_NONE;
$$ = (Node *) n;
}
| NEXT opt_from_in cursor_name
Expand All @@ -7495,6 +7510,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_NEXT;
$$ = (Node *) n;
}
| PRIOR opt_from_in cursor_name
Expand All @@ -7504,6 +7521,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_BACKWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_PRIOR;
$$ = (Node *) n;
}
| FIRST_P opt_from_in cursor_name
Expand All @@ -7513,6 +7532,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FIRST;
$$ = (Node *) n;
}
| LAST_P opt_from_in cursor_name
Expand All @@ -7522,6 +7543,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_ABSOLUTE;
n->howMany = -1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_LAST;
$$ = (Node *) n;
}
| ABSOLUTE_P SignedIconst opt_from_in cursor_name
Expand All @@ -7531,6 +7554,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_ABSOLUTE;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_ABSOLUTE;
$$ = (Node *) n;
}
| RELATIVE_P SignedIconst opt_from_in cursor_name
Expand All @@ -7540,15 +7565,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_RELATIVE;
n->howMany = $2;
$$ = (Node *) n;
}
| SignedIconst opt_from_in cursor_name
{
FetchStmt *n = makeNode(FetchStmt);

n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = $1;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_RELATIVE;
$$ = (Node *) n;
}
| ALL opt_from_in cursor_name
Expand All @@ -7558,6 +7576,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_ALL;
$$ = (Node *) n;
}
| FORWARD opt_from_in cursor_name
Expand All @@ -7567,6 +7587,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_FORWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FORWARD;
$$ = (Node *) n;
}
| FORWARD SignedIconst opt_from_in cursor_name
Expand All @@ -7576,6 +7598,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_FORWARD;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_FORWARD;
$$ = (Node *) n;
}
| FORWARD ALL opt_from_in cursor_name
Expand All @@ -7585,6 +7609,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_FORWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_FORWARD_ALL;
$$ = (Node *) n;
}
| BACKWARD opt_from_in cursor_name
Expand All @@ -7594,6 +7620,8 @@ fetch_args: cursor_name
n->portalname = $3;
n->direction = FETCH_BACKWARD;
n->howMany = 1;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_BACKWARD;
$$ = (Node *) n;
}
| BACKWARD SignedIconst opt_from_in cursor_name
Expand All @@ -7603,6 +7631,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_BACKWARD;
n->howMany = $2;
n->location = @2;
n->direction_keyword = FETCH_KEYWORD_BACKWARD;
$$ = (Node *) n;
}
| BACKWARD ALL opt_from_in cursor_name
Expand All @@ -7612,6 +7642,8 @@ fetch_args: cursor_name
n->portalname = $4;
n->direction = FETCH_BACKWARD;
n->howMany = FETCH_ALL;
n->location = -1;
n->direction_keyword = FETCH_KEYWORD_BACKWARD_ALL;
$$ = (Node *) n;
}
;
Expand Down
1 change: 1 addition & 0 deletions src/backend/utils/adt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ OBJS = \
arrayutils.o \
ascii.o \
bool.o \
bytea.o \
cash.o \
char.o \
cryptohashfuncs.o \
Expand Down
Loading