pg_query 1.1.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (478) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +163 -52
  3. data/README.md +80 -69
  4. data/Rakefile +82 -1
  5. data/ext/pg_query/extconf.rb +3 -31
  6. data/ext/pg_query/guc-file.c +0 -0
  7. data/ext/pg_query/include/access/amapi.h +246 -0
  8. data/ext/pg_query/include/access/attmap.h +52 -0
  9. data/ext/pg_query/include/access/attnum.h +64 -0
  10. data/ext/pg_query/include/access/clog.h +61 -0
  11. data/ext/pg_query/include/access/commit_ts.h +77 -0
  12. data/ext/pg_query/include/access/detoast.h +92 -0
  13. data/ext/pg_query/include/access/genam.h +228 -0
  14. data/ext/pg_query/include/access/gin.h +78 -0
  15. data/ext/pg_query/include/access/htup.h +89 -0
  16. data/ext/pg_query/include/access/htup_details.h +819 -0
  17. data/ext/pg_query/include/access/itup.h +161 -0
  18. data/ext/pg_query/include/access/parallel.h +82 -0
  19. data/ext/pg_query/include/access/printtup.h +35 -0
  20. data/ext/pg_query/include/access/relation.h +28 -0
  21. data/ext/pg_query/include/access/relscan.h +176 -0
  22. data/ext/pg_query/include/access/rmgr.h +35 -0
  23. data/ext/pg_query/include/access/rmgrlist.h +49 -0
  24. data/ext/pg_query/include/access/sdir.h +58 -0
  25. data/ext/pg_query/include/access/skey.h +151 -0
  26. data/ext/pg_query/include/access/stratnum.h +83 -0
  27. data/ext/pg_query/include/access/sysattr.h +29 -0
  28. data/ext/pg_query/include/access/table.h +27 -0
  29. data/ext/pg_query/include/access/tableam.h +1825 -0
  30. data/ext/pg_query/include/access/transam.h +265 -0
  31. data/ext/pg_query/include/access/tupconvert.h +51 -0
  32. data/ext/pg_query/include/access/tupdesc.h +154 -0
  33. data/ext/pg_query/include/access/tupmacs.h +247 -0
  34. data/ext/pg_query/include/access/twophase.h +61 -0
  35. data/ext/pg_query/include/access/xact.h +463 -0
  36. data/ext/pg_query/include/access/xlog.h +398 -0
  37. data/ext/pg_query/include/access/xlog_internal.h +330 -0
  38. data/ext/pg_query/include/access/xlogdefs.h +109 -0
  39. data/ext/pg_query/include/access/xloginsert.h +64 -0
  40. data/ext/pg_query/include/access/xlogreader.h +327 -0
  41. data/ext/pg_query/include/access/xlogrecord.h +227 -0
  42. data/ext/pg_query/include/bootstrap/bootstrap.h +62 -0
  43. data/ext/pg_query/include/c.h +1322 -0
  44. data/ext/pg_query/include/catalog/catalog.h +42 -0
  45. data/ext/pg_query/include/catalog/catversion.h +58 -0
  46. data/ext/pg_query/include/catalog/dependency.h +275 -0
  47. data/ext/pg_query/include/catalog/genbki.h +64 -0
  48. data/ext/pg_query/include/catalog/index.h +199 -0
  49. data/ext/pg_query/include/catalog/indexing.h +366 -0
  50. data/ext/pg_query/include/catalog/namespace.h +188 -0
  51. data/ext/pg_query/include/catalog/objectaccess.h +197 -0
  52. data/ext/pg_query/include/catalog/objectaddress.h +84 -0
  53. data/ext/pg_query/include/catalog/pg_aggregate.h +176 -0
  54. data/ext/pg_query/include/catalog/pg_aggregate_d.h +77 -0
  55. data/ext/pg_query/include/catalog/pg_am.h +60 -0
  56. data/ext/pg_query/include/catalog/pg_am_d.h +45 -0
  57. data/ext/pg_query/include/catalog/pg_attribute.h +204 -0
  58. data/ext/pg_query/include/catalog/pg_attribute_d.h +59 -0
  59. data/ext/pg_query/include/catalog/pg_authid.h +58 -0
  60. data/ext/pg_query/include/catalog/pg_authid_d.h +49 -0
  61. data/ext/pg_query/include/catalog/pg_class.h +200 -0
  62. data/ext/pg_query/include/catalog/pg_class_d.h +103 -0
  63. data/ext/pg_query/include/catalog/pg_collation.h +73 -0
  64. data/ext/pg_query/include/catalog/pg_collation_d.h +45 -0
  65. data/ext/pg_query/include/catalog/pg_constraint.h +247 -0
  66. data/ext/pg_query/include/catalog/pg_constraint_d.h +67 -0
  67. data/ext/pg_query/include/catalog/pg_control.h +250 -0
  68. data/ext/pg_query/include/catalog/pg_conversion.h +72 -0
  69. data/ext/pg_query/include/catalog/pg_conversion_d.h +35 -0
  70. data/ext/pg_query/include/catalog/pg_depend.h +73 -0
  71. data/ext/pg_query/include/catalog/pg_depend_d.h +34 -0
  72. data/ext/pg_query/include/catalog/pg_event_trigger.h +51 -0
  73. data/ext/pg_query/include/catalog/pg_event_trigger_d.h +34 -0
  74. data/ext/pg_query/include/catalog/pg_index.h +80 -0
  75. data/ext/pg_query/include/catalog/pg_index_d.h +56 -0
  76. data/ext/pg_query/include/catalog/pg_language.h +67 -0
  77. data/ext/pg_query/include/catalog/pg_language_d.h +39 -0
  78. data/ext/pg_query/include/catalog/pg_namespace.h +59 -0
  79. data/ext/pg_query/include/catalog/pg_namespace_d.h +34 -0
  80. data/ext/pg_query/include/catalog/pg_opclass.h +85 -0
  81. data/ext/pg_query/include/catalog/pg_opclass_d.h +49 -0
  82. data/ext/pg_query/include/catalog/pg_operator.h +102 -0
  83. data/ext/pg_query/include/catalog/pg_operator_d.h +106 -0
  84. data/ext/pg_query/include/catalog/pg_opfamily.h +60 -0
  85. data/ext/pg_query/include/catalog/pg_opfamily_d.h +47 -0
  86. data/ext/pg_query/include/catalog/pg_partitioned_table.h +63 -0
  87. data/ext/pg_query/include/catalog/pg_partitioned_table_d.h +35 -0
  88. data/ext/pg_query/include/catalog/pg_proc.h +211 -0
  89. data/ext/pg_query/include/catalog/pg_proc_d.h +99 -0
  90. data/ext/pg_query/include/catalog/pg_publication.h +115 -0
  91. data/ext/pg_query/include/catalog/pg_publication_d.h +36 -0
  92. data/ext/pg_query/include/catalog/pg_replication_origin.h +57 -0
  93. data/ext/pg_query/include/catalog/pg_replication_origin_d.h +29 -0
  94. data/ext/pg_query/include/catalog/pg_statistic.h +275 -0
  95. data/ext/pg_query/include/catalog/pg_statistic_d.h +194 -0
  96. data/ext/pg_query/include/catalog/pg_statistic_ext.h +74 -0
  97. data/ext/pg_query/include/catalog/pg_statistic_ext_d.h +40 -0
  98. data/ext/pg_query/include/catalog/pg_transform.h +45 -0
  99. data/ext/pg_query/include/catalog/pg_transform_d.h +32 -0
  100. data/ext/pg_query/include/catalog/pg_trigger.h +137 -0
  101. data/ext/pg_query/include/catalog/pg_trigger_d.h +106 -0
  102. data/ext/pg_query/include/catalog/pg_ts_config.h +50 -0
  103. data/ext/pg_query/include/catalog/pg_ts_config_d.h +32 -0
  104. data/ext/pg_query/include/catalog/pg_ts_dict.h +54 -0
  105. data/ext/pg_query/include/catalog/pg_ts_dict_d.h +33 -0
  106. data/ext/pg_query/include/catalog/pg_ts_parser.h +57 -0
  107. data/ext/pg_query/include/catalog/pg_ts_parser_d.h +35 -0
  108. data/ext/pg_query/include/catalog/pg_ts_template.h +48 -0
  109. data/ext/pg_query/include/catalog/pg_ts_template_d.h +32 -0
  110. data/ext/pg_query/include/catalog/pg_type.h +372 -0
  111. data/ext/pg_query/include/catalog/pg_type_d.h +285 -0
  112. data/ext/pg_query/include/catalog/storage.h +48 -0
  113. data/ext/pg_query/include/commands/async.h +54 -0
  114. data/ext/pg_query/include/commands/dbcommands.h +35 -0
  115. data/ext/pg_query/include/commands/defrem.h +173 -0
  116. data/ext/pg_query/include/commands/event_trigger.h +88 -0
  117. data/ext/pg_query/include/commands/explain.h +127 -0
  118. data/ext/pg_query/include/commands/prepare.h +61 -0
  119. data/ext/pg_query/include/commands/tablespace.h +67 -0
  120. data/ext/pg_query/include/commands/trigger.h +277 -0
  121. data/ext/pg_query/include/commands/user.h +37 -0
  122. data/ext/pg_query/include/commands/vacuum.h +293 -0
  123. data/ext/pg_query/include/commands/variable.h +38 -0
  124. data/ext/pg_query/include/common/file_perm.h +56 -0
  125. data/ext/pg_query/include/common/hashfn.h +104 -0
  126. data/ext/pg_query/include/common/ip.h +37 -0
  127. data/ext/pg_query/include/common/keywords.h +33 -0
  128. data/ext/pg_query/include/common/kwlookup.h +44 -0
  129. data/ext/pg_query/include/common/relpath.h +90 -0
  130. data/ext/pg_query/include/common/string.h +19 -0
  131. data/ext/pg_query/include/common/unicode_combining_table.h +196 -0
  132. data/ext/pg_query/include/datatype/timestamp.h +197 -0
  133. data/ext/pg_query/include/executor/execdesc.h +70 -0
  134. data/ext/pg_query/include/executor/executor.h +614 -0
  135. data/ext/pg_query/include/executor/functions.h +41 -0
  136. data/ext/pg_query/include/executor/instrument.h +101 -0
  137. data/ext/pg_query/include/executor/spi.h +175 -0
  138. data/ext/pg_query/include/executor/tablefunc.h +67 -0
  139. data/ext/pg_query/include/executor/tuptable.h +487 -0
  140. data/ext/pg_query/include/fmgr.h +775 -0
  141. data/ext/pg_query/include/funcapi.h +348 -0
  142. data/ext/pg_query/include/getaddrinfo.h +162 -0
  143. data/ext/pg_query/include/jit/jit.h +105 -0
  144. data/ext/pg_query/include/kwlist_d.h +1072 -0
  145. data/ext/pg_query/include/lib/ilist.h +727 -0
  146. data/ext/pg_query/include/lib/pairingheap.h +102 -0
  147. data/ext/pg_query/include/lib/simplehash.h +1059 -0
  148. data/ext/pg_query/include/lib/stringinfo.h +161 -0
  149. data/ext/pg_query/include/libpq/auth.h +29 -0
  150. data/ext/pg_query/include/libpq/crypt.h +46 -0
  151. data/ext/pg_query/include/libpq/hba.h +140 -0
  152. data/ext/pg_query/include/libpq/libpq-be.h +326 -0
  153. data/ext/pg_query/include/libpq/libpq.h +133 -0
  154. data/ext/pg_query/include/libpq/pqcomm.h +208 -0
  155. data/ext/pg_query/include/libpq/pqformat.h +210 -0
  156. data/ext/pg_query/include/libpq/pqsignal.h +42 -0
  157. data/ext/pg_query/include/mb/pg_wchar.h +672 -0
  158. data/ext/pg_query/include/mb/stringinfo_mb.h +24 -0
  159. data/ext/pg_query/include/miscadmin.h +476 -0
  160. data/ext/pg_query/include/nodes/bitmapset.h +122 -0
  161. data/ext/pg_query/include/nodes/execnodes.h +2520 -0
  162. data/ext/pg_query/include/nodes/extensible.h +160 -0
  163. data/ext/pg_query/include/nodes/lockoptions.h +61 -0
  164. data/ext/pg_query/include/nodes/makefuncs.h +108 -0
  165. data/ext/pg_query/include/nodes/memnodes.h +108 -0
  166. data/ext/pg_query/include/nodes/nodeFuncs.h +162 -0
  167. data/ext/pg_query/include/nodes/nodes.h +842 -0
  168. data/ext/pg_query/include/nodes/params.h +170 -0
  169. data/ext/pg_query/include/nodes/parsenodes.h +3579 -0
  170. data/ext/pg_query/include/nodes/pathnodes.h +2556 -0
  171. data/ext/pg_query/include/nodes/pg_list.h +605 -0
  172. data/ext/pg_query/include/nodes/plannodes.h +1251 -0
  173. data/ext/pg_query/include/nodes/primnodes.h +1541 -0
  174. data/ext/pg_query/include/nodes/print.h +34 -0
  175. data/ext/pg_query/include/nodes/tidbitmap.h +75 -0
  176. data/ext/pg_query/include/nodes/value.h +61 -0
  177. data/ext/pg_query/include/optimizer/cost.h +206 -0
  178. data/ext/pg_query/include/optimizer/geqo.h +88 -0
  179. data/ext/pg_query/include/optimizer/geqo_gene.h +45 -0
  180. data/ext/pg_query/include/optimizer/optimizer.h +199 -0
  181. data/ext/pg_query/include/optimizer/paths.h +249 -0
  182. data/ext/pg_query/include/optimizer/planmain.h +119 -0
  183. data/ext/pg_query/include/parser/analyze.h +49 -0
  184. data/ext/pg_query/include/parser/gram.h +1067 -0
  185. data/ext/pg_query/include/parser/gramparse.h +75 -0
  186. data/ext/pg_query/include/parser/kwlist.h +477 -0
  187. data/ext/pg_query/include/parser/parse_agg.h +68 -0
  188. data/ext/pg_query/include/parser/parse_clause.h +54 -0
  189. data/ext/pg_query/include/parser/parse_coerce.h +97 -0
  190. data/ext/pg_query/include/parser/parse_collate.h +27 -0
  191. data/ext/pg_query/include/parser/parse_expr.h +26 -0
  192. data/ext/pg_query/include/parser/parse_func.h +73 -0
  193. data/ext/pg_query/include/parser/parse_node.h +327 -0
  194. data/ext/pg_query/include/parser/parse_oper.h +67 -0
  195. data/ext/pg_query/include/parser/parse_relation.h +123 -0
  196. data/ext/pg_query/include/parser/parse_target.h +46 -0
  197. data/ext/pg_query/include/parser/parse_type.h +60 -0
  198. data/ext/pg_query/include/parser/parser.h +41 -0
  199. data/ext/pg_query/include/parser/parsetree.h +61 -0
  200. data/ext/pg_query/include/parser/scanner.h +152 -0
  201. data/ext/pg_query/include/parser/scansup.h +30 -0
  202. data/ext/pg_query/include/partitioning/partdefs.h +26 -0
  203. data/ext/pg_query/include/pg_config.h +988 -0
  204. data/ext/pg_query/include/pg_config_ext.h +8 -0
  205. data/ext/pg_query/include/pg_config_manual.h +350 -0
  206. data/ext/pg_query/include/pg_config_os.h +8 -0
  207. data/ext/pg_query/include/pg_getopt.h +56 -0
  208. data/ext/pg_query/include/pg_query.h +121 -0
  209. data/ext/pg_query/include/pg_query_enum_defs.c +2454 -0
  210. data/ext/pg_query/include/pg_query_fingerprint_conds.c +875 -0
  211. data/ext/pg_query/include/pg_query_fingerprint_defs.c +12413 -0
  212. data/ext/pg_query/include/pg_query_json_helper.c +61 -0
  213. data/ext/pg_query/include/pg_query_outfuncs_conds.c +686 -0
  214. data/ext/pg_query/include/pg_query_outfuncs_defs.c +2437 -0
  215. data/ext/pg_query/include/pg_query_readfuncs_conds.c +222 -0
  216. data/ext/pg_query/include/pg_query_readfuncs_defs.c +2878 -0
  217. data/ext/pg_query/include/pg_trace.h +17 -0
  218. data/ext/pg_query/include/pgstat.h +1487 -0
  219. data/ext/pg_query/include/pgtime.h +84 -0
  220. data/ext/pg_query/include/pl_gram.h +385 -0
  221. data/ext/pg_query/include/pl_reserved_kwlist.h +52 -0
  222. data/ext/pg_query/include/pl_reserved_kwlist_d.h +114 -0
  223. data/ext/pg_query/include/pl_unreserved_kwlist.h +112 -0
  224. data/ext/pg_query/include/pl_unreserved_kwlist_d.h +246 -0
  225. data/ext/pg_query/include/plerrcodes.h +990 -0
  226. data/ext/pg_query/include/plpgsql.h +1347 -0
  227. data/ext/pg_query/include/port.h +524 -0
  228. data/ext/pg_query/include/port/atomics.h +524 -0
  229. data/ext/pg_query/include/port/atomics/arch-arm.h +26 -0
  230. data/ext/pg_query/include/port/atomics/arch-ppc.h +254 -0
  231. data/ext/pg_query/include/port/atomics/arch-x86.h +252 -0
  232. data/ext/pg_query/include/port/atomics/fallback.h +170 -0
  233. data/ext/pg_query/include/port/atomics/generic-gcc.h +286 -0
  234. data/ext/pg_query/include/port/atomics/generic.h +401 -0
  235. data/ext/pg_query/include/port/pg_bitutils.h +226 -0
  236. data/ext/pg_query/include/port/pg_bswap.h +161 -0
  237. data/ext/pg_query/include/port/pg_crc32c.h +101 -0
  238. data/ext/pg_query/include/portability/instr_time.h +256 -0
  239. data/ext/pg_query/include/postgres.h +764 -0
  240. data/ext/pg_query/include/postgres_ext.h +74 -0
  241. data/ext/pg_query/include/postmaster/autovacuum.h +83 -0
  242. data/ext/pg_query/include/postmaster/bgworker.h +161 -0
  243. data/ext/pg_query/include/postmaster/bgworker_internals.h +64 -0
  244. data/ext/pg_query/include/postmaster/bgwriter.h +45 -0
  245. data/ext/pg_query/include/postmaster/fork_process.h +17 -0
  246. data/ext/pg_query/include/postmaster/interrupt.h +32 -0
  247. data/ext/pg_query/include/postmaster/pgarch.h +39 -0
  248. data/ext/pg_query/include/postmaster/postmaster.h +77 -0
  249. data/ext/pg_query/include/postmaster/syslogger.h +98 -0
  250. data/ext/pg_query/include/postmaster/walwriter.h +21 -0
  251. data/ext/pg_query/include/protobuf-c.h +1106 -0
  252. data/ext/pg_query/include/protobuf-c/protobuf-c.h +1106 -0
  253. data/ext/pg_query/include/protobuf/pg_query.pb-c.h +10846 -0
  254. data/ext/pg_query/include/protobuf/pg_query.pb.h +124718 -0
  255. data/ext/pg_query/include/regex/regex.h +184 -0
  256. data/ext/pg_query/include/replication/logicallauncher.h +31 -0
  257. data/ext/pg_query/include/replication/logicalproto.h +110 -0
  258. data/ext/pg_query/include/replication/logicalworker.h +19 -0
  259. data/ext/pg_query/include/replication/origin.h +73 -0
  260. data/ext/pg_query/include/replication/reorderbuffer.h +467 -0
  261. data/ext/pg_query/include/replication/slot.h +219 -0
  262. data/ext/pg_query/include/replication/syncrep.h +115 -0
  263. data/ext/pg_query/include/replication/walreceiver.h +340 -0
  264. data/ext/pg_query/include/replication/walsender.h +74 -0
  265. data/ext/pg_query/include/rewrite/prs2lock.h +46 -0
  266. data/ext/pg_query/include/rewrite/rewriteHandler.h +40 -0
  267. data/ext/pg_query/include/rewrite/rewriteManip.h +87 -0
  268. data/ext/pg_query/include/rewrite/rewriteSupport.h +26 -0
  269. data/ext/pg_query/include/storage/backendid.h +37 -0
  270. data/ext/pg_query/include/storage/block.h +121 -0
  271. data/ext/pg_query/include/storage/buf.h +46 -0
  272. data/ext/pg_query/include/storage/bufmgr.h +292 -0
  273. data/ext/pg_query/include/storage/bufpage.h +459 -0
  274. data/ext/pg_query/include/storage/condition_variable.h +62 -0
  275. data/ext/pg_query/include/storage/dsm.h +61 -0
  276. data/ext/pg_query/include/storage/dsm_impl.h +75 -0
  277. data/ext/pg_query/include/storage/fd.h +168 -0
  278. data/ext/pg_query/include/storage/ipc.h +81 -0
  279. data/ext/pg_query/include/storage/item.h +19 -0
  280. data/ext/pg_query/include/storage/itemid.h +184 -0
  281. data/ext/pg_query/include/storage/itemptr.h +206 -0
  282. data/ext/pg_query/include/storage/large_object.h +100 -0
  283. data/ext/pg_query/include/storage/latch.h +190 -0
  284. data/ext/pg_query/include/storage/lmgr.h +114 -0
  285. data/ext/pg_query/include/storage/lock.h +612 -0
  286. data/ext/pg_query/include/storage/lockdefs.h +59 -0
  287. data/ext/pg_query/include/storage/lwlock.h +232 -0
  288. data/ext/pg_query/include/storage/lwlocknames.h +51 -0
  289. data/ext/pg_query/include/storage/off.h +57 -0
  290. data/ext/pg_query/include/storage/pg_sema.h +61 -0
  291. data/ext/pg_query/include/storage/pg_shmem.h +90 -0
  292. data/ext/pg_query/include/storage/pmsignal.h +94 -0
  293. data/ext/pg_query/include/storage/predicate.h +87 -0
  294. data/ext/pg_query/include/storage/proc.h +333 -0
  295. data/ext/pg_query/include/storage/proclist_types.h +51 -0
  296. data/ext/pg_query/include/storage/procsignal.h +75 -0
  297. data/ext/pg_query/include/storage/relfilenode.h +99 -0
  298. data/ext/pg_query/include/storage/s_lock.h +1047 -0
  299. data/ext/pg_query/include/storage/sharedfileset.h +45 -0
  300. data/ext/pg_query/include/storage/shm_mq.h +85 -0
  301. data/ext/pg_query/include/storage/shm_toc.h +58 -0
  302. data/ext/pg_query/include/storage/shmem.h +81 -0
  303. data/ext/pg_query/include/storage/sinval.h +153 -0
  304. data/ext/pg_query/include/storage/sinvaladt.h +43 -0
  305. data/ext/pg_query/include/storage/smgr.h +109 -0
  306. data/ext/pg_query/include/storage/spin.h +77 -0
  307. data/ext/pg_query/include/storage/standby.h +91 -0
  308. data/ext/pg_query/include/storage/standbydefs.h +74 -0
  309. data/ext/pg_query/include/storage/sync.h +62 -0
  310. data/ext/pg_query/include/tcop/cmdtag.h +58 -0
  311. data/ext/pg_query/include/tcop/cmdtaglist.h +217 -0
  312. data/ext/pg_query/include/tcop/deparse_utility.h +108 -0
  313. data/ext/pg_query/include/tcop/dest.h +149 -0
  314. data/ext/pg_query/include/tcop/fastpath.h +21 -0
  315. data/ext/pg_query/include/tcop/pquery.h +45 -0
  316. data/ext/pg_query/include/tcop/tcopprot.h +89 -0
  317. data/ext/pg_query/include/tcop/utility.h +108 -0
  318. data/ext/pg_query/include/tsearch/ts_cache.h +98 -0
  319. data/ext/pg_query/include/utils/acl.h +312 -0
  320. data/ext/pg_query/include/utils/aclchk_internal.h +45 -0
  321. data/ext/pg_query/include/utils/array.h +458 -0
  322. data/ext/pg_query/include/utils/builtins.h +127 -0
  323. data/ext/pg_query/include/utils/bytea.h +27 -0
  324. data/ext/pg_query/include/utils/catcache.h +231 -0
  325. data/ext/pg_query/include/utils/date.h +90 -0
  326. data/ext/pg_query/include/utils/datetime.h +343 -0
  327. data/ext/pg_query/include/utils/datum.h +68 -0
  328. data/ext/pg_query/include/utils/dsa.h +123 -0
  329. data/ext/pg_query/include/utils/dynahash.h +19 -0
  330. data/ext/pg_query/include/utils/elog.h +439 -0
  331. data/ext/pg_query/include/utils/errcodes.h +352 -0
  332. data/ext/pg_query/include/utils/expandeddatum.h +159 -0
  333. data/ext/pg_query/include/utils/expandedrecord.h +231 -0
  334. data/ext/pg_query/include/utils/float.h +356 -0
  335. data/ext/pg_query/include/utils/fmgroids.h +2657 -0
  336. data/ext/pg_query/include/utils/fmgrprotos.h +2646 -0
  337. data/ext/pg_query/include/utils/fmgrtab.h +48 -0
  338. data/ext/pg_query/include/utils/guc.h +443 -0
  339. data/ext/pg_query/include/utils/guc_tables.h +272 -0
  340. data/ext/pg_query/include/utils/hsearch.h +149 -0
  341. data/ext/pg_query/include/utils/inval.h +64 -0
  342. data/ext/pg_query/include/utils/lsyscache.h +197 -0
  343. data/ext/pg_query/include/utils/memdebug.h +82 -0
  344. data/ext/pg_query/include/utils/memutils.h +225 -0
  345. data/ext/pg_query/include/utils/numeric.h +76 -0
  346. data/ext/pg_query/include/utils/palloc.h +136 -0
  347. data/ext/pg_query/include/utils/partcache.h +102 -0
  348. data/ext/pg_query/include/utils/pg_locale.h +119 -0
  349. data/ext/pg_query/include/utils/pg_lsn.h +29 -0
  350. data/ext/pg_query/include/utils/pidfile.h +56 -0
  351. data/ext/pg_query/include/utils/plancache.h +235 -0
  352. data/ext/pg_query/include/utils/portal.h +241 -0
  353. data/ext/pg_query/include/utils/probes.h +114 -0
  354. data/ext/pg_query/include/utils/ps_status.h +25 -0
  355. data/ext/pg_query/include/utils/queryenvironment.h +74 -0
  356. data/ext/pg_query/include/utils/regproc.h +28 -0
  357. data/ext/pg_query/include/utils/rel.h +644 -0
  358. data/ext/pg_query/include/utils/relcache.h +151 -0
  359. data/ext/pg_query/include/utils/reltrigger.h +81 -0
  360. data/ext/pg_query/include/utils/resowner.h +86 -0
  361. data/ext/pg_query/include/utils/rls.h +50 -0
  362. data/ext/pg_query/include/utils/ruleutils.h +44 -0
  363. data/ext/pg_query/include/utils/sharedtuplestore.h +61 -0
  364. data/ext/pg_query/include/utils/snapmgr.h +158 -0
  365. data/ext/pg_query/include/utils/snapshot.h +206 -0
  366. data/ext/pg_query/include/utils/sortsupport.h +276 -0
  367. data/ext/pg_query/include/utils/syscache.h +219 -0
  368. data/ext/pg_query/include/utils/timeout.h +88 -0
  369. data/ext/pg_query/include/utils/timestamp.h +116 -0
  370. data/ext/pg_query/include/utils/tuplesort.h +277 -0
  371. data/ext/pg_query/include/utils/tuplestore.h +91 -0
  372. data/ext/pg_query/include/utils/typcache.h +202 -0
  373. data/ext/pg_query/include/utils/tzparser.h +39 -0
  374. data/ext/pg_query/include/utils/varlena.h +39 -0
  375. data/ext/pg_query/include/utils/xml.h +84 -0
  376. data/ext/pg_query/include/xxhash.h +5445 -0
  377. data/ext/pg_query/include/xxhash/xxhash.h +5445 -0
  378. data/ext/pg_query/pg_query.c +104 -0
  379. data/ext/pg_query/pg_query.pb-c.c +37628 -0
  380. data/ext/pg_query/pg_query_deparse.c +9953 -0
  381. data/ext/pg_query/pg_query_fingerprint.c +292 -0
  382. data/ext/pg_query/pg_query_fingerprint.h +8 -0
  383. data/ext/pg_query/pg_query_internal.h +24 -0
  384. data/ext/pg_query/pg_query_json_plpgsql.c +738 -0
  385. data/ext/pg_query/pg_query_json_plpgsql.h +9 -0
  386. data/ext/pg_query/pg_query_normalize.c +437 -0
  387. data/ext/pg_query/pg_query_outfuncs.h +10 -0
  388. data/ext/pg_query/pg_query_outfuncs_json.c +297 -0
  389. data/ext/pg_query/pg_query_outfuncs_protobuf.c +237 -0
  390. data/ext/pg_query/pg_query_parse.c +148 -0
  391. data/ext/pg_query/pg_query_parse_plpgsql.c +460 -0
  392. data/ext/pg_query/pg_query_readfuncs.h +11 -0
  393. data/ext/pg_query/pg_query_readfuncs_protobuf.c +142 -0
  394. data/ext/pg_query/pg_query_ruby.c +108 -12
  395. data/ext/pg_query/pg_query_scan.c +173 -0
  396. data/ext/pg_query/pg_query_split.c +221 -0
  397. data/ext/pg_query/protobuf-c.c +3660 -0
  398. data/ext/pg_query/src_backend_catalog_namespace.c +1051 -0
  399. data/ext/pg_query/src_backend_catalog_pg_proc.c +142 -0
  400. data/ext/pg_query/src_backend_commands_define.c +117 -0
  401. data/ext/pg_query/src_backend_libpq_pqcomm.c +651 -0
  402. data/ext/pg_query/src_backend_nodes_bitmapset.c +513 -0
  403. data/ext/pg_query/src_backend_nodes_copyfuncs.c +6013 -0
  404. data/ext/pg_query/src_backend_nodes_equalfuncs.c +4003 -0
  405. data/ext/pg_query/src_backend_nodes_extensible.c +99 -0
  406. data/ext/pg_query/src_backend_nodes_list.c +922 -0
  407. data/ext/pg_query/src_backend_nodes_makefuncs.c +417 -0
  408. data/ext/pg_query/src_backend_nodes_nodeFuncs.c +1363 -0
  409. data/ext/pg_query/src_backend_nodes_value.c +84 -0
  410. data/ext/pg_query/src_backend_parser_gram.c +47456 -0
  411. data/ext/pg_query/src_backend_parser_parse_expr.c +313 -0
  412. data/ext/pg_query/src_backend_parser_parser.c +497 -0
  413. data/ext/pg_query/src_backend_parser_scan.c +7091 -0
  414. data/ext/pg_query/src_backend_parser_scansup.c +160 -0
  415. data/ext/pg_query/src_backend_postmaster_postmaster.c +2230 -0
  416. data/ext/pg_query/src_backend_storage_ipc_ipc.c +192 -0
  417. data/ext/pg_query/src_backend_storage_lmgr_s_lock.c +370 -0
  418. data/ext/pg_query/src_backend_tcop_postgres.c +776 -0
  419. data/ext/pg_query/src_backend_utils_adt_datum.c +326 -0
  420. data/ext/pg_query/src_backend_utils_adt_expandeddatum.c +98 -0
  421. data/ext/pg_query/src_backend_utils_adt_format_type.c +136 -0
  422. data/ext/pg_query/src_backend_utils_adt_ruleutils.c +1683 -0
  423. data/ext/pg_query/src_backend_utils_error_assert.c +74 -0
  424. data/ext/pg_query/src_backend_utils_error_elog.c +1748 -0
  425. data/ext/pg_query/src_backend_utils_fmgr_fmgr.c +570 -0
  426. data/ext/pg_query/src_backend_utils_hash_dynahash.c +1086 -0
  427. data/ext/pg_query/src_backend_utils_init_globals.c +168 -0
  428. data/ext/pg_query/src_backend_utils_mb_mbutils.c +839 -0
  429. data/ext/pg_query/src_backend_utils_misc_guc.c +1831 -0
  430. data/ext/pg_query/src_backend_utils_mmgr_aset.c +1560 -0
  431. data/ext/pg_query/src_backend_utils_mmgr_mcxt.c +1006 -0
  432. data/ext/pg_query/src_common_encnames.c +158 -0
  433. data/ext/pg_query/src_common_keywords.c +39 -0
  434. data/ext/pg_query/src_common_kwlist_d.h +1081 -0
  435. data/ext/pg_query/src_common_kwlookup.c +91 -0
  436. data/ext/pg_query/src_common_psprintf.c +158 -0
  437. data/ext/pg_query/src_common_string.c +86 -0
  438. data/ext/pg_query/src_common_stringinfo.c +336 -0
  439. data/ext/pg_query/src_common_wchar.c +1651 -0
  440. data/ext/pg_query/src_pl_plpgsql_src_pl_comp.c +1133 -0
  441. data/ext/pg_query/src_pl_plpgsql_src_pl_funcs.c +877 -0
  442. data/ext/pg_query/src_pl_plpgsql_src_pl_gram.c +6533 -0
  443. data/ext/pg_query/src_pl_plpgsql_src_pl_handler.c +107 -0
  444. data/ext/pg_query/src_pl_plpgsql_src_pl_reserved_kwlist_d.h +123 -0
  445. data/ext/pg_query/src_pl_plpgsql_src_pl_scanner.c +671 -0
  446. data/ext/pg_query/src_pl_plpgsql_src_pl_unreserved_kwlist_d.h +255 -0
  447. data/ext/pg_query/src_port_erand48.c +127 -0
  448. data/ext/pg_query/src_port_pg_bitutils.c +246 -0
  449. data/ext/pg_query/src_port_pgsleep.c +69 -0
  450. data/ext/pg_query/src_port_pgstrcasecmp.c +83 -0
  451. data/ext/pg_query/src_port_qsort.c +240 -0
  452. data/ext/pg_query/src_port_random.c +31 -0
  453. data/ext/pg_query/src_port_snprintf.c +1449 -0
  454. data/ext/pg_query/src_port_strerror.c +324 -0
  455. data/ext/pg_query/src_port_strnlen.c +39 -0
  456. data/ext/pg_query/xxhash.c +43 -0
  457. data/lib/pg_query.rb +7 -4
  458. data/lib/pg_query/constants.rb +21 -0
  459. data/lib/pg_query/deparse.rb +16 -1117
  460. data/lib/pg_query/filter_columns.rb +86 -85
  461. data/lib/pg_query/fingerprint.rb +122 -87
  462. data/lib/pg_query/json_field_names.rb +1402 -0
  463. data/lib/pg_query/node.rb +31 -0
  464. data/lib/pg_query/param_refs.rb +42 -37
  465. data/lib/pg_query/parse.rb +220 -200
  466. data/lib/pg_query/parse_error.rb +1 -1
  467. data/lib/pg_query/pg_query_pb.rb +3211 -0
  468. data/lib/pg_query/scan.rb +23 -0
  469. data/lib/pg_query/treewalker.rb +24 -40
  470. data/lib/pg_query/truncate.rb +64 -43
  471. data/lib/pg_query/version.rb +2 -2
  472. metadata +473 -11
  473. data/ext/pg_query/pg_query_ruby.h +0 -10
  474. data/lib/pg_query/deep_dup.rb +0 -16
  475. data/lib/pg_query/deparse/alter_table.rb +0 -42
  476. data/lib/pg_query/deparse/interval.rb +0 -105
  477. data/lib/pg_query/legacy_parsetree.rb +0 -109
  478. data/lib/pg_query/node_types.rb +0 -284
