Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
resource_owner.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "guard.hpp"
7#include "imports.h"
8
9extern "C" {
10#include <executor/executor.h>
11#include <utils/plancache.h>
12#include <utils/resowner.h>
13}
14
15namespace cppgres {
16
25 explicit resource_owner(const char *name, ::ResourceOwner parent = nullptr)
26 : owner(ffi_guard{::ResourceOwnerCreate}(parent, name)) {}
27
28 resource_owner(const resource_owner &) = delete;
29 resource_owner &operator=(const resource_owner &) = delete;
30
31 resource_owner(resource_owner &&other) noexcept : owner(other.owner) { other.owner = nullptr; }
32
33 resource_owner &operator=(resource_owner &&other) noexcept {
34 if (this != &other) {
35 dispose();
36 owner = other.owner;
37 other.owner = nullptr;
38 }
39 return *this;
40 }
41
42 operator ::ResourceOwner() const { return owner; }
43
47 ::ResourceOwner release() noexcept {
48 ::ResourceOwner o = owner;
49 owner = nullptr;
50 return o;
51 }
52
53 ~resource_owner() noexcept { dispose(); }
54
55private:
56 void dispose() noexcept {
57 if (owner == nullptr) {
58 return;
59 }
60 try {
61 // Releasing plan cache references when there are none is harmless,
62 // so always do both. (Renamed in Postgres 17.)
63#if PG_MAJORVERSION_NUM >= 17
64 ffi_guard{::ReleaseAllPlanCacheRefsInOwner}(owner);
65#else
66 ffi_guard{::ResourceOwnerReleaseAllPlanCacheRefs}(owner);
67#endif
68 ffi_guard{::ResourceOwnerDelete}(owner);
69 } catch (...) {
70 elog(WARNING, "cppgres: deleting resource owner failed");
71 }
72 owner = nullptr;
73 }
74
75 ::ResourceOwner owner;
76};
77
86 executor_state() : estate(ffi_guard{::CreateExecutorState}()) {}
87
88 executor_state(const executor_state &) = delete;
89 executor_state &operator=(const executor_state &) = delete;
90
91 executor_state(executor_state &&other) noexcept : estate(other.estate) {
92 other.estate = nullptr;
93 }
94
95 executor_state &operator=(executor_state &&other) noexcept {
96 if (this != &other) {
97 dispose();
98 estate = other.estate;
99 other.estate = nullptr;
100 }
101 return *this;
102 }
103
104 operator ::EState *() const { return estate; }
105
109 ::EState *release() noexcept {
110 ::EState *e = estate;
111 estate = nullptr;
112 return e;
113 }
114
115 ~executor_state() noexcept { dispose(); }
116
117private:
118 void dispose() noexcept {
119 if (estate == nullptr) {
120 return;
121 }
122 try {
123 ffi_guard{::FreeExecutorState}(estate);
124 } catch (...) {
125 elog(WARNING, "cppgres: freeing executor state failed");
126 }
127 estate = nullptr;
128 }
129
130 ::EState *estate;
131};
132
133} // namespace cppgres
RAII-managed executor state (EState)
Definition: resource_owner.hpp:85
::EState * release() noexcept
Disown the executor state and return it.
Definition: resource_owner.hpp:109
Definition: guard.hpp:20
Definition: name.hpp:7
RAII-managed resource owner.
Definition: resource_owner.hpp:24
::ResourceOwner release() noexcept
Disown the resource owner and return it.
Definition: resource_owner.hpp:47