Skip to content

Commit e332f03

Browse files
committed
fix: HEAD unnecessarily executing aggregates
1 parent 905fcb0 commit e332f03

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2828
- #2821, Fix OPTIONS not accepting all available media types - @steve-chavez
2929
- #2834, Fix compilation on Ubuntu by being compatible with GHC 9.0.2 - @steve-chavez
3030
- #2840, Fix `Prefer: missing=default` with DOMAIN default values - @steve-chavez
31+
- #2849, Fix HEAD unnecessarily executing aggregates - @steve-chavez
3132

3233
## [11.1.0] - 2023-06-07
3334

src/PostgREST/MediaType.hs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ module PostgREST.MediaType
77
, toContentType
88
, toMime
99
, decodeMediaType
10-
, getMediaType
1110
) where
1211

1312
import qualified Data.ByteString as BS
@@ -143,8 +142,3 @@ decodeMediaType mt =
143142
[PlanSettings | inOpts "settings"] ++
144143
[PlanBuffers | inOpts "buffers" ] ++
145144
[PlanWAL | inOpts "wal" ]
146-
147-
getMediaType :: MediaType -> MediaType
148-
getMediaType mt = case mt of
149-
MTPlan mType _ _ -> mType
150-
other -> other

src/PostgREST/Plan.hs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ wrappedReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest
112112
wrappedReadPlan identifier conf sCache apiRequest = do
113113
rPlan <- readPlan identifier conf sCache apiRequest
114114
binField <- mapLeft ApiRequestError $ binaryField conf (iAcceptMediaType apiRequest) Nothing rPlan
115-
return $ WrappedReadPlan rPlan SQL.Read $ mediaToAggregate (iAcceptMediaType apiRequest) binField Nothing
115+
return $ WrappedReadPlan rPlan SQL.Read $ mediaToAggregate (iAcceptMediaType apiRequest) binField apiRequest
116116

117117
mutateReadPlan :: Mutation -> ApiRequest -> QualifiedIdentifier -> AppConfig -> SchemaCache -> Either Error MutateReadPlan
118-
mutateReadPlan mutation apiRequest@ApiRequest{iPreferences=Preferences{preferRepresentation}} identifier conf sCache = do
118+
mutateReadPlan mutation apiRequest identifier conf sCache = do
119119
rPlan <- readPlan identifier conf sCache apiRequest
120120
binField <- mapLeft ApiRequestError $ binaryField conf (iAcceptMediaType apiRequest) Nothing rPlan
121121
mPlan <- mutatePlan mutation identifier apiRequest sCache rPlan
122-
return $ MutateReadPlan rPlan mPlan SQL.Write $ mediaToAggregate (iAcceptMediaType apiRequest) binField (Just preferRepresentation)
122+
return $ MutateReadPlan rPlan mPlan SQL.Write $ mediaToAggregate (iAcceptMediaType apiRequest) binField apiRequest
123123

124124
callReadPlan :: QualifiedIdentifier -> AppConfig -> SchemaCache -> ApiRequest -> InvokeMethod -> Either Error CallReadPlan
125125
callReadPlan identifier conf sCache apiRequest invMethod = do
@@ -144,7 +144,7 @@ callReadPlan identifier conf sCache apiRequest invMethod = do
144144
(InvPost, Routine.Volatile) -> SQL.Write
145145
cPlan = callPlan proc apiRequest paramKeys args rPlan
146146
binField <- mapLeft ApiRequestError $ binaryField conf (iAcceptMediaType apiRequest) (Just proc) rPlan
147-
return $ CallReadPlan rPlan cPlan txMode proc $ mediaToAggregate (iAcceptMediaType apiRequest) binField Nothing
147+
return $ CallReadPlan rPlan cPlan txMode proc $ mediaToAggregate (iAcceptMediaType apiRequest) binField apiRequest
148148
where
149149
Preferences{..} = iPreferences apiRequest
150150
qsParams' = QueryParams.qsParams (iQueryParams apiRequest)
@@ -839,10 +839,10 @@ binaryField AppConfig{configRawMediaTypes} acceptMediaType proc rpTree
839839
fstFieldName (Node ReadPlan{select=[(CoercibleField{cfName=fld, cfJsonPath=[]}, _, _)]} []) = Just fld
840840
fstFieldName _ = Nothing
841841

842-
mediaToAggregate :: MediaType -> Maybe FieldName -> Maybe PreferRepresentation -> ResultAggregate
843-
mediaToAggregate mt binField rep =
844-
if rep == Just HeadersOnly || rep == Just None
845-
then NoAgg
842+
843+
mediaToAggregate :: MediaType -> Maybe FieldName -> ApiRequest -> ResultAggregate
844+
mediaToAggregate mt binField apiReq@ApiRequest{iAction=act, iPreferences=Preferences{preferRepresentation=rep}} =
845+
if noAgg then NoAgg
846846
else case mt of
847847
MTApplicationJSON -> BuiltinAggJson
848848
MTSingularJSON -> BuiltinAggSingleJson
@@ -861,4 +861,10 @@ mediaToAggregate mt binField rep =
861861
-- Doing `Accept: application/vnd.pgrst.plan; for="application/vnd.pgrst.plan"` doesn't make sense, so we just empty the body.
862862
-- TODO: fail instead to be more strict
863863
MTPlan (MTPlan{}) _ _ -> NoAgg
864-
MTPlan media _ _ -> mediaToAggregate media binField rep
864+
MTPlan media _ _ -> mediaToAggregate media binField apiReq
865+
where
866+
noAgg = case act of
867+
ActionMutate _ -> rep == HeadersOnly || rep == None
868+
ActionRead _isHead -> _isHead -- no need for an aggregate on HEAD https://github.com/PostgREST/postgrest/issues/2849
869+
ActionInvoke invMethod -> invMethod == InvHead
870+
_ -> False

0 commit comments

Comments
 (0)