Skip to content

Commit

Permalink
fix: unnecessary count() on RPC returning single
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-chavez committed Oct 19, 2023
1 parent 00f3cb3 commit 54786a6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #2771, Add `Server-Timing` header with JWT duration - @taimoorzaeem
- #2698, Add config `jwt-cache-max-lifetime` and implement JWT caching - @taimoorzaeem
- #2943, Add `handling=strict/lenient` for Prefer header - @taimoorzaeem
- #2983, Add more data to `Server-Timing` header - @develop7

### Fixed

- #2824, Fix range request with 0 rows and 0 offset return status 416 - @strengthless
- #3015, Fix unnecessary count() on RPC returning single - @steve-chavez

## [11.2.1] - 2023-10-03
- #2983, Add more data to `Server-Timing` header - @develop7

### Fixed

Expand Down
6 changes: 4 additions & 2 deletions src/PostgREST/Query/Statements.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import PostgREST.MediaType (MTPlanFormat (..),
MediaType (..))
import PostgREST.Query.SqlFragment
import PostgREST.SchemaCache.Routine (ResultAggregate (..),
Routine)
Routine, funcReturnsSingle)

import Protolude

Expand Down Expand Up @@ -121,7 +121,9 @@ prepareCall rout callProcQuery selectQuery countQuery countTotal mt rAgg =
countCTEF <>
"SELECT " <>
countResultF <> " AS total_result_set, " <>
"pg_catalog.count(_postgrest_t) AS page_total, " <>
(if funcReturnsSingle rout
then "1"
else "pg_catalog.count(_postgrest_t)") <> " AS page_total, " <>
aggF (Just rout) rAgg <> " AS body, " <>
responseHeadersF <> " AS response_headers, " <>
responseStatusF <> " AS response_status " <>
Expand Down
6 changes: 6 additions & 0 deletions src/PostgREST/SchemaCache/Routine.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module PostgREST.SchemaCache.Routine
, funcReturnsVoid
, funcTableName
, funcReturnsCompositeAlias
, funcReturnsSingle
, ResultAggregate(..)
) where

Expand Down Expand Up @@ -99,6 +100,11 @@ data ResultAggregate
| NoAgg
deriving (Eq, Show)

funcReturnsSingle :: Routine -> Bool
funcReturnsSingle proc = case proc of
Function{pdReturnType = Single _} -> True
_ -> False

funcReturnsScalar :: Routine -> Bool
funcReturnsScalar proc = case proc of
Function{pdReturnType = Single (Scalar{})} -> True
Expand Down
2 changes: 1 addition & 1 deletion test/spec/Feature/Query/PlanSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ spec actualPgVersion = do
r <- request methodGet "/rpc/add_them?a=3&b=4"
[planHdr] ""

liftIO $ planCost r `shouldSatisfy` (< 1.18)
liftIO $ planCost r `shouldSatisfy` (< 0.11)

context "function inlining" $ do
it "should inline a zero argument function(the function won't appear in the plan tree)" $ do
Expand Down

0 comments on commit 54786a6

Please sign in to comment.