1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*-------------------------------------------------------------------------
*
* queryenvironment.c
* Query environment, to store context-specific values like ephemeral named
* relations. Initial use is for named tuplestores for delta information
* from "normal" relations.
*
* The initial implementation uses a list because the number of such relations
* in any one context is expected to be very small. If that becomes a
* performance problem, the implementation can be changed with no other impact
* on callers, since this is an opaque structure. This is the reason to
* require a create function.
*
* Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/utils/misc/queryenvironment.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/table.h"
#include "utils/queryenvironment.h"
#include "utils/rel.h"
/*
* Private state of a query environment.
*/
struct QueryEnvironment
{
List *namedRelList;
};
QueryEnvironment *
create_queryEnv(void)
{
return (QueryEnvironment *) palloc0(sizeof(QueryEnvironment));
}
EphemeralNamedRelationMetadata
get_visible_ENR_metadata(QueryEnvironment *queryEnv, const char *refname)
{
EphemeralNamedRelation enr;
Assert(refname != NULL);
if (queryEnv == NULL)
return NULL;
enr = get_ENR(queryEnv, refname);
if (enr)
return &(enr->md);
return NULL;
}
/*
* Register a named relation for use in the given environment.
*
* If this is intended exclusively for planning purposes, the tstate field can
* be left NULL;
*/
void
register_ENR(QueryEnvironment *queryEnv, EphemeralNamedRelation enr)
{
Assert(enr != NULL);
Assert(get_ENR(queryEnv, enr->md.name) == NULL);
queryEnv->namedRelList = lappend(queryEnv->namedRelList, enr);
}
/*
* Unregister an ephemeral relation by name. This will probably be a rarely
* used function, but seems like it should be provided "just in case".
*/
void
unregister_ENR(QueryEnvironment *queryEnv, const char *name)
{
EphemeralNamedRelation match;
match = get_ENR(queryEnv, name);
if (match)
queryEnv->namedRelList = list_delete(queryEnv->namedRelList, match);
}
/*
* This returns an ENR if there is a name match in the given collection. It
* must quietly return NULL if no match is found.
*/
EphemeralNamedRelation
get_ENR(QueryEnvironment *queryEnv, const char *name)
{
ListCell *lc;
Assert(name != NULL);
if (queryEnv == NULL)
return NULL;
foreach(lc, queryEnv->namedRelList)
{
EphemeralNamedRelation enr = (EphemeralNamedRelation) lfirst(lc);
if (strcmp(enr->md.name, name) == 0)
return enr;
}
return NULL;
}
/*
* Gets the TupleDesc for a Ephemeral Named Relation, based on which field was
* filled.
*
* When the TupleDesc is based on a relation from the catalogs, we count on
* that relation being used at the same time, so that appropriate locks will
* already be held. Locking here would be too late anyway.
*/
TupleDesc
ENRMetadataGetTupDesc(EphemeralNamedRelationMetadata enrmd)
{
TupleDesc tupdesc;
/* One, and only one, of these fields must be filled. */
Assert((enrmd->reliddesc == InvalidOid) != (enrmd->tupdesc == NULL));
if (enrmd->tupdesc != NULL)
tupdesc = enrmd->tupdesc;
else
{
Relation relation;
relation = table_open(enrmd->reliddesc, NoLock);
tupdesc = relation->rd_att;
table_close(relation, NoLock);
}
return tupdesc;
}
|