diff --git a/src/mongo/db/query/optimizer/syntax/expr.cpp b/src/mongo/db/query/optimizer/syntax/expr.cpp
index 4bf8f18f562..7842efa8233 100644
--- a/src/mongo/db/query/optimizer/syntax/expr.cpp
+++ b/src/mongo/db/query/optimizer/syntax/expr.cpp
@@ -36,17 +36,30 @@
 #include "mongo/db/query/optimizer/node.h"  // IWYU pragma: keep
 #include "mongo/db/storage/key_string.h"
 #include "mongo/platform/decimal128.h"
+#include <cstddef>
 
 namespace mongo::optimizer {
 
 using namespace sbe::value;
 
-Constant::Constant(TypeTags tag, sbe::value::Value val) : _tag(tag), _val(val) {}
+Constant::Constant(TypeTags tag, sbe::value::Value val) : _tag(tag) {
+    static_assert(sizeof(sbe::value::Value) >= sizeof(RefCountedValue*));
+    if (isShallowType(tag)) {
+        _val = val;
+    } else {
+        RefCountedValue* valRefPtr = new RefCountedValue(val);
+        _val = reinterpret_cast<sbe::value::Value>(valRefPtr);
+    }
+}
 
 Constant::Constant(const Constant& other) {
-    auto [tag, val] = copyValue(other._tag, other._val);
-    _tag = tag;
-    _val = val;
+    static_assert(sizeof(sbe::value::Value) == sizeof(RefCountedValue*));
+    _tag = other._tag;
+    _val = other._val;
+    if (!isShallowType(_tag)) {
+        RefCountedValue* valRefPtr = reinterpret_cast<RefCountedValue*>(_val);
+        valRefPtr->inc();
+    }
 }
 
 Constant::Constant(Constant&& other) noexcept {
@@ -123,7 +136,10 @@ ABT Constant::maxKey() {
 }
 
 Constant::~Constant() {
-    releaseValue(_tag, _val);
+    if (!isShallowType(_tag)) {
+        RefCountedValue* valRefPtr = reinterpret_cast<RefCountedValue*>(_val);
+        valRefPtr->dec(_tag);
+    }
 }
 
 bool Constant::operator==(const Constant& other) const {
@@ -139,15 +155,15 @@ bool Constant::operator==(const Constant& other) const {
         if (_tag != other._tag) {
             return false;
         } else if (_tag == sbe::value::TypeTags::makeObjSpec) {
-            const auto& mosView = *sbe::value::getMakeObjSpecView(_val);
-            const auto& otherMosView = *sbe::value::getMakeObjSpecView(other._val);
+            const auto& mosView = *sbe::value::getMakeObjSpecView(val());
+            const auto& otherMosView = *sbe::value::getMakeObjSpecView(other.val());
             return mosView == otherMosView;
         }
 
         MONGO_UNREACHABLE_TASSERT(7936707);
     }
 
-    const auto [compareTag, compareVal] = compareValue(_tag, _val, other._tag, other._val);
+    const auto [compareTag, compareVal] = compareValue(_tag, val(), other._tag, other.val());
     uassert(7086702, "Invalid comparison result", compareTag == sbe::value::TypeTags::NumberInt32);
     return sbe::value::bitcastTo<int32_t>(compareVal) == 0;
 }
@@ -157,7 +173,7 @@ bool Constant::isString() const {
 }
 
 StringData Constant::getString() const {
-    return getStringView(_tag, _val);
+    return getStringView(_tag, val());
 }
 
 bool Constant::isValueInt64() const {
@@ -166,7 +182,7 @@ bool Constant::isValueInt64() const {
 
 int64_t Constant::getValueInt64() const {
     uassert(6624057, "Constant value type is not int64_t", isValueInt64());
-    return bitcastTo<int64_t>(_val);
+    return bitcastTo<int64_t>(val());
 }
 
 bool Constant::isValueInt32() const {
@@ -175,7 +191,7 @@ bool Constant::isValueInt32() const {
 
 int32_t Constant::getValueInt32() const {
     uassert(6624354, "Constant value type is not int32_t", isValueInt32());
-    return bitcastTo<int32_t>(_val);
+    return bitcastTo<int32_t>(val());
 }
 
 bool Constant::isValueDouble() const {
@@ -184,7 +200,7 @@ bool Constant::isValueDouble() const {
 
 double Constant::getValueDouble() const {
     uassert(673180, "Constant value type is not double", isValueDouble());
-    return bitcastTo<double>(_val);
+    return bitcastTo<double>(val());
 }
 
 bool Constant::isValueDecimal() const {
@@ -193,7 +209,7 @@ bool Constant::isValueDecimal() const {
 
 Decimal128 Constant::getValueDecimal() const {
     uassert(673181, "Constant value type is not Decimal128", isValueDecimal());
-    return bitcastTo<Decimal128>(_val);
+    return bitcastTo<Decimal128>(val());
 }
 
 bool Constant::isValueBool() const {
@@ -202,7 +218,7 @@ bool Constant::isValueBool() const {
 
 bool Constant::getValueBool() const {
     uassert(6624356, "Constant value type is not bool", isValueBool());
-    return bitcastTo<bool>(_val);
+    return bitcastTo<bool>(val());
 }
 
 }  // namespace mongo::optimizer
diff --git a/src/mongo/db/query/optimizer/syntax/expr.h b/src/mongo/db/query/optimizer/syntax/expr.h
index b42bd79f83f..41dd6373302 100644
--- a/src/mongo/db/query/optimizer/syntax/expr.h
+++ b/src/mongo/db/query/optimizer/syntax/expr.h
@@ -54,6 +54,44 @@ namespace mongo::optimizer {
  */
 class ExpressionSyntaxSort {};
 
+class RefCountedValue {
+public:
+    RefCountedValue() = delete;
+
+    RefCountedValue(sbe::value::Value val) : _val(val) {
+        tassert(0, "Ref count must be 0 on construction", _refCount == 0);
+        ++_refCount;
+    }
+
+    ~RefCountedValue() {
+        tassert(0, "Contained value should have been freed by dec()", _val == 0 && _refCount == 0);
+    }
+
+    sbe::value::Value val() const {
+        tassert(0, "Ref count must be > 0", _refCount > 0);
+        tassert(0, "Value must be > 0", _val > 0);
+        return _val;
+    }
+
+    void inc() {
+        ++_refCount;
+    }
+
+    void dec(sbe::value::TypeTags tag) {
+        --_refCount;
+        if (_refCount == 0) {
+            tassert(0, "_val should be > 0", _val > 0);
+            sbe::value::releaseValueDeep(tag, _val);
+            _val = 0;
+            delete this;
+        }
+    }
+
+private:
+    sbe::value::Value _val = 0;
+    size_t _refCount = 0;
+};
+
 /**
  * Holds a constant SBE value with corresponding type tag.
  */
@@ -103,10 +141,18 @@ public:
 
     bool operator==(const Constant& other) const;
 
+    sbe::value::Value val() const {
+        if (isShallowType(_tag)) {
+            return _val;
+        }
+        RefCountedValue* valRefPtr = reinterpret_cast<RefCountedValue*>(_val);
+        return valRefPtr->val();
+    }
     auto get() const {
-        return std::pair{_tag, _val};
+        return std::pair{_tag, val()};
     }
 
+
     bool isString() const;
     StringData getString() const;
 
@@ -147,6 +193,8 @@ public:
 
 private:
     sbe::value::TypeTags _tag;
+    // If this is a shallow type Value, hold the value itself, otherwise holds a pointer to a
+    // reference counted Value which is stored inside a value pool.
     sbe::value::Value _val;
 };
 
