Skip to content

Commit 2698e55

Browse files
author
hackorum
committed
Apply use_enr_like_temptable.patch
1 parent 4255812 commit 2698e55

5 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/backend/executor/spi.c‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "access/xact.h"
2121
#include "catalog/heap.h"
2222
#include "catalog/pg_type.h"
23+
#include "commands/portalcmds.h"
2324
#include "commands/trigger.h"
2425
#include "executor/executor.h"
2526
#include "executor/spi_priv.h"
@@ -3435,3 +3436,30 @@ SPI_register_trigger_data(TriggerData *tdata)
34353436

34363437
return SPI_OK_TD_REGISTER;
34373438
}
3439+
3440+
int
3441+
SPI_register_portal(Portal portal, char *name)
3442+
{
3443+
int rc;
3444+
EphemeralNamedRelation enr;
3445+
3446+
if (portal == NULL || name == NULL)
3447+
return SPI_ERROR_ARGUMENT;
3448+
3449+
PortalCreateHoldStore(portal);
3450+
PersistHoldablePortal(portal);
3451+
3452+
enr = palloc(sizeof(EphemeralNamedRelationData));
3453+
3454+
enr->md.name = name;
3455+
enr->md.reliddesc = InvalidOid;
3456+
enr->md.tupdesc = portal->tupDesc;
3457+
enr->md.enrtype = ENR_NAMED_TUPLESTORE;
3458+
enr->md.enrtuples = tuplestore_tuple_count(portal->holdStore);
3459+
enr->reldata = portal->holdStore;
3460+
rc = SPI_register_relation(enr);
3461+
if (rc != SPI_OK_REL_REGISTER)
3462+
return rc;
3463+
3464+
return SPI_OK_TD_REGISTER;
3465+
}

‎src/include/executor/spi.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ extern void SPI_cursor_close(Portal portal);
193193
extern int SPI_register_relation(EphemeralNamedRelation enr);
194194
extern int SPI_unregister_relation(const char *name);
195195
extern int SPI_register_trigger_data(TriggerData *tdata);
196+
extern int SPI_register_portal(Portal portal, char *name);
196197

197198
extern void SPI_start_transaction(void);
198199
extern void SPI_commit(void);

‎src/pl/plpgsql/src/pl_exec.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4935,6 +4935,9 @@ exec_stmt_open(PLpgSQL_execstate *estate, PLpgSQL_stmt_open *stmt)
49354935
elog(ERROR, "could not open cursor: %s",
49364936
SPI_result_code_string(SPI_result));
49374937

4938+
if (stmt->tablename)
4939+
SPI_register_portal(portal, stmt->tablename);
4940+
49384941
/*
49394942
* If cursor variable was NULL, store the generated portal name in it,
49404943
* after verifying it's okay to assign to.

‎src/pl/plpgsql/src/pl_gram.y‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static void check_raise_parameters(PLpgSQL_stmt_raise *stmt);
200200
%type <ival> foreach_slice
201201
%type <stmt> for_control
202202

203-
%type <str> any_identifier opt_block_label opt_loop_label opt_label
203+
%type <str> any_identifier opt_block_label opt_into_table opt_loop_label opt_label
204204
%type <str> option_value
205205

206206
%type <list> proc_sect stmt_elsifs stmt_else
@@ -2100,7 +2100,17 @@ stmt_dynexecute : K_EXECUTE
21002100
;
21012101

21022102

2103-
stmt_open : K_OPEN cursor_variable
2103+
opt_into_table :
2104+
{
2105+
$$ = NULL;
2106+
}
2107+
| K_INTO K_TABLE any_identifier
2108+
{
2109+
$$ = $3;
2110+
}
2111+
;
2112+
2113+
stmt_open : K_OPEN cursor_variable opt_into_table
21042114
{
21052115
PLpgSQL_stmt_open *new;
21062116
int tok;
@@ -2111,6 +2121,7 @@ stmt_open : K_OPEN cursor_variable
21112121
new->stmtid = ++plpgsql_curr_compile->nstatements;
21122122
new->curvar = $2->dno;
21132123
new->cursor_options = CURSOR_OPT_FAST_PLAN;
2124+
new->tablename = $3;
21142125

21152126
if ($2->cursor_explicit_expr == NULL)
21162127
{

‎src/pl/plpgsql/src/plpgsql.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ typedef struct PLpgSQL_stmt_open
795795
PLpgSQL_expr *query;
796796
PLpgSQL_expr *dynquery;
797797
List *params; /* USING expressions */
798+
char *tablename;
798799
} PLpgSQL_stmt_open;
799800

800801
/*

0 commit comments

Comments
 (0)