@@ -0,0 +1,1086 @@
1
+ /*--------------------------------------------------------------------
2
+ * Symbols referenced in this file:
3
+ * - hash_search
4
+ * - hash_search_with_hash_value
5
+ * - has_seq_scans
6
+ * - num_seq_scans
7
+ * - seq_scan_tables
8
+ * - expand_table
9
+ * - dir_realloc
10
+ * - CurrentDynaHashCxt
11
+ * - seg_alloc
12
+ * - calc_bucket
13
+ * - hash_corrupted
14
+ * - DynaHashAlloc
15
+ * - get_hash_entry
16
+ * - element_alloc
17
+ *--------------------------------------------------------------------
18
+ */
19
+
20
+ /*-------------------------------------------------------------------------
21
+ *
22
+ * dynahash.c
23
+ * dynamic hash tables
24
+ *
25
+ * dynahash.c supports both local-to-a-backend hash tables and hash tables in
26
+ * shared memory. For shared hash tables, it is the caller's responsibility
27
+ * to provide appropriate access interlocking. The simplest convention is
28
+ * that a single LWLock protects the whole hash table. Searches (HASH_FIND or
29
+ * hash_seq_search) need only shared lock, but any update requires exclusive
30
+ * lock. For heavily-used shared tables, the single-lock approach creates a
31
+ * concurrency bottleneck, so we also support "partitioned" locking wherein
32
+ * there are multiple LWLocks guarding distinct subsets of the table. To use
33
+ * a hash table in partitioned mode, the HASH_PARTITION flag must be given
34
+ * to hash_create. This prevents any attempt to split buckets on-the-fly.
35
+ * Therefore, each hash bucket chain operates independently, and no fields
36
+ * of the hash header change after init except nentries and freeList.
37
+ * (A partitioned table uses multiple copies of those fields, guarded by
38
+ * spinlocks, for additional concurrency.)
39
+ * This lets any subset of the hash buckets be treated as a separately
40
+ * lockable partition. We expect callers to use the low-order bits of a
41
+ * lookup key's hash value as a partition number --- this will work because
42
+ * of the way calc_bucket() maps hash values to bucket numbers.
43
+ *
44
+ * For hash tables in shared memory, the memory allocator function should
45
+ * match malloc's semantics of returning NULL on failure. For hash tables
46
+ * in local memory, we typically use palloc() which will throw error on
47
+ * failure. The code in this file has to cope with both cases.
48
+ *
49
+ * dynahash.c provides support for these types of lookup keys:
50
+ *
51
+ * 1. Null-terminated C strings (truncated if necessary to fit in keysize),
52
+ * compared as though by strcmp(). This is the default behavior.
53
+ *
54
+ * 2. Arbitrary binary data of size keysize, compared as though by memcmp().
55
+ * (Caller must ensure there are no undefined padding bits in the keys!)
56
+ * This is selected by specifying HASH_BLOBS flag to hash_create.
57
+ *
58
+ * 3. More complex key behavior can be selected by specifying user-supplied
59
+ * hashing, comparison, and/or key-copying functions. At least a hashing
60
+ * function must be supplied; comparison defaults to memcmp() and key copying
61
+ * to memcpy() when a user-defined hashing function is selected.
62
+ *
63
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
64
+ * Portions Copyright (c) 1994, Regents of the University of California
65
+ *
66
+ *
67
+ * IDENTIFICATION
68
+ * src/backend/utils/hash/dynahash.c
69
+ *
70
+ *-------------------------------------------------------------------------
71
+ */
72
+
73
+ /*
74
+ * Original comments:
75
+ *
76
+ * Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson.
77
+ * Coded into C, with minor code improvements, and with hsearch(3) interface,
78
+ * by ejp@ausmelb.oz, Jul 26, 1988: 13:16;
79
+ * also, hcreate/hdestroy routines added to simulate hsearch(3).
80
+ *
81
+ * These routines simulate hsearch(3) and family, with the important
82
+ * difference that the hash table is dynamic - can grow indefinitely
83
+ * beyond its original size (as supplied to hcreate()).
84
+ *
85
+ * Performance appears to be comparable to that of hsearch(3).
86
+ * The 'source-code' options referred to in hsearch(3)'s 'man' page
87
+ * are not implemented; otherwise functionality is identical.
88
+ *
89
+ * Compilation controls:
90
+ * HASH_DEBUG controls some informative traces, mainly for debugging.
91
+ * HASH_STATISTICS causes HashAccesses and HashCollisions to be maintained;
92
+ * when combined with HASH_DEBUG, these are displayed by hdestroy().
93
+ *
94
+ * Problems & fixes to ejp@ausmelb.oz. WARNING: relies on pre-processor
95
+ * concatenation property, in probably unnecessary code 'optimization'.
96
+ *
97
+ * Modified margo@postgres.berkeley.edu February 1990
98
+ * added multiple table interface
99
+ * Modified by sullivan@postgres.berkeley.edu April 1990
100
+ * changed ctl structure for shared memory
101
+ */
102
+
103
+ #include "postgres.h"
104
+
105
+ #include <limits.h>
106
+
107
+ #include "access/xact.h"
108
+ #include "common/hashfn.h"
109
+ #include "port/pg_bitutils.h"
110
+ #include "storage/shmem.h"
111
+ #include "storage/spin.h"
112
+ #include "utils/dynahash.h"
113
+ #include "utils/memutils.h"
114
+
115
+
116
+ /*
117
+ * Constants
118
+ *
119
+ * A hash table has a top-level "directory", each of whose entries points
120
+ * to a "segment" of ssize bucket headers. The maximum number of hash
121
+ * buckets is thus dsize * ssize (but dsize may be expansible). Of course,
122
+ * the number of records in the table can be larger, but we don't want a
123
+ * whole lot of records per bucket or performance goes down.
124
+ *
125
+ * In a hash table allocated in shared memory, the directory cannot be
126
+ * expanded because it must stay at a fixed address. The directory size
127
+ * should be selected using hash_select_dirsize (and you'd better have
128
+ * a good idea of the maximum number of entries!). For non-shared hash
129
+ * tables, the initial directory size can be left at the default.
130
+ */
131
+ #define DEF_SEGSIZE 256
132
+ #define DEF_SEGSIZE_SHIFT 8 /* must be log2(DEF_SEGSIZE) */
133
+ #define DEF_DIRSIZE 256
134
+ #define DEF_FFACTOR 1 /* default fill factor */
135
+
136
+ /* Number of freelists to be used for a partitioned hash table. */
137
+ #define NUM_FREELISTS 32
138
+
139
+ /* A hash bucket is a linked list of HASHELEMENTs */
140
+ typedef HASHELEMENT *HASHBUCKET;
141
+
142
+ /* A hash segment is an array of bucket headers */
143
+ typedef HASHBUCKET *HASHSEGMENT;
144
+
145
+ /*
146
+ * Per-freelist data.
147
+ *
148
+ * In a partitioned hash table, each freelist is associated with a specific
149
+ * set of hashcodes, as determined by the FREELIST_IDX() macro below.
150
+ * nentries tracks the number of live hashtable entries having those hashcodes
151
+ * (NOT the number of entries in the freelist, as you might expect).
152
+ *
153
+ * The coverage of a freelist might be more or less than one partition, so it
154
+ * needs its own lock rather than relying on caller locking. Relying on that
155
+ * wouldn't work even if the coverage was the same, because of the occasional
156
+ * need to "borrow" entries from another freelist; see get_hash_entry().
157
+ *
158
+ * Using an array of FreeListData instead of separate arrays of mutexes,
159
+ * nentries and freeLists helps to reduce sharing of cache lines between
160
+ * different mutexes.
161
+ */
162
+ typedef struct
163
+ {
164
+ slock_t mutex; /* spinlock for this freelist */
165
+ long nentries; /* number of entries in associated buckets */
166
+ HASHELEMENT *freeList; /* chain of free elements */
167
+ } FreeListData;
168
+
169
+ /*
170
+ * Header structure for a hash table --- contains all changeable info
171
+ *
172
+ * In a shared-memory hash table, the HASHHDR is in shared memory, while
173
+ * each backend has a local HTAB struct. For a non-shared table, there isn't
174
+ * any functional difference between HASHHDR and HTAB, but we separate them
175
+ * anyway to share code between shared and non-shared tables.
176
+ */
177
+ struct HASHHDR
178
+ {
179
+ /*
180
+ * The freelist can become a point of contention in high-concurrency hash
181
+ * tables, so we use an array of freelists, each with its own mutex and
182
+ * nentries count, instead of just a single one. Although the freelists
183
+ * normally operate independently, we will scavenge entries from freelists
184
+ * other than a hashcode's default freelist when necessary.
185
+ *
186
+ * If the hash table is not partitioned, only freeList[0] is used and its
187
+ * spinlock is not used at all; callers' locking is assumed sufficient.
188
+ */
189
+ FreeListData freeList[NUM_FREELISTS];
190
+
191
+ /* These fields can change, but not in a partitioned table */
192
+ /* Also, dsize can't change in a shared table, even if unpartitioned */
193
+ long dsize; /* directory size */
194
+ long nsegs; /* number of allocated segments (<= dsize) */
195
+ uint32 max_bucket; /* ID of maximum bucket in use */
196
+ uint32 high_mask; /* mask to modulo into entire table */
197
+ uint32 low_mask; /* mask to modulo into lower half of table */
198
+
199
+ /* These fields are fixed at hashtable creation */
200
+ Size keysize; /* hash key length in bytes */
201
+ Size entrysize; /* total user element size in bytes */
202
+ long num_partitions; /* # partitions (must be power of 2), or 0 */
203
+ long ffactor; /* target fill factor */
204
+ long max_dsize; /* 'dsize' limit if directory is fixed size */
205
+ long ssize; /* segment size --- must be power of 2 */
206
+ int sshift; /* segment shift = log2(ssize) */
207
+ int nelem_alloc; /* number of entries to allocate at once */
208
+
209
+ #ifdef HASH_STATISTICS
210
+
211
+ /*
212
+ * Count statistics here. NB: stats code doesn't bother with mutex, so
213
+ * counts could be corrupted a bit in a partitioned table.
214
+ */
215
+ long accesses;
216
+ long collisions;
217
+ #endif
218
+ };
219
+
220
+ #define IS_PARTITIONED(hctl) ((hctl)->num_partitions != 0)
221
+
222
+ #define FREELIST_IDX(hctl, hashcode) \
223
+ (IS_PARTITIONED(hctl) ? (hashcode) % NUM_FREELISTS : 0)
224
+
225
+ /*
226
+ * Top control structure for a hashtable --- in a shared table, each backend
227
+ * has its own copy (OK since no fields change at runtime)
228
+ */
229
+ struct HTAB
230
+ {
231
+ HASHHDR *hctl; /* => shared control information */
232
+ HASHSEGMENT *dir; /* directory of segment starts */
233
+ HashValueFunc hash; /* hash function */
234
+ HashCompareFunc match; /* key comparison function */
235
+ HashCopyFunc keycopy; /* key copying function */
236
+ HashAllocFunc alloc; /* memory allocator */
237
+ MemoryContext hcxt; /* memory context if default allocator used */
238
+ char *tabname; /* table name (for error messages) */
239
+ bool isshared; /* true if table is in shared memory */
240
+ bool isfixed; /* if true, don't enlarge */
241
+
242
+ /* freezing a shared table isn't allowed, so we can keep state here */
243
+ bool frozen; /* true = no more inserts allowed */
244
+
245
+ /* We keep local copies of these fixed values to reduce contention */
246
+ Size keysize; /* hash key length in bytes */
247
+ long ssize; /* segment size --- must be power of 2 */
248
+ int sshift; /* segment shift = log2(ssize) */
249
+ };
250
+
251
+ /*
252
+ * Key (also entry) part of a HASHELEMENT
253
+ */
254
+ #define ELEMENTKEY(helem) (((char *)(helem)) + MAXALIGN(sizeof(HASHELEMENT)))
255
+
256
+ /*
257
+ * Obtain element pointer given pointer to key
258
+ */
259
+ #define ELEMENT_FROM_KEY(key) \
260
+ ((HASHELEMENT *) (((char *) (key)) - MAXALIGN(sizeof(HASHELEMENT))))
261
+
262
+ /*
263
+ * Fast MOD arithmetic, assuming that y is a power of 2 !
264
+ */
265
+ #define MOD(x,y) ((x) & ((y)-1))
266
+
267
+ #ifdef HASH_STATISTICS
268
+ static long hash_accesses,
269
+ hash_collisions,
270
+ hash_expansions;
271
+ #endif
272
+
273
+ /*
274
+ * Private function prototypes
275
+ */
276
+ static void *DynaHashAlloc(Size size);
277
+ static HASHSEGMENT seg_alloc(HTAB *hashp);
278
+ static bool element_alloc(HTAB *hashp, int nelem, int freelist_idx);
279
+ static bool dir_realloc(HTAB *hashp);
280
+ static bool expand_table(HTAB *hashp);
281
+ static HASHBUCKET get_hash_entry(HTAB *hashp, int freelist_idx);
282
+ static void hdefault(HTAB *hashp);
283
+ static int choose_nelem_alloc(Size entrysize);
284
+ static bool init_htab(HTAB *hashp, long nelem);
285
+ static void hash_corrupted(HTAB *hashp);
286
+ static long next_pow2_long(long num);
287
+ static int next_pow2_int(long num);
288
+ static void register_seq_scan(HTAB *hashp);
289
+ static void deregister_seq_scan(HTAB *hashp);
290
+ static bool has_seq_scans(HTAB *hashp);
291
+
292
+
293
+ /*
294
+ * memory allocation support
295
+ */
296
+ static __thread MemoryContext CurrentDynaHashCxt = NULL;
297
+
298
+
299
+ static void *
300
+ DynaHashAlloc(Size size)
301
+ {
302
+ Assert(MemoryContextIsValid(CurrentDynaHashCxt));
303
+ return MemoryContextAlloc(CurrentDynaHashCxt, size);
304
+ }
305
+
306
+
307
+ /*
308
+ * HashCompareFunc for string keys
309
+ *
310
+ * Because we copy keys with strlcpy(), they will be truncated at keysize-1
311
+ * bytes, so we can only compare that many ... hence strncmp is almost but
312
+ * not quite the right thing.
313
+ */
314
+
315
+
316
+
317
+ /************************** CREATE ROUTINES **********************/
318
+
319
+ /*
320
+ * hash_create -- create a new dynamic hash table
321
+ *
322
+ * tabname: a name for the table (for debugging purposes)
323
+ * nelem: maximum number of elements expected
324
+ * *info: additional table parameters, as indicated by flags
325
+ * flags: bitmask indicating which parameters to take from *info
326
+ *
327
+ * Note: for a shared-memory hashtable, nelem needs to be a pretty good
328
+ * estimate, since we can't expand the table on the fly. But an unshared
329
+ * hashtable can be expanded on-the-fly, so it's better for nelem to be
330
+ * on the small side and let the table grow if it's exceeded. An overly
331
+ * large nelem will penalize hash_seq_search speed without buying much.
332
+ */
333
+
334
+
335
+ /*
336
+ * Set default HASHHDR parameters.
337
+ */
338
+ #ifdef HASH_STATISTICS
339
+ #endif
340
+
341
+ /*
342
+ * Given the user-specified entry size, choose nelem_alloc, ie, how many
343
+ * elements to add to the hash table when we need more.
344
+ */
345
+
346
+
347
+ /*
348
+ * Compute derived fields of hctl and build the initial directory/segment
349
+ * arrays
350
+ */
351
+ #ifdef HASH_DEBUG
352
+ #endif
353
+
354
+ /*
355
+ * Estimate the space needed for a hashtable containing the given number
356
+ * of entries of given size.
357
+ * NOTE: this is used to estimate the footprint of hashtables in shared
358
+ * memory; therefore it does not count HTAB which is in local memory.
359
+ * NB: assumes that all hash structure parameters have default values!
360
+ */
361
+
362
+
363
+ /*
364
+ * Select an appropriate directory size for a hashtable with the given
365
+ * maximum number of entries.
366
+ * This is only needed for hashtables in shared memory, whose directories
367
+ * cannot be expanded dynamically.
368
+ * NB: assumes that all hash structure parameters have default values!
369
+ *
370
+ * XXX this had better agree with the behavior of init_htab()...
371
+ */
372
+
373
+
374
+ /*
375
+ * Compute the required initial memory allocation for a shared-memory
376
+ * hashtable with the given parameters. We need space for the HASHHDR
377
+ * and for the (non expansible) directory.
378
+ */
379
+
380
+
381
+
382
+ /********************** DESTROY ROUTINES ************************/
383
+
384
+
385
+
386
+ #ifdef HASH_STATISTICS
387
+ #endif
388
+
389
+ /*******************************SEARCH ROUTINES *****************************/
390
+
391
+
392
+ /*
393
+ * get_hash_value -- exported routine to calculate a key's hash value
394
+ *
395
+ * We export this because for partitioned tables, callers need to compute
396
+ * the partition number (from the low-order bits of the hash value) before
397
+ * searching.
398
+ */
399
+
400
+
401
+ /* Convert a hash value to a bucket number */
402
+ static inline uint32
403
+ calc_bucket(HASHHDR *hctl, uint32 hash_val)
404
+ {
405
+ uint32 bucket;
406
+
407
+ bucket = hash_val & hctl->high_mask;
408
+ if (bucket > hctl->max_bucket)
409
+ bucket = bucket & hctl->low_mask;
410
+
411
+ return bucket;
412
+ }
413
+
414
+ /*
415
+ * hash_search -- look up key in table and perform action
416
+ * hash_search_with_hash_value -- same, with key's hash value already computed
417
+ *
418
+ * action is one of:
419
+ * HASH_FIND: look up key in table
420
+ * HASH_ENTER: look up key in table, creating entry if not present
421
+ * HASH_ENTER_NULL: same, but return NULL if out of memory
422
+ * HASH_REMOVE: look up key in table, remove entry if present
423
+ *
424
+ * Return value is a pointer to the element found/entered/removed if any,
425
+ * or NULL if no match was found. (NB: in the case of the REMOVE action,
426
+ * the result is a dangling pointer that shouldn't be dereferenced!)
427
+ *
428
+ * HASH_ENTER will normally ereport a generic "out of memory" error if
429
+ * it is unable to create a new entry. The HASH_ENTER_NULL operation is
430
+ * the same except it will return NULL if out of memory. Note that
431
+ * HASH_ENTER_NULL cannot be used with the default palloc-based allocator,
432
+ * since palloc internally ereports on out-of-memory.
433
+ *
434
+ * If foundPtr isn't NULL, then *foundPtr is set true if we found an
435
+ * existing entry in the table, false otherwise. This is needed in the
436
+ * HASH_ENTER case, but is redundant with the return value otherwise.
437
+ *
438
+ * For hash_search_with_hash_value, the hashvalue parameter must have been
439
+ * calculated with get_hash_value().
440
+ */
441
+ void *
442
+ hash_search(HTAB *hashp,
443
+ const void *keyPtr,
444
+ HASHACTION action,
445
+ bool *foundPtr)
446
+ {
447
+ return hash_search_with_hash_value(hashp,
448
+ keyPtr,
449
+ hashp->hash(keyPtr, hashp->keysize),
450
+ action,
451
+ foundPtr);
452
+ }
453
+
454
+ void *
455
+ hash_search_with_hash_value(HTAB *hashp,
456
+ const void *keyPtr,
457
+ uint32 hashvalue,
458
+ HASHACTION action,
459
+ bool *foundPtr)
460
+ {
461
+ HASHHDR *hctl = hashp->hctl;
462
+ int freelist_idx = FREELIST_IDX(hctl, hashvalue);
463
+ Size keysize;
464
+ uint32 bucket;
465
+ long segment_num;
466
+ long segment_ndx;
467
+ HASHSEGMENT segp;
468
+ HASHBUCKET currBucket;
469
+ HASHBUCKET *prevBucketPtr;
470
+ HashCompareFunc match;
471
+
472
+ #ifdef HASH_STATISTICS
473
+ hash_accesses++;
474
+ hctl->accesses++;
475
+ #endif
476
+
477
+ /*
478
+ * If inserting, check if it is time to split a bucket.
479
+ *
480
+ * NOTE: failure to expand table is not a fatal error, it just means we
481
+ * have to run at higher fill factor than we wanted. However, if we're
482
+ * using the palloc allocator then it will throw error anyway on
483
+ * out-of-memory, so we must do this before modifying the table.
484
+ */
485
+ if (action == HASH_ENTER || action == HASH_ENTER_NULL)
486
+ {
487
+ /*
488
+ * Can't split if running in partitioned mode, nor if frozen, nor if
489
+ * table is the subject of any active hash_seq_search scans. Strange
490
+ * order of these tests is to try to check cheaper conditions first.
491
+ */
492
+ if (!IS_PARTITIONED(hctl) && !hashp->frozen &&
493
+ hctl->freeList[0].nentries / (long) (hctl->max_bucket + 1) >= hctl->ffactor &&
494
+ !has_seq_scans(hashp))
495
+ (void) expand_table(hashp);
496
+ }
497
+
498
+ /*
499
+ * Do the initial lookup
500
+ */
501
+ bucket = calc_bucket(hctl, hashvalue);
502
+
503
+ segment_num = bucket >> hashp->sshift;
504
+ segment_ndx = MOD(bucket, hashp->ssize);
505
+
506
+ segp = hashp->dir[segment_num];
507
+
508
+ if (segp == NULL)
509
+ hash_corrupted(hashp);
510
+
511
+ prevBucketPtr = &segp[segment_ndx];
512
+ currBucket = *prevBucketPtr;
513
+
514
+ /*
515
+ * Follow collision chain looking for matching key
516
+ */
517
+ match = hashp->match; /* save one fetch in inner loop */
518
+ keysize = hashp->keysize; /* ditto */
519
+
520
+ while (currBucket != NULL)
521
+ {
522
+ if (currBucket->hashvalue == hashvalue &&
523
+ match(ELEMENTKEY(currBucket), keyPtr, keysize) == 0)
524
+ break;
525
+ prevBucketPtr = &(currBucket->link);
526
+ currBucket = *prevBucketPtr;
527
+ #ifdef HASH_STATISTICS
528
+ hash_collisions++;
529
+ hctl->collisions++;
530
+ #endif
531
+ }
532
+
533
+ if (foundPtr)
534
+ *foundPtr = (bool) (currBucket != NULL);
535
+
536
+ /*
537
+ * OK, now what?
538
+ */
539
+ switch (action)
540
+ {
541
+ case HASH_FIND:
542
+ if (currBucket != NULL)
543
+ return (void *) ELEMENTKEY(currBucket);
544
+ return NULL;
545
+
546
+ case HASH_REMOVE:
547
+ if (currBucket != NULL)
548
+ {
549
+ /* if partitioned, must lock to touch nentries and freeList */
550
+ if (IS_PARTITIONED(hctl))
551
+ SpinLockAcquire(&(hctl->freeList[freelist_idx].mutex));
552
+
553
+ /* delete the record from the appropriate nentries counter. */
554
+ Assert(hctl->freeList[freelist_idx].nentries > 0);
555
+ hctl->freeList[freelist_idx].nentries--;
556
+
557
+ /* remove record from hash bucket's chain. */
558
+ *prevBucketPtr = currBucket->link;
559
+
560
+ /* add the record to the appropriate freelist. */
561
+ currBucket->link = hctl->freeList[freelist_idx].freeList;
562
+ hctl->freeList[freelist_idx].freeList = currBucket;
563
+
564
+ if (IS_PARTITIONED(hctl))
565
+ SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
566
+
567
+ /*
568
+ * better hope the caller is synchronizing access to this
569
+ * element, because someone else is going to reuse it the next
570
+ * time something is added to the table
571
+ */
572
+ return (void *) ELEMENTKEY(currBucket);
573
+ }
574
+ return NULL;
575
+
576
+ case HASH_ENTER_NULL:
577
+ /* ENTER_NULL does not work with palloc-based allocator */
578
+ Assert(hashp->alloc != DynaHashAlloc);
579
+ /* FALL THRU */
580
+
581
+ case HASH_ENTER:
582
+ /* Return existing element if found, else create one */
583
+ if (currBucket != NULL)
584
+ return (void *) ELEMENTKEY(currBucket);
585
+
586
+ /* disallow inserts if frozen */
587
+ if (hashp->frozen)
588
+ elog(ERROR, "cannot insert into frozen hashtable \"%s\"",
589
+ hashp->tabname);
590
+
591
+ currBucket = get_hash_entry(hashp, freelist_idx);
592
+ if (currBucket == NULL)
593
+ {
594
+ /* out of memory */
595
+ if (action == HASH_ENTER_NULL)
596
+ return NULL;
597
+ /* report a generic message */
598
+ if (hashp->isshared)
599
+ ereport(ERROR,
600
+ (errcode(ERRCODE_OUT_OF_MEMORY),
601
+ errmsg("out of shared memory")));
602
+ else
603
+ ereport(ERROR,
604
+ (errcode(ERRCODE_OUT_OF_MEMORY),
605
+ errmsg("out of memory")));
606
+ }
607
+
608
+ /* link into hashbucket chain */
609
+ *prevBucketPtr = currBucket;
610
+ currBucket->link = NULL;
611
+
612
+ /* copy key into record */
613
+ currBucket->hashvalue = hashvalue;
614
+ hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, keysize);
615
+
616
+ /*
617
+ * Caller is expected to fill the data field on return. DO NOT
618
+ * insert any code that could possibly throw error here, as doing
619
+ * so would leave the table entry incomplete and hence corrupt the
620
+ * caller's data structure.
621
+ */
622
+
623
+ return (void *) ELEMENTKEY(currBucket);
624
+ }
625
+
626
+ elog(ERROR, "unrecognized hash action code: %d", (int) action);
627
+
628
+ return NULL; /* keep compiler quiet */
629
+ }
630
+
631
+ /*
632
+ * hash_update_hash_key -- change the hash key of an existing table entry
633
+ *
634
+ * This is equivalent to removing the entry, making a new entry, and copying
635
+ * over its data, except that the entry never goes to the table's freelist.
636
+ * Therefore this cannot suffer an out-of-memory failure, even if there are
637
+ * other processes operating in other partitions of the hashtable.
638
+ *
639
+ * Returns true if successful, false if the requested new hash key is already
640
+ * present. Throws error if the specified entry pointer isn't actually a
641
+ * table member.
642
+ *
643
+ * NB: currently, there is no special case for old and new hash keys being
644
+ * identical, which means we'll report false for that situation. This is
645
+ * preferable for existing uses.
646
+ *
647
+ * NB: for a partitioned hashtable, caller must hold lock on both relevant
648
+ * partitions, if the new hash key would belong to a different partition.
649
+ */
650
+ #ifdef HASH_STATISTICS
651
+ #endif
652
+ #ifdef HASH_STATISTICS
653
+ #endif
654
+
655
+ /*
656
+ * Allocate a new hashtable entry if possible; return NULL if out of memory.
657
+ * (Or, if the underlying space allocator throws error for out-of-memory,
658
+ * we won't return at all.)
659
+ */
660
+ static HASHBUCKET
661
+ get_hash_entry(HTAB *hashp, int freelist_idx)
662
+ {
663
+ HASHHDR *hctl = hashp->hctl;
664
+ HASHBUCKET newElement;
665
+
666
+ for (;;)
667
+ {
668
+ /* if partitioned, must lock to touch nentries and freeList */
669
+ if (IS_PARTITIONED(hctl))
670
+ SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
671
+
672
+ /* try to get an entry from the freelist */
673
+ newElement = hctl->freeList[freelist_idx].freeList;
674
+
675
+ if (newElement != NULL)
676
+ break;
677
+
678
+ if (IS_PARTITIONED(hctl))
679
+ SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
680
+
681
+ /*
682
+ * No free elements in this freelist. In a partitioned table, there
683
+ * might be entries in other freelists, but to reduce contention we
684
+ * prefer to first try to get another chunk of buckets from the main
685
+ * shmem allocator. If that fails, though, we *MUST* root through all
686
+ * the other freelists before giving up. There are multiple callers
687
+ * that assume that they can allocate every element in the initially
688
+ * requested table size, or that deleting an element guarantees they
689
+ * can insert a new element, even if shared memory is entirely full.
690
+ * Failing because the needed element is in a different freelist is
691
+ * not acceptable.
692
+ */
693
+ if (!element_alloc(hashp, hctl->nelem_alloc, freelist_idx))
694
+ {
695
+ int borrow_from_idx;
696
+
697
+ if (!IS_PARTITIONED(hctl))
698
+ return NULL; /* out of memory */
699
+
700
+ /* try to borrow element from another freelist */
701
+ borrow_from_idx = freelist_idx;
702
+ for (;;)
703
+ {
704
+ borrow_from_idx = (borrow_from_idx + 1) % NUM_FREELISTS;
705
+ if (borrow_from_idx == freelist_idx)
706
+ break; /* examined all freelists, fail */
707
+
708
+ SpinLockAcquire(&(hctl->freeList[borrow_from_idx].mutex));
709
+ newElement = hctl->freeList[borrow_from_idx].freeList;
710
+
711
+ if (newElement != NULL)
712
+ {
713
+ hctl->freeList[borrow_from_idx].freeList = newElement->link;
714
+ SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
715
+
716
+ /* careful: count the new element in its proper freelist */
717
+ SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
718
+ hctl->freeList[freelist_idx].nentries++;
719
+ SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
720
+
721
+ return newElement;
722
+ }
723
+
724
+ SpinLockRelease(&(hctl->freeList[borrow_from_idx].mutex));
725
+ }
726
+
727
+ /* no elements available to borrow either, so out of memory */
728
+ return NULL;
729
+ }
730
+ }
731
+
732
+ /* remove entry from freelist, bump nentries */
733
+ hctl->freeList[freelist_idx].freeList = newElement->link;
734
+ hctl->freeList[freelist_idx].nentries++;
735
+
736
+ if (IS_PARTITIONED(hctl))
737
+ SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
738
+
739
+ return newElement;
740
+ }
741
+
742
+ /*
743
+ * hash_get_num_entries -- get the number of entries in a hashtable
744
+ */
745
+
746
+
747
+ /*
748
+ * hash_seq_init/_search/_term
749
+ * Sequentially search through hash table and return
750
+ * all the elements one by one, return NULL when no more.
751
+ *
752
+ * hash_seq_term should be called if and only if the scan is abandoned before
753
+ * completion; if hash_seq_search returns NULL then it has already done the
754
+ * end-of-scan cleanup.
755
+ *
756
+ * NOTE: caller may delete the returned element before continuing the scan.
757
+ * However, deleting any other element while the scan is in progress is
758
+ * UNDEFINED (it might be the one that curIndex is pointing at!). Also,
759
+ * if elements are added to the table while the scan is in progress, it is
760
+ * unspecified whether they will be visited by the scan or not.
761
+ *
762
+ * NOTE: it is possible to use hash_seq_init/hash_seq_search without any
763
+ * worry about hash_seq_term cleanup, if the hashtable is first locked against
764
+ * further insertions by calling hash_freeze.
765
+ *
766
+ * NOTE: to use this with a partitioned hashtable, caller had better hold
767
+ * at least shared lock on all partitions of the table throughout the scan!
768
+ * We can cope with insertions or deletions by our own backend, but *not*
769
+ * with concurrent insertions or deletions by another.
770
+ */
771
+
772
+
773
+
774
+
775
+
776
+
777
+ /*
778
+ * hash_freeze
779
+ * Freeze a hashtable against future insertions (deletions are
780
+ * still allowed)
781
+ *
782
+ * The reason for doing this is that by preventing any more bucket splits,
783
+ * we no longer need to worry about registering hash_seq_search scans,
784
+ * and thus caller need not be careful about ensuring hash_seq_term gets
785
+ * called at the right times.
786
+ *
787
+ * Multiple calls to hash_freeze() are allowed, but you can't freeze a table
788
+ * with active scans (since hash_seq_term would then do the wrong thing).
789
+ */
790
+
791
+
792
+
793
+ /********************************* UTILITIES ************************/
794
+
795
+ /*
796
+ * Expand the table by adding one more hash bucket.
797
+ */
798
+ static bool
799
+ expand_table(HTAB *hashp)
800
+ {
801
+ HASHHDR *hctl = hashp->hctl;
802
+ HASHSEGMENT old_seg,
803
+ new_seg;
804
+ long old_bucket,
805
+ new_bucket;
806
+ long new_segnum,
807
+ new_segndx;
808
+ long old_segnum,
809
+ old_segndx;
810
+ HASHBUCKET *oldlink,
811
+ *newlink;
812
+ HASHBUCKET currElement,
813
+ nextElement;
814
+
815
+ Assert(!IS_PARTITIONED(hctl));
816
+
817
+ #ifdef HASH_STATISTICS
818
+ hash_expansions++;
819
+ #endif
820
+
821
+ new_bucket = hctl->max_bucket + 1;
822
+ new_segnum = new_bucket >> hashp->sshift;
823
+ new_segndx = MOD(new_bucket, hashp->ssize);
824
+
825
+ if (new_segnum >= hctl->nsegs)
826
+ {
827
+ /* Allocate new segment if necessary -- could fail if dir full */
828
+ if (new_segnum >= hctl->dsize)
829
+ if (!dir_realloc(hashp))
830
+ return false;
831
+ if (!(hashp->dir[new_segnum] = seg_alloc(hashp)))
832
+ return false;
833
+ hctl->nsegs++;
834
+ }
835
+
836
+ /* OK, we created a new bucket */
837
+ hctl->max_bucket++;
838
+
839
+ /*
840
+ * *Before* changing masks, find old bucket corresponding to same hash
841
+ * values; values in that bucket may need to be relocated to new bucket.
842
+ * Note that new_bucket is certainly larger than low_mask at this point,
843
+ * so we can skip the first step of the regular hash mask calc.
844
+ */
845
+ old_bucket = (new_bucket & hctl->low_mask);
846
+
847
+ /*
848
+ * If we crossed a power of 2, readjust masks.
849
+ */
850
+ if ((uint32) new_bucket > hctl->high_mask)
851
+ {
852
+ hctl->low_mask = hctl->high_mask;
853
+ hctl->high_mask = (uint32) new_bucket | hctl->low_mask;
854
+ }
855
+
856
+ /*
857
+ * Relocate records to the new bucket. NOTE: because of the way the hash
858
+ * masking is done in calc_bucket, only one old bucket can need to be
859
+ * split at this point. With a different way of reducing the hash value,
860
+ * that might not be true!
861
+ */
862
+ old_segnum = old_bucket >> hashp->sshift;
863
+ old_segndx = MOD(old_bucket, hashp->ssize);
864
+
865
+ old_seg = hashp->dir[old_segnum];
866
+ new_seg = hashp->dir[new_segnum];
867
+
868
+ oldlink = &old_seg[old_segndx];
869
+ newlink = &new_seg[new_segndx];
870
+
871
+ for (currElement = *oldlink;
872
+ currElement != NULL;
873
+ currElement = nextElement)
874
+ {
875
+ nextElement = currElement->link;
876
+ if ((long) calc_bucket(hctl, currElement->hashvalue) == old_bucket)
877
+ {
878
+ *oldlink = currElement;
879
+ oldlink = &currElement->link;
880
+ }
881
+ else
882
+ {
883
+ *newlink = currElement;
884
+ newlink = &currElement->link;
885
+ }
886
+ }
887
+ /* don't forget to terminate the rebuilt hash chains... */
888
+ *oldlink = NULL;
889
+ *newlink = NULL;
890
+
891
+ return true;
892
+ }
893
+
894
+
895
+ static bool
896
+ dir_realloc(HTAB *hashp)
897
+ {
898
+ HASHSEGMENT *p;
899
+ HASHSEGMENT *old_p;
900
+ long new_dsize;
901
+ long old_dirsize;
902
+ long new_dirsize;
903
+
904
+ if (hashp->hctl->max_dsize != NO_MAX_DSIZE)
905
+ return false;
906
+
907
+ /* Reallocate directory */
908
+ new_dsize = hashp->hctl->dsize << 1;
909
+ old_dirsize = hashp->hctl->dsize * sizeof(HASHSEGMENT);
910
+ new_dirsize = new_dsize * sizeof(HASHSEGMENT);
911
+
912
+ old_p = hashp->dir;
913
+ CurrentDynaHashCxt = hashp->hcxt;
914
+ p = (HASHSEGMENT *) hashp->alloc((Size) new_dirsize);
915
+
916
+ if (p != NULL)
917
+ {
918
+ memcpy(p, old_p, old_dirsize);
919
+ MemSet(((char *) p) + old_dirsize, 0, new_dirsize - old_dirsize);
920
+ hashp->dir = p;
921
+ hashp->hctl->dsize = new_dsize;
922
+
923
+ /* XXX assume the allocator is palloc, so we know how to free */
924
+ Assert(hashp->alloc == DynaHashAlloc);
925
+ pfree(old_p);
926
+
927
+ return true;
928
+ }
929
+
930
+ return false;
931
+ }
932
+
933
+
934
+ static HASHSEGMENT
935
+ seg_alloc(HTAB *hashp)
936
+ {
937
+ HASHSEGMENT segp;
938
+
939
+ CurrentDynaHashCxt = hashp->hcxt;
940
+ segp = (HASHSEGMENT) hashp->alloc(sizeof(HASHBUCKET) * hashp->ssize);
941
+
942
+ if (!segp)
943
+ return NULL;
944
+
945
+ MemSet(segp, 0, sizeof(HASHBUCKET) * hashp->ssize);
946
+
947
+ return segp;
948
+ }
949
+
950
+ /*
951
+ * allocate some new elements and link them into the indicated free list
952
+ */
953
+ static bool
954
+ element_alloc(HTAB *hashp, int nelem, int freelist_idx)
955
+ {
956
+ HASHHDR *hctl = hashp->hctl;
957
+ Size elementSize;
958
+ HASHELEMENT *firstElement;
959
+ HASHELEMENT *tmpElement;
960
+ HASHELEMENT *prevElement;
961
+ int i;
962
+
963
+ if (hashp->isfixed)
964
+ return false;
965
+
966
+ /* Each element has a HASHELEMENT header plus user data. */
967
+ elementSize = MAXALIGN(sizeof(HASHELEMENT)) + MAXALIGN(hctl->entrysize);
968
+
969
+ CurrentDynaHashCxt = hashp->hcxt;
970
+ firstElement = (HASHELEMENT *) hashp->alloc(nelem * elementSize);
971
+
972
+ if (!firstElement)
973
+ return false;
974
+
975
+ /* prepare to link all the new entries into the freelist */
976
+ prevElement = NULL;
977
+ tmpElement = firstElement;
978
+ for (i = 0; i < nelem; i++)
979
+ {
980
+ tmpElement->link = prevElement;
981
+ prevElement = tmpElement;
982
+ tmpElement = (HASHELEMENT *) (((char *) tmpElement) + elementSize);
983
+ }
984
+
985
+ /* if partitioned, must lock to touch freeList */
986
+ if (IS_PARTITIONED(hctl))
987
+ SpinLockAcquire(&hctl->freeList[freelist_idx].mutex);
988
+
989
+ /* freelist could be nonempty if two backends did this concurrently */
990
+ firstElement->link = hctl->freeList[freelist_idx].freeList;
991
+ hctl->freeList[freelist_idx].freeList = prevElement;
992
+
993
+ if (IS_PARTITIONED(hctl))
994
+ SpinLockRelease(&hctl->freeList[freelist_idx].mutex);
995
+
996
+ return true;
997
+ }
998
+
999
+ /* complain when we have detected a corrupted hashtable */
1000
+ static void
1001
+ hash_corrupted(HTAB *hashp)
1002
+ {
1003
+ /*
1004
+ * If the corruption is in a shared hashtable, we'd better force a
1005
+ * systemwide restart. Otherwise, just shut down this one backend.
1006
+ */
1007
+ if (hashp->isshared)
1008
+ elog(PANIC, "hash table \"%s\" corrupted", hashp->tabname);
1009
+ else
1010
+ elog(FATAL, "hash table \"%s\" corrupted", hashp->tabname);
1011
+ }
1012
+
1013
+ /* calculate ceil(log base 2) of num */
1014
+ #if SIZEOF_LONG < 8
1015
+ #else
1016
+ #endif
1017
+
1018
+ /* calculate first power of 2 >= num, bounded to what will fit in a long */
1019
+
1020
+
1021
+ /* calculate first power of 2 >= num, bounded to what will fit in an int */
1022
+
1023
+
1024
+
1025
+ /************************* SEQ SCAN TRACKING ************************/
1026
+
1027
+ /*
1028
+ * We track active hash_seq_search scans here. The need for this mechanism
1029
+ * comes from the fact that a scan will get confused if a bucket split occurs
1030
+ * while it's in progress: it might visit entries twice, or even miss some
1031
+ * entirely (if it's partway through the same bucket that splits). Hence
1032
+ * we want to inhibit bucket splits if there are any active scans on the
1033
+ * table being inserted into. This is a fairly rare case in current usage,
1034
+ * so just postponing the split until the next insertion seems sufficient.
1035
+ *
1036
+ * Given present usages of the function, only a few scans are likely to be
1037
+ * open concurrently; so a finite-size stack of open scans seems sufficient,
1038
+ * and we don't worry that linear search is too slow. Note that we do
1039
+ * allow multiple scans of the same hashtable to be open concurrently.
1040
+ *
1041
+ * This mechanism can support concurrent scan and insertion in a shared
1042
+ * hashtable if it's the same backend doing both. It would fail otherwise,
1043
+ * but locking reasons seem to preclude any such scenario anyway, so we don't
1044
+ * worry.
1045
+ *
1046
+ * This arrangement is reasonably robust if a transient hashtable is deleted
1047
+ * without notifying us. The absolute worst case is we might inhibit splits
1048
+ * in another table created later at exactly the same address. We will give
1049
+ * a warning at transaction end for reference leaks, so any bugs leading to
1050
+ * lack of notification should be easy to catch.
1051
+ */
1052
+
1053
+ #define MAX_SEQ_SCANS 100
1054
+
1055
+ static __thread HTAB *seq_scan_tables[MAX_SEQ_SCANS];
1056
+ /* tables being scanned */
1057
+ /* subtransaction nest level */
1058
+ static __thread int num_seq_scans = 0;
1059
+
1060
+
1061
+
1062
+ /* Register a table as having an active hash_seq_search scan */
1063
+
1064
+
1065
+ /* Deregister an active scan */
1066
+
1067
+
1068
+ /* Check if a table has any active scan */
1069
+ static bool
1070
+ has_seq_scans(HTAB *hashp)
1071
+ {
1072
+ int i;
1073
+
1074
+ for (i = 0; i < num_seq_scans; i++)
1075
+ {
1076
+ if (seq_scan_tables[i] == hashp)
1077
+ return true;
1078
+ }
1079
+ return false;
1080
+ }
1081
+
1082
+ /* Clean up any open scans at end of transaction */
1083
+
1084
+
1085
+ /* Clean up any open scans at end of subtransaction */
1086
+