-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathpg_dirtyread.c
164 lines (150 loc) · 5.5 KB
/
pg_dirtyread.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright (c) 1996-2024, PostgreSQL Global Development Group
* Copyright (c) 2012, OmniTI Computer Consulting, Inc.
* Portions Copyright (c) 1994, The Regents of the University of California
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name OmniTI Computer Consulting, Inc. nor the names
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "postgres.h"
#include "funcapi.h"
#include "access/heapam.h"
#if PG_VERSION_NUM >= 120000
#include "access/table.h"
#include "utils/snapmgr.h"
#else
#include "utils/tqual.h"
#endif
#include "utils/rel.h"
#include "catalog/pg_type.h"
#include "access/tupconvert.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#include "access/xlog.h" /* RecoveryInProgress */
#include "miscadmin.h" /* superuser */
#include "storage/procarray.h" /* GetOldestXmin */
#include "dirtyread_tupconvert.h"
typedef struct
{
Relation rel;
TupleDesc reltupdesc;
TupleConversionMap *map;
#if PG_VERSION_NUM >= 120000
TableScanDesc scan;
#else
HeapScanDesc scan;
#endif
OldestXminType oldest_xmin;
} pg_dirtyread_ctx;
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(pg_dirtyread);
PGDLLEXPORT Datum pg_dirtyread(PG_FUNCTION_ARGS);
Datum
pg_dirtyread(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
pg_dirtyread_ctx *usr_ctx;
HeapTuple tuplein;
if (SRF_IS_FIRSTCALL())
{
MemoryContext oldcontext;
Oid relid;
TupleDesc tupdesc;
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to use pg_dirtyread")));
relid = PG_GETARG_OID(0);
if (!OidIsValid(relid))
elog(ERROR, "invalid relation oid \"%d\"", relid);
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
usr_ctx = (pg_dirtyread_ctx *) palloc(sizeof(pg_dirtyread_ctx));
usr_ctx->rel =
#if PG_VERSION_NUM >= 120000
table_open(relid, AccessShareLock);
#else
heap_open(relid, AccessShareLock);
#endif
usr_ctx->reltupdesc = RelationGetDescr(usr_ctx->rel);
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("function returning record called in context "
"that cannot accept type record")));
funcctx->tuple_desc = BlessTupleDesc(tupdesc);
usr_ctx->map = dirtyread_convert_tuples_by_name(usr_ctx->reltupdesc,
funcctx->tuple_desc, "Error converting tuple descriptors!");
usr_ctx->scan = heap_beginscan(usr_ctx->rel, SnapshotAny, 0, NULL
#if PG_VERSION_NUM >= 120000
, NULL, SO_TYPE_SEQSCAN
#endif
);
#if PG_VERSION_NUM >= 140000
usr_ctx->oldest_xmin = GlobalVisTestFor(usr_ctx->rel);
#else
/* only call GetOldestXmin while not in recovery */
if (!RecoveryInProgress())
usr_ctx->oldest_xmin = GetOldestXmin(
#if PG_VERSION_NUM >= 90400
usr_ctx->rel
#else
false /* allDbs */
#endif
, 0);
#endif
funcctx->user_fctx = (void *) usr_ctx;
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
usr_ctx = (pg_dirtyread_ctx *) funcctx->user_fctx;
if ((tuplein = heap_getnext(usr_ctx->scan, ForwardScanDirection)) != NULL)
{
if (usr_ctx->map != NULL)
{
tuplein = dirtyread_do_convert_tuple(tuplein, usr_ctx->map, usr_ctx->oldest_xmin);
SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuplein));
}
else
SRF_RETURN_NEXT(funcctx, heap_copy_tuple_as_datum(tuplein, usr_ctx->reltupdesc));
}
else
{
heap_endscan(usr_ctx->scan);
#if PG_VERSION_NUM >= 120000
table_close(usr_ctx->rel, AccessShareLock);
#else
heap_close(usr_ctx->rel, AccessShareLock);
#endif
SRF_RETURN_DONE(funcctx);
}
}
/* vim:et
*/