limit.sql 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. --
  2. -- LIMIT
  3. -- Check the LIMIT/OFFSET feature of SELECT
  4. --
  5. SELECT ''::text AS two, unique1, unique2, stringu1
  6. FROM onek WHERE unique1 > 50
  7. ORDER BY unique1 LIMIT 2;
  8. SELECT ''::text AS five, unique1, unique2, stringu1
  9. FROM onek WHERE unique1 > 60
  10. ORDER BY unique1 LIMIT 5;
  11. SELECT ''::text AS two, unique1, unique2, stringu1
  12. FROM onek WHERE unique1 > 60 AND unique1 < 63
  13. ORDER BY unique1 LIMIT 5;
  14. SELECT ''::text AS three, unique1, unique2, stringu1
  15. FROM onek WHERE unique1 > 100
  16. ORDER BY unique1 LIMIT 3 OFFSET 20;
  17. SELECT ''::text AS zero, unique1, unique2, stringu1
  18. FROM onek WHERE unique1 < 50
  19. ORDER BY unique1 DESC LIMIT 8 OFFSET 99;
  20. SELECT ''::text AS eleven, unique1, unique2, stringu1
  21. FROM onek WHERE unique1 < 50
  22. ORDER BY unique1 DESC LIMIT 20 OFFSET 39;
  23. SELECT ''::text AS ten, unique1, unique2, stringu1
  24. FROM onek
  25. ORDER BY unique1 OFFSET 990;
  26. SELECT ''::text AS five, unique1, unique2, stringu1
  27. FROM onek
  28. ORDER BY unique1 OFFSET 990 LIMIT 5;
  29. SELECT ''::text AS five, unique1, unique2, stringu1
  30. FROM onek
  31. ORDER BY unique1 LIMIT 5 OFFSET 900;
  32. -- Test null limit and offset. The planner would discard a simple null
  33. -- constant, so to ensure executor is exercised, do this:
  34. select * from int8_tbl limit (case when random() < 0.5 then null::bigint end);
  35. select * from int8_tbl offset (case when random() < 0.5 then null::bigint end);
  36. -- Test assorted cases involving backwards fetch from a LIMIT plan node
  37. begin;
  38. rollback;
  39. select sum(tenthous) as s1, sum(tenthous) + random()*0 as s2
  40. from tenk1 group by thousand order by thousand limit 3;
  41. SELECT thousand
  42. FROM onek WHERE thousand < 5
  43. ORDER BY thousand FETCH FIRST 2 ROW ONLY;
  44. -- SKIP LOCKED and WITH TIES are incompatible
  45. SELECT thousand
  46. FROM onek WHERE thousand < 5
  47. ORDER BY thousand FETCH FIRST 1 ROW WITH TIES FOR UPDATE SKIP LOCKED;
  48. -- should fail
  49. SELECT ''::text AS two, unique1, unique2, stringu1
  50. FROM onek WHERE unique1 > 50
  51. FETCH FIRST 2 ROW WITH TIES;
  52. -- leave these views