pg_query 4.2.3 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (528) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -0
  3. data/README.md +6 -8
  4. data/Rakefile +3 -4
  5. data/ext/pg_query/extconf.rb +14 -6
  6. data/ext/pg_query/include/pg_query.h +27 -3
  7. data/ext/pg_query/include/pg_query_enum_defs.c +311 -149
  8. data/ext/pg_query/include/pg_query_fingerprint_conds.c +545 -489
  9. data/ext/pg_query/include/pg_query_fingerprint_defs.c +5092 -4432
  10. data/ext/pg_query/include/pg_query_outfuncs_conds.c +385 -343
  11. data/ext/pg_query/include/pg_query_outfuncs_defs.c +1294 -1161
  12. data/ext/pg_query/include/pg_query_readfuncs_conds.c +137 -123
  13. data/ext/pg_query/include/pg_query_readfuncs_defs.c +1657 -1496
  14. data/ext/pg_query/include/{access → postgres/access}/amapi.h +3 -1
  15. data/ext/pg_query/include/{access → postgres/access}/attmap.h +5 -3
  16. data/ext/pg_query/include/{access → postgres/access}/attnum.h +1 -1
  17. data/ext/pg_query/include/{access → postgres/access}/clog.h +1 -1
  18. data/ext/pg_query/include/{access → postgres/access}/commit_ts.h +1 -1
  19. data/ext/pg_query/include/{access → postgres/access}/detoast.h +1 -1
  20. data/ext/pg_query/include/{access → postgres/access}/genam.h +7 -5
  21. data/ext/pg_query/include/{access → postgres/access}/gin.h +16 -3
  22. data/ext/pg_query/include/{access → postgres/access}/htup.h +1 -1
  23. data/ext/pg_query/include/{access → postgres/access}/htup_details.h +6 -2
  24. data/ext/pg_query/include/{access → postgres/access}/itup.h +61 -58
  25. data/ext/pg_query/include/{access → postgres/access}/parallel.h +2 -2
  26. data/ext/pg_query/include/{access → postgres/access}/printtup.h +1 -1
  27. data/ext/pg_query/include/{access → postgres/access}/relation.h +1 -1
  28. data/ext/pg_query/include/{access → postgres/access}/relscan.h +1 -1
  29. data/ext/pg_query/include/{access → postgres/access}/rmgrlist.h +2 -2
  30. data/ext/pg_query/include/{access → postgres/access}/sdir.h +12 -3
  31. data/ext/pg_query/include/{access → postgres/access}/skey.h +1 -1
  32. data/ext/pg_query/include/{access → postgres/access}/stratnum.h +1 -1
  33. data/ext/pg_query/include/{access → postgres/access}/sysattr.h +1 -1
  34. data/ext/pg_query/include/{access → postgres/access}/table.h +1 -1
  35. data/ext/pg_query/include/{access → postgres/access}/tableam.h +68 -45
  36. data/ext/pg_query/include/{access → postgres/access}/toast_compression.h +1 -1
  37. data/ext/pg_query/include/{access → postgres/access}/transam.h +1 -1
  38. data/ext/pg_query/include/postgres/access/tsmapi.h +82 -0
  39. data/ext/pg_query/include/{access → postgres/access}/tupconvert.h +5 -2
  40. data/ext/pg_query/include/{access → postgres/access}/tupdesc.h +2 -2
  41. data/ext/pg_query/include/{access → postgres/access}/tupmacs.h +58 -98
  42. data/ext/pg_query/include/{access → postgres/access}/twophase.h +2 -2
  43. data/ext/pg_query/include/{access → postgres/access}/xact.h +25 -18
  44. data/ext/pg_query/include/{access → postgres/access}/xlog.h +15 -16
  45. data/ext/pg_query/include/{access → postgres/access}/xlog_internal.h +100 -62
  46. data/ext/pg_query/include/postgres/access/xlogbackup.h +41 -0
  47. data/ext/pg_query/include/{access → postgres/access}/xlogdefs.h +6 -25
  48. data/ext/pg_query/include/{access → postgres/access}/xlogprefetcher.h +1 -1
  49. data/ext/pg_query/include/{access → postgres/access}/xlogreader.h +7 -6
  50. data/ext/pg_query/include/{access → postgres/access}/xlogrecord.h +17 -5
  51. data/ext/pg_query/include/{access → postgres/access}/xlogrecovery.h +4 -3
  52. data/ext/pg_query/include/postgres/archive/archive_module.h +59 -0
  53. data/ext/pg_query/include/{c.h → postgres/c.h} +144 -156
  54. data/ext/pg_query/include/{catalog → postgres/catalog}/catalog.h +4 -3
  55. data/ext/pg_query/include/{catalog → postgres/catalog}/catversion.h +6 -2
  56. data/ext/pg_query/include/{catalog → postgres/catalog}/dependency.h +5 -4
  57. data/ext/pg_query/include/{catalog → postgres/catalog}/genbki.h +7 -6
  58. data/ext/pg_query/include/{catalog → postgres/catalog}/index.h +4 -4
  59. data/ext/pg_query/include/{catalog → postgres/catalog}/indexing.h +1 -1
  60. data/ext/pg_query/include/{catalog → postgres/catalog}/namespace.h +2 -2
  61. data/ext/pg_query/include/{catalog → postgres/catalog}/objectaccess.h +10 -8
  62. data/ext/pg_query/include/{catalog → postgres/catalog}/objectaddress.h +3 -3
  63. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_aggregate.h +1 -1
  64. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_aggregate_d.h +1 -1
  65. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_am.h +1 -1
  66. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_am_d.h +1 -1
  67. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_attribute.h +19 -17
  68. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_attribute_d.h +19 -19
  69. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_authid.h +1 -1
  70. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_authid_d.h +3 -1
  71. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_class.h +1 -1
  72. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_class_d.h +1 -1
  73. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_collation.h +3 -1
  74. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_collation_d.h +4 -3
  75. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_constraint.h +2 -2
  76. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_constraint_d.h +1 -1
  77. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_control.h +9 -1
  78. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_conversion.h +2 -2
  79. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_conversion_d.h +1 -1
  80. data/ext/pg_query/include/postgres/catalog/pg_database.h +124 -0
  81. data/ext/pg_query/include/postgres/catalog/pg_database_d.h +52 -0
  82. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_depend.h +1 -1
  83. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_depend_d.h +1 -1
  84. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_event_trigger.h +1 -1
  85. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_event_trigger_d.h +1 -1
  86. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_index.h +1 -1
  87. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_index_d.h +1 -1
  88. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_language.h +1 -1
  89. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_language_d.h +1 -1
  90. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_namespace.h +1 -1
  91. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_namespace_d.h +1 -1
  92. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opclass.h +1 -1
  93. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opclass_d.h +1 -1
  94. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_operator.h +1 -1
  95. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_operator_d.h +1 -1
  96. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opfamily.h +3 -2
  97. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opfamily_d.h +4 -2
  98. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_partitioned_table.h +1 -1
  99. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_partitioned_table_d.h +1 -1
  100. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_proc.h +1 -1
  101. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_proc_d.h +1 -1
  102. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_publication.h +2 -5
  103. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_publication_d.h +1 -1
  104. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_replication_origin.h +1 -1
  105. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_replication_origin_d.h +1 -1
  106. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic.h +1 -1
  107. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_d.h +1 -1
  108. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_ext.h +1 -1
  109. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_ext_d.h +1 -1
  110. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_transform.h +1 -1
  111. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_transform_d.h +1 -1
  112. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_trigger.h +1 -1
  113. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_trigger_d.h +1 -1
  114. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_config.h +1 -1
  115. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_config_d.h +1 -1
  116. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_dict.h +1 -1
  117. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_dict_d.h +1 -1
  118. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_parser.h +1 -1
  119. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_parser_d.h +1 -1
  120. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_template.h +1 -1
  121. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_template_d.h +1 -1
  122. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_type.h +1 -1
  123. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_type_d.h +1 -1
  124. data/ext/pg_query/include/{catalog → postgres/catalog}/storage.h +6 -6
  125. data/ext/pg_query/include/{commands → postgres/commands}/async.h +1 -1
  126. data/ext/pg_query/include/{commands → postgres/commands}/dbcommands.h +2 -1
  127. data/ext/pg_query/include/{commands → postgres/commands}/defrem.h +2 -1
  128. data/ext/pg_query/include/{commands → postgres/commands}/event_trigger.h +1 -1
  129. data/ext/pg_query/include/{commands → postgres/commands}/explain.h +3 -1
  130. data/ext/pg_query/include/{commands → postgres/commands}/prepare.h +1 -1
  131. data/ext/pg_query/include/{commands → postgres/commands}/tablespace.h +4 -4
  132. data/ext/pg_query/include/{commands → postgres/commands}/trigger.h +15 -14
  133. data/ext/pg_query/include/{commands → postgres/commands}/user.h +9 -3
  134. data/ext/pg_query/include/{commands → postgres/commands}/vacuum.h +60 -14
  135. data/ext/pg_query/include/postgres/common/cryptohash.h +39 -0
  136. data/ext/pg_query/include/{common → postgres/common}/file_perm.h +1 -1
  137. data/ext/pg_query/include/{common → postgres/common}/hashfn.h +1 -1
  138. data/ext/pg_query/include/postgres/common/int.h +437 -0
  139. data/ext/pg_query/include/{common → postgres/common}/keywords.h +1 -1
  140. data/ext/pg_query/include/{common → postgres/common}/kwlookup.h +2 -2
  141. data/ext/pg_query/include/{common → postgres/common}/pg_prng.h +3 -2
  142. data/ext/pg_query/include/{common → postgres/common}/relpath.h +20 -13
  143. data/ext/pg_query/include/postgres/common/scram-common.h +70 -0
  144. data/ext/pg_query/include/postgres/common/sha2.h +32 -0
  145. data/ext/pg_query/include/{common → postgres/common}/string.h +5 -3
  146. data/ext/pg_query/include/{common → postgres/common}/unicode_east_asian_fw_table.h +10 -10
  147. data/ext/pg_query/include/{common/unicode_combining_table.h → postgres/common/unicode_nonspacing_table.h} +31 -13
  148. data/ext/pg_query/include/postgres/copyfuncs.funcs.c +5013 -0
  149. data/ext/pg_query/include/postgres/copyfuncs.switch.c +938 -0
  150. data/ext/pg_query/include/{datatype → postgres/datatype}/timestamp.h +11 -4
  151. data/ext/pg_query/include/postgres/equalfuncs.funcs.c +3097 -0
  152. data/ext/pg_query/include/postgres/equalfuncs.switch.c +785 -0
  153. data/ext/pg_query/include/{executor → postgres/executor}/execdesc.h +1 -1
  154. data/ext/pg_query/include/{executor → postgres/executor}/executor.h +34 -17
  155. data/ext/pg_query/include/{executor → postgres/executor}/functions.h +1 -1
  156. data/ext/pg_query/include/{executor → postgres/executor}/instrument.h +1 -1
  157. data/ext/pg_query/include/{executor → postgres/executor}/spi.h +2 -2
  158. data/ext/pg_query/include/{executor → postgres/executor}/tablefunc.h +1 -1
  159. data/ext/pg_query/include/{executor → postgres/executor}/tuptable.h +18 -11
  160. data/ext/pg_query/include/{fmgr.h → postgres/fmgr.h} +21 -2
  161. data/ext/pg_query/include/postgres/foreign/fdwapi.h +294 -0
  162. data/ext/pg_query/include/{funcapi.h → postgres/funcapi.h} +12 -12
  163. data/ext/pg_query/include/postgres/gram.h +1127 -0
  164. data/ext/pg_query/include/{parser → postgres}/gramparse.h +4 -4
  165. data/ext/pg_query/include/{jit → postgres/jit}/jit.h +2 -2
  166. data/ext/pg_query/include/postgres/kwlist_d.h +1119 -0
  167. data/ext/pg_query/include/{lib → postgres/lib}/dshash.h +4 -1
  168. data/ext/pg_query/include/{lib → postgres/lib}/ilist.h +435 -22
  169. data/ext/pg_query/include/{lib → postgres/lib}/pairingheap.h +1 -1
  170. data/ext/pg_query/include/{lib → postgres/lib}/simplehash.h +9 -9
  171. data/ext/pg_query/include/{lib → postgres/lib}/sort_template.h +1 -1
  172. data/ext/pg_query/include/{lib → postgres/lib}/stringinfo.h +3 -3
  173. data/ext/pg_query/include/{libpq → postgres/libpq}/auth.h +8 -2
  174. data/ext/pg_query/include/{libpq → postgres/libpq}/crypt.h +1 -1
  175. data/ext/pg_query/include/{libpq → postgres/libpq}/hba.h +24 -17
  176. data/ext/pg_query/include/{libpq → postgres/libpq}/libpq-be.h +36 -25
  177. data/ext/pg_query/include/{libpq → postgres/libpq}/libpq.h +1 -1
  178. data/ext/pg_query/include/{libpq → postgres/libpq}/pqcomm.h +10 -41
  179. data/ext/pg_query/include/{libpq → postgres/libpq}/pqformat.h +2 -2
  180. data/ext/pg_query/include/{libpq → postgres/libpq}/pqsignal.h +22 -10
  181. data/ext/pg_query/include/postgres/libpq/sasl.h +136 -0
  182. data/ext/pg_query/include/postgres/libpq/scram.h +37 -0
  183. data/ext/pg_query/include/{mb → postgres/mb}/pg_wchar.h +35 -18
  184. data/ext/pg_query/include/{mb → postgres/mb}/stringinfo_mb.h +1 -1
  185. data/ext/pg_query/include/{miscadmin.h → postgres/miscadmin.h} +27 -15
  186. data/ext/pg_query/include/{nodes → postgres/nodes}/bitmapset.h +11 -7
  187. data/ext/pg_query/include/{nodes → postgres/nodes}/execnodes.h +83 -30
  188. data/ext/pg_query/include/{nodes → postgres/nodes}/extensible.h +5 -3
  189. data/ext/pg_query/include/{nodes → postgres/nodes}/lockoptions.h +1 -1
  190. data/ext/pg_query/include/{nodes → postgres/nodes}/makefuncs.h +14 -2
  191. data/ext/pg_query/include/{nodes → postgres/nodes}/memnodes.h +7 -4
  192. data/ext/pg_query/include/postgres/nodes/miscnodes.h +56 -0
  193. data/ext/pg_query/include/{nodes → postgres/nodes}/nodeFuncs.h +89 -29
  194. data/ext/pg_query/include/{nodes → postgres/nodes}/nodes.h +96 -511
  195. data/ext/pg_query/include/postgres/nodes/nodetags.h +471 -0
  196. data/ext/pg_query/include/{nodes → postgres/nodes}/params.h +3 -3
  197. data/ext/pg_query/include/{nodes → postgres/nodes}/parsenodes.h +377 -139
  198. data/ext/pg_query/include/{nodes → postgres/nodes}/pathnodes.h +1090 -440
  199. data/ext/pg_query/include/{nodes → postgres/nodes}/pg_list.h +30 -7
  200. data/ext/pg_query/include/{nodes → postgres/nodes}/plannodes.h +367 -124
  201. data/ext/pg_query/include/{nodes → postgres/nodes}/primnodes.h +670 -222
  202. data/ext/pg_query/include/{nodes → postgres/nodes}/print.h +1 -1
  203. data/ext/pg_query/include/{utils → postgres/nodes}/queryjumble.h +5 -7
  204. data/ext/pg_query/include/postgres/nodes/replnodes.h +111 -0
  205. data/ext/pg_query/include/postgres/nodes/supportnodes.h +346 -0
  206. data/ext/pg_query/include/{nodes → postgres/nodes}/tidbitmap.h +1 -1
  207. data/ext/pg_query/include/{nodes → postgres/nodes}/value.h +12 -2
  208. data/ext/pg_query/include/{optimizer → postgres/optimizer}/cost.h +6 -4
  209. data/ext/pg_query/include/{optimizer → postgres/optimizer}/geqo.h +1 -1
  210. data/ext/pg_query/include/{optimizer → postgres/optimizer}/geqo_gene.h +1 -1
  211. data/ext/pg_query/include/{optimizer → postgres/optimizer}/optimizer.h +8 -8
  212. data/ext/pg_query/include/{optimizer → postgres/optimizer}/paths.h +16 -7
  213. data/ext/pg_query/include/{optimizer → postgres/optimizer}/planmain.h +3 -6
  214. data/ext/pg_query/include/{parser → postgres/parser}/analyze.h +4 -3
  215. data/ext/pg_query/include/{parser → postgres/parser}/kwlist.h +12 -1
  216. data/ext/pg_query/include/{parser → postgres/parser}/parse_agg.h +4 -2
  217. data/ext/pg_query/include/{parser → postgres/parser}/parse_coerce.h +3 -1
  218. data/ext/pg_query/include/{parser → postgres/parser}/parse_expr.h +1 -1
  219. data/ext/pg_query/include/{parser → postgres/parser}/parse_func.h +1 -1
  220. data/ext/pg_query/include/{parser → postgres/parser}/parse_node.h +22 -4
  221. data/ext/pg_query/include/{parser → postgres/parser}/parse_oper.h +3 -3
  222. data/ext/pg_query/include/{parser → postgres/parser}/parse_relation.h +8 -3
  223. data/ext/pg_query/include/{parser → postgres/parser}/parse_type.h +4 -3
  224. data/ext/pg_query/include/{parser → postgres/parser}/parser.h +4 -4
  225. data/ext/pg_query/include/{parser → postgres/parser}/parsetree.h +1 -1
  226. data/ext/pg_query/include/{parser → postgres/parser}/scanner.h +2 -2
  227. data/ext/pg_query/include/{parser → postgres/parser}/scansup.h +1 -1
  228. data/ext/pg_query/include/{partitioning → postgres/partitioning}/partdefs.h +1 -1
  229. data/ext/pg_query/include/{pg_config.h → postgres/pg_config.h} +156 -217
  230. data/ext/pg_query/include/{pg_config_manual.h → postgres/pg_config_manual.h} +8 -46
  231. data/ext/pg_query/include/postgres/pg_config_os.h +8 -0
  232. data/ext/pg_query/include/{pg_getopt.h → postgres/pg_getopt.h} +1 -1
  233. data/ext/pg_query/include/{pg_trace.h → postgres/pg_trace.h} +1 -1
  234. data/ext/pg_query/include/{pgstat.h → postgres/pgstat.h} +173 -94
  235. data/ext/pg_query/include/{pgtime.h → postgres/pgtime.h} +3 -3
  236. data/ext/pg_query/include/{pl_gram.h → postgres/pl_gram.h} +66 -64
  237. data/ext/pg_query/include/{pl_reserved_kwlist.h → postgres/pl_reserved_kwlist.h} +1 -1
  238. data/ext/pg_query/include/{pl_reserved_kwlist_d.h → postgres/pl_reserved_kwlist_d.h} +1 -1
  239. data/ext/pg_query/include/{pl_unreserved_kwlist.h → postgres/pl_unreserved_kwlist.h} +2 -1
  240. data/ext/pg_query/include/{pl_unreserved_kwlist_d.h → postgres/pl_unreserved_kwlist_d.h} +48 -46
  241. data/ext/pg_query/include/{plpgsql.h → postgres/plpgsql.h} +28 -33
  242. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-arm.h +3 -3
  243. data/ext/pg_query/include/postgres/port/atomics/arch-hppa.h +17 -0
  244. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-ppc.h +21 -21
  245. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-x86.h +2 -2
  246. data/ext/pg_query/include/{port → postgres/port}/atomics/fallback.h +3 -3
  247. data/ext/pg_query/include/{port → postgres/port}/atomics/generic-gcc.h +1 -1
  248. data/ext/pg_query/include/postgres/port/atomics/generic-msvc.h +101 -0
  249. data/ext/pg_query/include/postgres/port/atomics/generic-sunpro.h +106 -0
  250. data/ext/pg_query/include/{port → postgres/port}/atomics/generic.h +1 -1
  251. data/ext/pg_query/include/{port → postgres/port}/atomics.h +2 -7
  252. data/ext/pg_query/include/{port → postgres/port}/pg_bitutils.h +62 -25
  253. data/ext/pg_query/include/{port → postgres/port}/pg_bswap.h +1 -1
  254. data/ext/pg_query/include/{port → postgres/port}/pg_crc32c.h +1 -1
  255. data/ext/pg_query/include/postgres/port/simd.h +375 -0
  256. data/ext/pg_query/include/postgres/port/win32/arpa/inet.h +3 -0
  257. data/ext/pg_query/include/postgres/port/win32/dlfcn.h +1 -0
  258. data/ext/pg_query/include/postgres/port/win32/grp.h +1 -0
  259. data/ext/pg_query/include/postgres/port/win32/netdb.h +7 -0
  260. data/ext/pg_query/include/postgres/port/win32/netinet/in.h +3 -0
  261. data/ext/pg_query/include/postgres/port/win32/netinet/tcp.h +7 -0
  262. data/ext/pg_query/include/postgres/port/win32/pwd.h +3 -0
  263. data/ext/pg_query/include/postgres/port/win32/sys/resource.h +20 -0
  264. data/ext/pg_query/include/postgres/port/win32/sys/select.h +3 -0
  265. data/ext/pg_query/include/postgres/port/win32/sys/socket.h +26 -0
  266. data/ext/pg_query/include/postgres/port/win32/sys/un.h +17 -0
  267. data/ext/pg_query/include/postgres/port/win32/sys/wait.h +3 -0
  268. data/ext/pg_query/include/postgres/port/win32.h +59 -0
  269. data/ext/pg_query/include/postgres/port/win32_msvc/dirent.h +34 -0
  270. data/ext/pg_query/include/postgres/port/win32_msvc/sys/file.h +1 -0
  271. data/ext/pg_query/include/postgres/port/win32_msvc/sys/param.h +1 -0
  272. data/ext/pg_query/include/postgres/port/win32_msvc/sys/time.h +1 -0
  273. data/ext/pg_query/include/postgres/port/win32_msvc/unistd.h +9 -0
  274. data/ext/pg_query/include/postgres/port/win32_msvc/utime.h +3 -0
  275. data/ext/pg_query/include/postgres/port/win32_port.h +594 -0
  276. data/ext/pg_query/include/{port.h → postgres/port.h} +42 -75
  277. data/ext/pg_query/include/postgres/portability/instr_time.h +197 -0
  278. data/ext/pg_query/include/postgres/postgres.h +579 -0
  279. data/ext/pg_query/include/{postgres_ext.h → postgres/postgres_ext.h} +0 -1
  280. data/ext/pg_query/include/{postmaster → postgres/postmaster}/autovacuum.h +1 -4
  281. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgworker.h +2 -2
  282. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgworker_internals.h +1 -1
  283. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgwriter.h +2 -2
  284. data/ext/pg_query/include/{postmaster → postgres/postmaster}/interrupt.h +1 -1
  285. data/ext/pg_query/include/postgres/postmaster/pgarch.h +36 -0
  286. data/ext/pg_query/include/{postmaster → postgres/postmaster}/postmaster.h +6 -3
  287. data/ext/pg_query/include/{postmaster → postgres/postmaster}/startup.h +3 -1
  288. data/ext/pg_query/include/{postmaster → postgres/postmaster}/syslogger.h +2 -2
  289. data/ext/pg_query/include/{postmaster → postgres/postmaster}/walwriter.h +3 -1
  290. data/ext/pg_query/include/{regex → postgres/regex}/regex.h +9 -6
  291. data/ext/pg_query/include/{replication → postgres/replication}/logicallauncher.h +6 -1
  292. data/ext/pg_query/include/{replication → postgres/replication}/logicalproto.h +30 -10
  293. data/ext/pg_query/include/postgres/replication/logicalworker.h +32 -0
  294. data/ext/pg_query/include/{replication → postgres/replication}/origin.h +4 -4
  295. data/ext/pg_query/include/{replication → postgres/replication}/reorderbuffer.h +113 -45
  296. data/ext/pg_query/include/{replication → postgres/replication}/slot.h +25 -6
  297. data/ext/pg_query/include/{replication → postgres/replication}/syncrep.h +2 -8
  298. data/ext/pg_query/include/{replication → postgres/replication}/walreceiver.h +15 -9
  299. data/ext/pg_query/include/{replication → postgres/replication}/walsender.h +13 -13
  300. data/ext/pg_query/include/{rewrite → postgres/rewrite}/prs2lock.h +1 -1
  301. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteHandler.h +1 -4
  302. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteManip.h +11 -2
  303. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteSupport.h +1 -1
  304. data/ext/pg_query/include/{storage → postgres/storage}/backendid.h +1 -1
  305. data/ext/pg_query/include/{storage → postgres/storage}/block.h +24 -31
  306. data/ext/pg_query/include/{storage → postgres/storage}/buf.h +1 -1
  307. data/ext/pg_query/include/{storage → postgres/storage}/bufmgr.h +183 -87
  308. data/ext/pg_query/include/{storage → postgres/storage}/bufpage.h +146 -93
  309. data/ext/pg_query/include/{storage → postgres/storage}/condition_variable.h +2 -2
  310. data/ext/pg_query/include/{storage → postgres/storage}/dsm.h +3 -6
  311. data/ext/pg_query/include/{storage → postgres/storage}/dsm_impl.h +4 -1
  312. data/ext/pg_query/include/{storage → postgres/storage}/fd.h +24 -20
  313. data/ext/pg_query/include/{storage → postgres/storage}/fileset.h +1 -1
  314. data/ext/pg_query/include/{storage → postgres/storage}/ipc.h +2 -2
  315. data/ext/pg_query/include/{storage → postgres/storage}/item.h +1 -1
  316. data/ext/pg_query/include/{storage → postgres/storage}/itemid.h +1 -1
  317. data/ext/pg_query/include/{storage → postgres/storage}/itemptr.h +94 -57
  318. data/ext/pg_query/include/{storage → postgres/storage}/large_object.h +1 -1
  319. data/ext/pg_query/include/{storage → postgres/storage}/latch.h +9 -1
  320. data/ext/pg_query/include/{storage → postgres/storage}/lmgr.h +6 -1
  321. data/ext/pg_query/include/{storage → postgres/storage}/lock.h +21 -13
  322. data/ext/pg_query/include/{storage → postgres/storage}/lockdefs.h +3 -3
  323. data/ext/pg_query/include/{storage → postgres/storage}/lwlock.h +16 -2
  324. data/ext/pg_query/include/{storage → postgres/storage}/off.h +1 -1
  325. data/ext/pg_query/include/{storage → postgres/storage}/pg_sema.h +1 -1
  326. data/ext/pg_query/include/{storage → postgres/storage}/pg_shmem.h +1 -1
  327. data/ext/pg_query/include/{storage → postgres/storage}/pmsignal.h +1 -1
  328. data/ext/pg_query/include/{storage → postgres/storage}/predicate.h +2 -2
  329. data/ext/pg_query/include/{storage → postgres/storage}/proc.h +22 -17
  330. data/ext/pg_query/include/{storage → postgres/storage}/procarray.h +3 -2
  331. data/ext/pg_query/include/{storage → postgres/storage}/proclist_types.h +1 -1
  332. data/ext/pg_query/include/{storage → postgres/storage}/procsignal.h +3 -1
  333. data/ext/pg_query/include/postgres/storage/relfilelocator.h +99 -0
  334. data/ext/pg_query/include/{storage → postgres/storage}/s_lock.h +66 -309
  335. data/ext/pg_query/include/{storage → postgres/storage}/sharedfileset.h +1 -1
  336. data/ext/pg_query/include/{storage → postgres/storage}/shm_mq.h +1 -1
  337. data/ext/pg_query/include/{storage → postgres/storage}/shm_toc.h +1 -1
  338. data/ext/pg_query/include/{storage → postgres/storage}/shmem.h +1 -23
  339. data/ext/pg_query/include/{storage → postgres/storage}/sinval.h +3 -3
  340. data/ext/pg_query/include/{storage → postgres/storage}/sinvaladt.h +4 -2
  341. data/ext/pg_query/include/{storage → postgres/storage}/smgr.h +12 -10
  342. data/ext/pg_query/include/{storage → postgres/storage}/spin.h +1 -1
  343. data/ext/pg_query/include/{storage → postgres/storage}/standby.h +9 -8
  344. data/ext/pg_query/include/{storage → postgres/storage}/standbydefs.h +1 -1
  345. data/ext/pg_query/include/{storage → postgres/storage}/sync.h +3 -3
  346. data/ext/pg_query/include/{tcop → postgres/tcop}/cmdtag.h +7 -2
  347. data/ext/pg_query/include/{tcop → postgres/tcop}/cmdtaglist.h +1 -1
  348. data/ext/pg_query/include/{tcop → postgres/tcop}/deparse_utility.h +1 -1
  349. data/ext/pg_query/include/{tcop → postgres/tcop}/dest.h +1 -3
  350. data/ext/pg_query/include/{tcop → postgres/tcop}/fastpath.h +1 -1
  351. data/ext/pg_query/include/{tcop → postgres/tcop}/pquery.h +1 -1
  352. data/ext/pg_query/include/{tcop → postgres/tcop}/tcopprot.h +3 -6
  353. data/ext/pg_query/include/{tcop → postgres/tcop}/utility.h +1 -1
  354. data/ext/pg_query/include/{tsearch → postgres/tsearch}/ts_cache.h +2 -4
  355. data/ext/pg_query/include/{utils → postgres/utils}/acl.h +26 -81
  356. data/ext/pg_query/include/{utils → postgres/utils}/aclchk_internal.h +1 -1
  357. data/ext/pg_query/include/{utils → postgres/utils}/array.h +19 -1
  358. data/ext/pg_query/include/{utils → postgres/utils}/backend_progress.h +2 -1
  359. data/ext/pg_query/include/{utils → postgres/utils}/backend_status.h +24 -3
  360. data/ext/pg_query/include/{utils → postgres/utils}/builtins.h +15 -6
  361. data/ext/pg_query/include/{utils → postgres/utils}/bytea.h +1 -1
  362. data/ext/pg_query/include/{utils → postgres/utils}/catcache.h +1 -1
  363. data/ext/pg_query/include/{utils → postgres/utils}/date.h +37 -9
  364. data/ext/pg_query/include/{utils → postgres/utils}/datetime.h +41 -21
  365. data/ext/pg_query/include/{utils → postgres/utils}/datum.h +1 -1
  366. data/ext/pg_query/include/{utils → postgres/utils}/dsa.h +5 -1
  367. data/ext/pg_query/include/{utils → postgres/utils}/elog.h +104 -29
  368. data/ext/pg_query/include/{utils → postgres/utils}/expandeddatum.h +14 -3
  369. data/ext/pg_query/include/{utils → postgres/utils}/expandedrecord.h +14 -4
  370. data/ext/pg_query/include/{utils → postgres/utils}/float.h +7 -6
  371. data/ext/pg_query/include/{utils → postgres/utils}/fmgroids.h +54 -1
  372. data/ext/pg_query/include/{utils → postgres/utils}/fmgrprotos.h +45 -3
  373. data/ext/pg_query/include/{utils → postgres/utils}/fmgrtab.h +1 -1
  374. data/ext/pg_query/include/{utils → postgres/utils}/guc.h +59 -86
  375. data/ext/pg_query/include/postgres/utils/guc_hooks.h +163 -0
  376. data/ext/pg_query/include/{utils → postgres/utils}/guc_tables.h +49 -3
  377. data/ext/pg_query/include/{utils → postgres/utils}/hsearch.h +1 -1
  378. data/ext/pg_query/include/{utils → postgres/utils}/inval.h +3 -3
  379. data/ext/pg_query/include/postgres/utils/logtape.h +77 -0
  380. data/ext/pg_query/include/{utils → postgres/utils}/lsyscache.h +5 -1
  381. data/ext/pg_query/include/{utils → postgres/utils}/memdebug.h +1 -1
  382. data/ext/pg_query/include/{utils → postgres/utils}/memutils.h +7 -51
  383. data/ext/pg_query/include/postgres/utils/memutils_internal.h +136 -0
  384. data/ext/pg_query/include/postgres/utils/memutils_memorychunk.h +237 -0
  385. data/ext/pg_query/include/{utils → postgres/utils}/numeric.h +20 -5
  386. data/ext/pg_query/include/{utils → postgres/utils}/palloc.h +9 -2
  387. data/ext/pg_query/include/{utils → postgres/utils}/partcache.h +3 -2
  388. data/ext/pg_query/include/{utils → postgres/utils}/pg_locale.h +22 -14
  389. data/ext/pg_query/include/{utils → postgres/utils}/pgstat_internal.h +37 -7
  390. data/ext/pg_query/include/{utils → postgres/utils}/plancache.h +1 -1
  391. data/ext/pg_query/include/{utils → postgres/utils}/portal.h +1 -1
  392. data/ext/pg_query/include/{utils → postgres/utils}/probes.h +6 -6
  393. data/ext/pg_query/include/postgres/utils/ps_status.h +47 -0
  394. data/ext/pg_query/include/{utils → postgres/utils}/queryenvironment.h +1 -1
  395. data/ext/pg_query/include/{utils → postgres/utils}/regproc.h +3 -3
  396. data/ext/pg_query/include/{utils → postgres/utils}/rel.h +60 -43
  397. data/ext/pg_query/include/{utils → postgres/utils}/relcache.h +13 -8
  398. data/ext/pg_query/include/{utils → postgres/utils}/reltrigger.h +1 -1
  399. data/ext/pg_query/include/{utils → postgres/utils}/resowner.h +1 -1
  400. data/ext/pg_query/include/{utils → postgres/utils}/ruleutils.h +6 -1
  401. data/ext/pg_query/include/{utils → postgres/utils}/sharedtuplestore.h +1 -1
  402. data/ext/pg_query/include/{utils → postgres/utils}/snapmgr.h +4 -2
  403. data/ext/pg_query/include/{utils → postgres/utils}/snapshot.h +1 -1
  404. data/ext/pg_query/include/{utils → postgres/utils}/sortsupport.h +2 -2
  405. data/ext/pg_query/include/{utils → postgres/utils}/syscache.h +4 -1
  406. data/ext/pg_query/include/{utils → postgres/utils}/timeout.h +1 -1
  407. data/ext/pg_query/include/{utils → postgres/utils}/timestamp.h +41 -11
  408. data/ext/pg_query/include/{utils → postgres/utils}/tuplesort.h +189 -35
  409. data/ext/pg_query/include/{utils → postgres/utils}/tuplestore.h +1 -1
  410. data/ext/pg_query/include/{utils → postgres/utils}/typcache.h +1 -1
  411. data/ext/pg_query/include/{utils → postgres/utils}/varlena.h +13 -1
  412. data/ext/pg_query/include/{utils → postgres/utils}/wait_event.h +9 -4
  413. data/ext/pg_query/include/{utils → postgres/utils}/xml.h +15 -5
  414. data/ext/pg_query/include/{postgres.h → postgres/varatt.h} +7 -457
  415. data/ext/pg_query/include/protobuf/pg_query.pb-c.h +6186 -5585
  416. data/ext/pg_query/include/protobuf/pg_query.pb.h +112443 -91222
  417. data/ext/pg_query/pg_query.c +10 -1
  418. data/ext/pg_query/pg_query.pb-c.c +19755 -17757
  419. data/ext/pg_query/pg_query_fingerprint.c +12 -8
  420. data/ext/pg_query/pg_query_fingerprint.h +1 -1
  421. data/ext/pg_query/pg_query_internal.h +1 -1
  422. data/ext/pg_query/pg_query_json_plpgsql.c +1 -0
  423. data/ext/pg_query/pg_query_normalize.c +1 -1
  424. data/ext/pg_query/pg_query_outfuncs_json.c +6 -0
  425. data/ext/pg_query/pg_query_outfuncs_protobuf.c +3 -2
  426. data/ext/pg_query/pg_query_parse.c +47 -5
  427. data/ext/pg_query/pg_query_parse_plpgsql.c +7 -5
  428. data/ext/pg_query/pg_query_readfuncs_protobuf.c +1 -0
  429. data/ext/pg_query/pg_query_ruby.c +5 -0
  430. data/ext/pg_query/pg_query_scan.c +1 -1
  431. data/ext/pg_query/pg_query_split.c +3 -3
  432. data/ext/pg_query/postgres_deparse.c +503 -105
  433. data/ext/pg_query/src_backend_catalog_namespace.c +7 -2
  434. data/ext/pg_query/src_backend_catalog_pg_proc.c +1 -1
  435. data/ext/pg_query/src_backend_commands_define.c +1 -1
  436. data/ext/pg_query/src_backend_nodes_bitmapset.c +11 -70
  437. data/ext/pg_query/src_backend_nodes_copyfuncs.c +96 -6202
  438. data/ext/pg_query/src_backend_nodes_equalfuncs.c +95 -4068
  439. data/ext/pg_query/src_backend_nodes_extensible.c +6 -29
  440. data/ext/pg_query/src_backend_nodes_list.c +14 -2
  441. data/ext/pg_query/src_backend_nodes_makefuncs.c +95 -1
  442. data/ext/pg_query/src_backend_nodes_nodeFuncs.c +283 -132
  443. data/ext/pg_query/src_backend_nodes_nodes.c +38 -0
  444. data/ext/pg_query/src_backend_nodes_value.c +1 -1
  445. data/ext/pg_query/src_backend_parser_gram.c +33208 -31806
  446. data/ext/pg_query/src_backend_parser_parser.c +28 -2
  447. data/ext/pg_query/src_backend_parser_scan.c +4318 -3329
  448. data/ext/pg_query/src_backend_parser_scansup.c +1 -1
  449. data/ext/pg_query/src_backend_storage_ipc_ipc.c +1 -1
  450. data/ext/pg_query/src_backend_tcop_postgres.c +66 -87
  451. data/ext/pg_query/src_backend_utils_activity_pgstat_database.c +1 -1
  452. data/ext/pg_query/src_backend_utils_adt_datum.c +5 -7
  453. data/ext/pg_query/src_backend_utils_adt_expandeddatum.c +1 -1
  454. data/ext/pg_query/src_backend_utils_adt_format_type.c +1 -1
  455. data/ext/pg_query/src_backend_utils_adt_numutils.c +489 -0
  456. data/ext/pg_query/src_backend_utils_adt_ruleutils.c +79 -5
  457. data/ext/pg_query/src_backend_utils_error_assert.c +4 -7
  458. data/ext/pg_query/src_backend_utils_error_elog.c +371 -249
  459. data/ext/pg_query/src_backend_utils_fmgr_fmgr.c +33 -1
  460. data/ext/pg_query/src_backend_utils_init_globals.c +6 -3
  461. data/ext/pg_query/src_backend_utils_mb_mbutils.c +21 -67
  462. data/ext/pg_query/src_backend_utils_misc_guc_tables.c +492 -0
  463. data/ext/pg_query/src_backend_utils_mmgr_alignedalloc.c +163 -0
  464. data/ext/pg_query/src_backend_utils_mmgr_aset.c +449 -312
  465. data/ext/pg_query/src_backend_utils_mmgr_generation.c +1039 -0
  466. data/ext/pg_query/src_backend_utils_mmgr_mcxt.c +398 -49
  467. data/ext/pg_query/src_backend_utils_mmgr_slab.c +1021 -0
  468. data/ext/pg_query/src_common_encnames.c +4 -1
  469. data/ext/pg_query/src_common_hashfn.c +1 -1
  470. data/ext/pg_query/src_common_keywords.c +1 -1
  471. data/ext/pg_query/src_common_kwlist_d.h +534 -510
  472. data/ext/pg_query/src_common_kwlookup.c +1 -1
  473. data/ext/pg_query/src_common_psprintf.c +1 -1
  474. data/ext/pg_query/src_common_stringinfo.c +4 -4
  475. data/ext/pg_query/src_common_wchar.c +9 -8
  476. data/ext/pg_query/src_pl_plpgsql_src_pl_comp.c +1 -1
  477. data/ext/pg_query/src_pl_plpgsql_src_pl_funcs.c +3 -1
  478. data/ext/pg_query/src_pl_plpgsql_src_pl_gram.c +661 -694
  479. data/ext/pg_query/src_pl_plpgsql_src_pl_handler.c +1 -1
  480. data/ext/pg_query/src_pl_plpgsql_src_pl_reserved_kwlist_d.h +1 -1
  481. data/ext/pg_query/src_pl_plpgsql_src_pl_scanner.c +1 -1
  482. data/ext/pg_query/src_pl_plpgsql_src_pl_unreserved_kwlist_d.h +48 -46
  483. data/ext/pg_query/src_port_pg_bitutils.c +79 -5
  484. data/ext/pg_query/src_port_pgstrcasecmp.c +29 -1
  485. data/ext/pg_query/src_port_snprintf.c +6 -10
  486. data/ext/pg_query/src_port_strerror.c +1 -1
  487. data/ext/pg_query/src_port_strlcpy.c +79 -0
  488. data/lib/pg_query/fingerprint.rb +2 -3
  489. data/lib/pg_query/node.rb +16 -11
  490. data/lib/pg_query/parse.rb +1 -1
  491. data/lib/pg_query/pg_query_pb.rb +166 -3191
  492. data/lib/pg_query/treewalker.rb +23 -5
  493. data/lib/pg_query/version.rb +1 -1
  494. metadata +432 -383
  495. data/ext/pg_query/guc-file.c +0 -0
  496. data/ext/pg_query/include/catalog/pg_parameter_acl.h +0 -60
  497. data/ext/pg_query/include/catalog/pg_parameter_acl_d.h +0 -34
  498. data/ext/pg_query/include/commands/variable.h +0 -38
  499. data/ext/pg_query/include/common/ip.h +0 -31
  500. data/ext/pg_query/include/getaddrinfo.h +0 -162
  501. data/ext/pg_query/include/kwlist_d.h +0 -1095
  502. data/ext/pg_query/include/parser/gram.h +0 -1101
  503. data/ext/pg_query/include/pg_config_os.h +0 -8
  504. data/ext/pg_query/include/portability/instr_time.h +0 -256
  505. data/ext/pg_query/include/postmaster/auxprocess.h +0 -20
  506. data/ext/pg_query/include/postmaster/fork_process.h +0 -17
  507. data/ext/pg_query/include/postmaster/pgarch.h +0 -73
  508. data/ext/pg_query/include/replication/logicalworker.h +0 -19
  509. data/ext/pg_query/include/storage/relfilenode.h +0 -99
  510. data/ext/pg_query/include/utils/dynahash.h +0 -20
  511. data/ext/pg_query/include/utils/pg_lsn.h +0 -29
  512. data/ext/pg_query/include/utils/pidfile.h +0 -56
  513. data/ext/pg_query/include/utils/ps_status.h +0 -25
  514. data/ext/pg_query/include/utils/rls.h +0 -50
  515. data/ext/pg_query/include/utils/tzparser.h +0 -39
  516. data/ext/pg_query/src_backend_postmaster_postmaster.c +0 -2201
  517. data/ext/pg_query/src_backend_storage_lmgr_s_lock.c +0 -371
  518. data/ext/pg_query/src_backend_utils_hash_dynahash.c +0 -1116
  519. data/ext/pg_query/src_backend_utils_misc_guc.c +0 -1993
  520. data/ext/pg_query/src_common_pg_prng.c +0 -152
  521. data/ext/pg_query/src_common_string.c +0 -92
  522. data/ext/pg_query/src_port_pgsleep.c +0 -69
  523. data/ext/pg_query/src_port_strnlen.c +0 -39
  524. /data/ext/pg_query/include/{access → postgres/access}/rmgr.h +0 -0
  525. /data/ext/pg_query/include/{pg_config_ext.h → postgres/pg_config_ext.h} +0 -0
  526. /data/ext/pg_query/include/{plerrcodes.h → postgres/plerrcodes.h} +0 -0
  527. /data/ext/pg_query/include/{storage → postgres/storage}/lwlocknames.h +0 -0
  528. /data/ext/pg_query/include/{utils → postgres/utils}/errcodes.h +0 -0
@@ -1,2201 +0,0 @@
1
- /*--------------------------------------------------------------------
2
- * Symbols referenced in this file:
3
- * - ClientAuthInProgress
4
- *--------------------------------------------------------------------
5
- */
6
-
7
- /*-------------------------------------------------------------------------
8
- *
9
- * postmaster.c
10
- * This program acts as a clearing house for requests to the
11
- * POSTGRES system. Frontend programs send a startup message
12
- * to the Postmaster and the postmaster uses the info in the
13
- * message to setup a backend process.
14
- *
15
- * The postmaster also manages system-wide operations such as
16
- * startup and shutdown. The postmaster itself doesn't do those
17
- * operations, mind you --- it just forks off a subprocess to do them
18
- * at the right times. It also takes care of resetting the system
19
- * if a backend crashes.
20
- *
21
- * The postmaster process creates the shared memory and semaphore
22
- * pools during startup, but as a rule does not touch them itself.
23
- * In particular, it is not a member of the PGPROC array of backends
24
- * and so it cannot participate in lock-manager operations. Keeping
25
- * the postmaster away from shared memory operations makes it simpler
26
- * and more reliable. The postmaster is almost always able to recover
27
- * from crashes of individual backends by resetting shared memory;
28
- * if it did much with shared memory then it would be prone to crashing
29
- * along with the backends.
30
- *
31
- * When a request message is received, we now fork() immediately.
32
- * The child process performs authentication of the request, and
33
- * then becomes a backend if successful. This allows the auth code
34
- * to be written in a simple single-threaded style (as opposed to the
35
- * crufty "poor man's multitasking" code that used to be needed).
36
- * More importantly, it ensures that blockages in non-multithreaded
37
- * libraries like SSL or PAM cannot cause denial of service to other
38
- * clients.
39
- *
40
- *
41
- * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
42
- * Portions Copyright (c) 1994, Regents of the University of California
43
- *
44
- *
45
- * IDENTIFICATION
46
- * src/backend/postmaster/postmaster.c
47
- *
48
- * NOTES
49
- *
50
- * Initialization:
51
- * The Postmaster sets up shared memory data structures
52
- * for the backends.
53
- *
54
- * Synchronization:
55
- * The Postmaster shares memory with the backends but should avoid
56
- * touching shared memory, so as not to become stuck if a crashing
57
- * backend screws up locks or shared memory. Likewise, the Postmaster
58
- * should never block on messages from frontend clients.
59
- *
60
- * Garbage Collection:
61
- * The Postmaster cleans up after backends if they have an emergency
62
- * exit and/or core dump.
63
- *
64
- * Error Reporting:
65
- * Use write_stderr() only for reporting "interactive" errors
66
- * (essentially, bogus arguments on the command line). Once the
67
- * postmaster is launched, use ereport().
68
- *
69
- *-------------------------------------------------------------------------
70
- */
71
-
72
- #include "postgres.h"
73
-
74
- #include <unistd.h>
75
- #include <signal.h>
76
- #include <time.h>
77
- #include <sys/wait.h>
78
- #include <ctype.h>
79
- #include <sys/stat.h>
80
- #include <sys/socket.h>
81
- #include <fcntl.h>
82
- #include <sys/param.h>
83
- #include <netdb.h>
84
- #include <limits.h>
85
-
86
- #ifdef HAVE_SYS_SELECT_H
87
- #include <sys/select.h>
88
- #endif
89
-
90
- #ifdef USE_BONJOUR
91
- #include <dns_sd.h>
92
- #endif
93
-
94
- #ifdef USE_SYSTEMD
95
- #include <systemd/sd-daemon.h>
96
- #endif
97
-
98
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
99
- #include <pthread.h>
100
- #endif
101
-
102
- #include "access/transam.h"
103
- #include "access/xlog.h"
104
- #include "access/xlogrecovery.h"
105
- #include "catalog/pg_control.h"
106
- #include "common/file_perm.h"
107
- #include "common/ip.h"
108
- #include "common/pg_prng.h"
109
- #include "common/string.h"
110
- #include "lib/ilist.h"
111
- #include "libpq/auth.h"
112
- #include "libpq/libpq.h"
113
- #include "libpq/pqformat.h"
114
- #include "libpq/pqsignal.h"
115
- #include "pg_getopt.h"
116
- #include "pgstat.h"
117
- #include "port/pg_bswap.h"
118
- #include "postmaster/autovacuum.h"
119
- #include "postmaster/auxprocess.h"
120
- #include "postmaster/bgworker_internals.h"
121
- #include "postmaster/fork_process.h"
122
- #include "postmaster/interrupt.h"
123
- #include "postmaster/pgarch.h"
124
- #include "postmaster/postmaster.h"
125
- #include "postmaster/syslogger.h"
126
- #include "replication/logicallauncher.h"
127
- #include "replication/walsender.h"
128
- #include "storage/fd.h"
129
- #include "storage/ipc.h"
130
- #include "storage/pg_shmem.h"
131
- #include "storage/pmsignal.h"
132
- #include "storage/proc.h"
133
- #include "tcop/tcopprot.h"
134
- #include "utils/builtins.h"
135
- #include "utils/datetime.h"
136
- #include "utils/memutils.h"
137
- #include "utils/pidfile.h"
138
- #include "utils/ps_status.h"
139
- #include "utils/queryjumble.h"
140
- #include "utils/timeout.h"
141
- #include "utils/timestamp.h"
142
- #include "utils/varlena.h"
143
-
144
- #ifdef EXEC_BACKEND
145
- #include "storage/spin.h"
146
- #endif
147
-
148
-
149
- /*
150
- * Possible types of a backend. Beyond being the possible bkend_type values in
151
- * struct bkend, these are OR-able request flag bits for SignalSomeChildren()
152
- * and CountChildren().
153
- */
154
- #define BACKEND_TYPE_NORMAL 0x0001 /* normal backend */
155
- #define BACKEND_TYPE_AUTOVAC 0x0002 /* autovacuum worker process */
156
- #define BACKEND_TYPE_WALSND 0x0004 /* walsender process */
157
- #define BACKEND_TYPE_BGWORKER 0x0008 /* bgworker process */
158
- #define BACKEND_TYPE_ALL 0x000F /* OR of all the above */
159
-
160
- /*
161
- * List of active backends (or child processes anyway; we don't actually
162
- * know whether a given child has become a backend or is still in the
163
- * authorization phase). This is used mainly to keep track of how many
164
- * children we have and send them appropriate signals when necessary.
165
- *
166
- * As shown in the above set of backend types, this list includes not only
167
- * "normal" client sessions, but also autovacuum workers, walsenders, and
168
- * background workers. (Note that at the time of launch, walsenders are
169
- * labeled BACKEND_TYPE_NORMAL; we relabel them to BACKEND_TYPE_WALSND
170
- * upon noticing they've changed their PMChildFlags entry. Hence that check
171
- * must be done before any operation that needs to distinguish walsenders
172
- * from normal backends.)
173
- *
174
- * Also, "dead_end" children are in it: these are children launched just for
175
- * the purpose of sending a friendly rejection message to a would-be client.
176
- * We must track them because they are attached to shared memory, but we know
177
- * they will never become live backends. dead_end children are not assigned a
178
- * PMChildSlot. dead_end children have bkend_type NORMAL.
179
- *
180
- * "Special" children such as the startup, bgwriter and autovacuum launcher
181
- * tasks are not in this list. They are tracked via StartupPID and other
182
- * pid_t variables below. (Thus, there can't be more than one of any given
183
- * "special" child process type. We use BackendList entries for any child
184
- * process there can be more than one of.)
185
- */
186
- typedef struct bkend
187
- {
188
- pid_t pid; /* process id of backend */
189
- int32 cancel_key; /* cancel key for cancels for this backend */
190
- int child_slot; /* PMChildSlot for this backend, if any */
191
- int bkend_type; /* child process flavor, see above */
192
- bool dead_end; /* is it going to send an error and quit? */
193
- bool bgworker_notify; /* gets bgworker start/stop notifications */
194
- dlist_node elem; /* list link in BackendList */
195
- } Backend;
196
-
197
-
198
-
199
- #ifdef EXEC_BACKEND
200
- static Backend *ShmemBackendArray;
201
- #endif
202
-
203
-
204
-
205
-
206
-
207
- /* The socket number we are listening for connections on */
208
-
209
-
210
- /* The directory names for Unix socket(s) */
211
-
212
-
213
- /* The TCP listen address(es) */
214
-
215
-
216
- /*
217
- * ReservedBackends is the number of backends reserved for superuser use.
218
- * This number is taken out of the pool size given by MaxConnections so
219
- * number of backend slots available to non-superusers is
220
- * (MaxConnections - ReservedBackends). Note what this really means is
221
- * "if there are <= ReservedBackends connections available, only superusers
222
- * can make new connections" --- pre-existing superuser connections don't
223
- * count against the limit.
224
- */
225
-
226
-
227
- /* The socket(s) we're listening to. */
228
- #define MAXLISTEN 64
229
-
230
-
231
- /*
232
- * These globals control the behavior of the postmaster in case some
233
- * backend dumps core. Normally, it kills all peers of the dead backend
234
- * and reinitializes shared memory. By specifying -s or -n, we can have
235
- * the postmaster stop (rather than kill) peers and not reinitialize
236
- * shared data structures. (Reinit is currently dead code, though.)
237
- */
238
-
239
-
240
-
241
- /* still more option variables */
242
-
243
-
244
-
245
-
246
-
247
- /* for ps display and logging */
248
-
249
-
250
-
251
-
252
-
253
-
254
-
255
-
256
- /* PIDs of special child processes; 0 when not running */
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
-
266
- /* Startup process's status */
267
- typedef enum
268
- {
269
- STARTUP_NOT_RUNNING,
270
- STARTUP_RUNNING,
271
- STARTUP_SIGNALED, /* we sent it a SIGQUIT or SIGKILL */
272
- STARTUP_CRASHED
273
- } StartupStatusEnum;
274
-
275
-
276
-
277
- /* Startup/shutdown state */
278
- #define NoShutdown 0
279
- #define SmartShutdown 1
280
- #define FastShutdown 2
281
- #define ImmediateShutdown 3
282
-
283
-
284
-
285
- /* T if recovering from backend crash */
286
-
287
- /*
288
- * We use a simple state machine to control startup, shutdown, and
289
- * crash recovery (which is rather like shutdown followed by startup).
290
- *
291
- * After doing all the postmaster initialization work, we enter PM_STARTUP
292
- * state and the startup process is launched. The startup process begins by
293
- * reading the control file and other preliminary initialization steps.
294
- * In a normal startup, or after crash recovery, the startup process exits
295
- * with exit code 0 and we switch to PM_RUN state. However, archive recovery
296
- * is handled specially since it takes much longer and we would like to support
297
- * hot standby during archive recovery.
298
- *
299
- * When the startup process is ready to start archive recovery, it signals the
300
- * postmaster, and we switch to PM_RECOVERY state. The background writer and
301
- * checkpointer are launched, while the startup process continues applying WAL.
302
- * If Hot Standby is enabled, then, after reaching a consistent point in WAL
303
- * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
304
- * state and begin accepting connections to perform read-only queries. When
305
- * archive recovery is finished, the startup process exits with exit code 0
306
- * and we switch to PM_RUN state.
307
- *
308
- * Normal child backends can only be launched when we are in PM_RUN or
309
- * PM_HOT_STANDBY state. (connsAllowed can also restrict launching.)
310
- * In other states we handle connection requests by launching "dead_end"
311
- * child processes, which will simply send the client an error message and
312
- * quit. (We track these in the BackendList so that we can know when they
313
- * are all gone; this is important because they're still connected to shared
314
- * memory, and would interfere with an attempt to destroy the shmem segment,
315
- * possibly leading to SHMALL failure when we try to make a new one.)
316
- * In PM_WAIT_DEAD_END state we are waiting for all the dead_end children
317
- * to drain out of the system, and therefore stop accepting connection
318
- * requests at all until the last existing child has quit (which hopefully
319
- * will not be very long).
320
- *
321
- * Notice that this state variable does not distinguish *why* we entered
322
- * states later than PM_RUN --- Shutdown and FatalError must be consulted
323
- * to find that out. FatalError is never true in PM_RECOVERY, PM_HOT_STANDBY,
324
- * or PM_RUN states, nor in PM_SHUTDOWN states (because we don't enter those
325
- * states when trying to recover from a crash). It can be true in PM_STARTUP
326
- * state, because we don't clear it until we've successfully started WAL redo.
327
- */
328
- typedef enum
329
- {
330
- PM_INIT, /* postmaster starting */
331
- PM_STARTUP, /* waiting for startup subprocess */
332
- PM_RECOVERY, /* in archive recovery mode */
333
- PM_HOT_STANDBY, /* in hot standby mode */
334
- PM_RUN, /* normal "database is alive" state */
335
- PM_STOP_BACKENDS, /* need to stop remaining backends */
336
- PM_WAIT_BACKENDS, /* waiting for live backends to exit */
337
- PM_SHUTDOWN, /* waiting for checkpointer to do shutdown
338
- * ckpt */
339
- PM_SHUTDOWN_2, /* waiting for archiver and walsenders to
340
- * finish */
341
- PM_WAIT_DEAD_END, /* waiting for dead_end children to exit */
342
- PM_NO_CHILDREN /* all important children have exited */
343
- } PMState;
344
-
345
-
346
-
347
- /*
348
- * While performing a "smart shutdown", we restrict new connections but stay
349
- * in PM_RUN or PM_HOT_STANDBY state until all the client backends are gone.
350
- * connsAllowed is a sub-state indicator showing the active restriction.
351
- * It is of no interest unless pmState is PM_RUN or PM_HOT_STANDBY.
352
- */
353
-
354
-
355
- /* Start time of SIGKILL timeout during immediate shutdown or child crash */
356
- /* Zero means timeout is not running */
357
-
358
-
359
- /* Length of said timeout */
360
- #define SIGKILL_CHILDREN_AFTER_SECS 5
361
-
362
- /* T if we've reached PM_RUN */
363
-
364
- __thread bool ClientAuthInProgress = false;
365
- /* T during new-client
366
- * authentication */
367
-
368
- /* stderr redirected for syslogger? */
369
-
370
- /* received START_AUTOVAC_LAUNCHER signal */
371
-
372
-
373
- /* the launcher needs to be signaled to communicate some condition */
374
-
375
-
376
- /* received START_WALRECEIVER signal */
377
-
378
-
379
- /* set when there's a worker that needs to be started up */
380
-
381
-
382
-
383
- #ifdef USE_SSL
384
- /* Set when and if SSL has been initialized properly */
385
- static bool LoadedSSL = false;
386
- #endif
387
-
388
- #ifdef USE_BONJOUR
389
- static DNSServiceRef bonjour_sdref = NULL;
390
- #endif
391
-
392
- /*
393
- * postmaster.c - function prototypes
394
- */
395
- static void CloseServerPorts(int status, Datum arg);
396
- static void unlink_external_pid_file(int status, Datum arg);
397
- static void getInstallationPaths(const char *argv0);
398
- static void checkControlFile(void);
399
- static Port *ConnCreate(int serverFd);
400
- static void ConnFree(Port *port);
401
- static void reset_shared(void);
402
- static void SIGHUP_handler(SIGNAL_ARGS);
403
- static void pmdie(SIGNAL_ARGS);
404
- static void reaper(SIGNAL_ARGS);
405
- static void sigusr1_handler(SIGNAL_ARGS);
406
- static void process_startup_packet_die(SIGNAL_ARGS);
407
- static void dummy_handler(SIGNAL_ARGS);
408
- static void StartupPacketTimeoutHandler(void);
409
- static void CleanupBackend(int pid, int exitstatus);
410
- static bool CleanupBackgroundWorker(int pid, int exitstatus);
411
- static void HandleChildCrash(int pid, int exitstatus, const char *procname);
412
- static void LogChildExit(int lev, const char *procname,
413
- int pid, int exitstatus);
414
- static void PostmasterStateMachine(void);
415
- static void BackendInitialize(Port *port);
416
- static void BackendRun(Port *port) pg_attribute_noreturn();
417
- static void ExitPostmaster(int status) pg_attribute_noreturn();
418
- static int ServerLoop(void);
419
- static int BackendStartup(Port *port);
420
- static int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done);
421
- static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
422
- static void processCancelRequest(Port *port, void *pkt);
423
- static int initMasks(fd_set *rmask);
424
- static void report_fork_failure_to_client(Port *port, int errnum);
425
- static CAC_state canAcceptConnections(int backend_type);
426
- static bool RandomCancelKey(int32 *cancel_key);
427
- static void signal_child(pid_t pid, int signal);
428
- static bool SignalSomeChildren(int signal, int targets);
429
- static void TerminateChildren(int signal);
430
-
431
- #define SignalChildren(sig) SignalSomeChildren(sig, BACKEND_TYPE_ALL)
432
-
433
- static int CountChildren(int target);
434
- static bool assign_backendlist_entry(RegisteredBgWorker *rw);
435
- static void maybe_start_bgworkers(void);
436
- static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
437
- static pid_t StartChildProcess(AuxProcType type);
438
- static void StartAutovacuumWorker(void);
439
- static void MaybeStartWalReceiver(void);
440
- static void InitPostmasterDeathWatchHandle(void);
441
-
442
- /*
443
- * Archiver is allowed to start up at the current postmaster state?
444
- *
445
- * If WAL archiving is enabled always, we are allowed to start archiver
446
- * even during recovery.
447
- */
448
- #define PgArchStartupAllowed() \
449
- (((XLogArchivingActive() && pmState == PM_RUN) || \
450
- (XLogArchivingAlways() && \
451
- (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY))) && \
452
- PgArchCanRestart())
453
-
454
- #ifdef EXEC_BACKEND
455
-
456
- #ifdef WIN32
457
- #define WNOHANG 0 /* ignored, so any integer value will do */
458
-
459
- static pid_t waitpid(pid_t pid, int *exitstatus, int options);
460
- static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
461
-
462
- static HANDLE win32ChildQueue;
463
-
464
- typedef struct
465
- {
466
- HANDLE waitHandle;
467
- HANDLE procHandle;
468
- DWORD procId;
469
- } win32_deadchild_waitinfo;
470
- #endif /* WIN32 */
471
-
472
- static pid_t backend_forkexec(Port *port);
473
- static pid_t internal_forkexec(int argc, char *argv[], Port *port);
474
-
475
- /* Type for a socket that can be inherited to a client process */
476
- #ifdef WIN32
477
- typedef struct
478
- {
479
- SOCKET origsocket; /* Original socket value, or PGINVALID_SOCKET
480
- * if not a socket */
481
- WSAPROTOCOL_INFO wsainfo;
482
- } InheritableSocket;
483
- #else
484
- typedef int InheritableSocket;
485
- #endif
486
-
487
- /*
488
- * Structure contains all variables passed to exec:ed backends
489
- */
490
- typedef struct
491
- {
492
- Port port;
493
- InheritableSocket portsocket;
494
- char DataDir[MAXPGPATH];
495
- pgsocket ListenSocket[MAXLISTEN];
496
- int32 MyCancelKey;
497
- int MyPMChildSlot;
498
- #ifndef WIN32
499
- unsigned long UsedShmemSegID;
500
- #else
501
- void *ShmemProtectiveRegion;
502
- HANDLE UsedShmemSegID;
503
- #endif
504
- void *UsedShmemSegAddr;
505
- slock_t *ShmemLock;
506
- VariableCache ShmemVariableCache;
507
- Backend *ShmemBackendArray;
508
- #ifndef HAVE_SPINLOCKS
509
- PGSemaphore *SpinlockSemaArray;
510
- #endif
511
- int NamedLWLockTrancheRequests;
512
- NamedLWLockTranche *NamedLWLockTrancheArray;
513
- LWLockPadded *MainLWLockArray;
514
- slock_t *ProcStructLock;
515
- PROC_HDR *ProcGlobal;
516
- PGPROC *AuxiliaryProcs;
517
- PGPROC *PreparedXactProcs;
518
- PMSignalData *PMSignalState;
519
- pid_t PostmasterPid;
520
- TimestampTz PgStartTime;
521
- TimestampTz PgReloadTime;
522
- pg_time_t first_syslogger_file_time;
523
- bool redirection_done;
524
- bool IsBinaryUpgrade;
525
- bool query_id_enabled;
526
- int max_safe_fds;
527
- int MaxBackends;
528
- #ifdef WIN32
529
- HANDLE PostmasterHandle;
530
- HANDLE initial_signal_pipe;
531
- HANDLE syslogPipe[2];
532
- #else
533
- int postmaster_alive_fds[2];
534
- int syslogPipe[2];
535
- #endif
536
- char my_exec_path[MAXPGPATH];
537
- char pkglib_path[MAXPGPATH];
538
- } BackendParameters;
539
-
540
- static void read_backend_variables(char *id, Port *port);
541
- static void restore_backend_variables(BackendParameters *param, Port *port);
542
-
543
- #ifndef WIN32
544
- static bool save_backend_variables(BackendParameters *param, Port *port);
545
- #else
546
- static bool save_backend_variables(BackendParameters *param, Port *port,
547
- HANDLE childProcess, pid_t childPid);
548
- #endif
549
-
550
- static void ShmemBackendArrayAdd(Backend *bn);
551
- static void ShmemBackendArrayRemove(Backend *bn);
552
- #endif /* EXEC_BACKEND */
553
-
554
- #define StartupDataBase() StartChildProcess(StartupProcess)
555
- #define StartArchiver() StartChildProcess(ArchiverProcess)
556
- #define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
557
- #define StartCheckpointer() StartChildProcess(CheckpointerProcess)
558
- #define StartWalWriter() StartChildProcess(WalWriterProcess)
559
- #define StartWalReceiver() StartChildProcess(WalReceiverProcess)
560
-
561
- /* Macros to check exit status of a child process */
562
- #define EXIT_STATUS_0(st) ((st) == 0)
563
- #define EXIT_STATUS_1(st) (WIFEXITED(st) && WEXITSTATUS(st) == 1)
564
- #define EXIT_STATUS_3(st) (WIFEXITED(st) && WEXITSTATUS(st) == 3)
565
-
566
- #ifndef WIN32
567
- /*
568
- * File descriptors for pipe used to monitor if postmaster is alive.
569
- * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
570
- */
571
-
572
- #else
573
- /* Process handle of postmaster used for the same purpose on Windows */
574
- HANDLE PostmasterHandle;
575
- #endif
576
-
577
- /*
578
- * Postmaster main entry point
579
- */
580
- #ifdef WIN32
581
- #endif
582
- #ifdef SIGURG
583
- #endif
584
- #ifdef SIGTTIN
585
- #endif
586
- #ifdef SIGTTOU
587
- #endif
588
- #ifdef SIGXFSZ
589
- #endif
590
- #ifdef HAVE_INT_OPTRESET
591
- #endif
592
- #ifdef USE_SSL
593
- #endif
594
- #ifdef WIN32
595
- #endif
596
- #ifdef EXEC_BACKEND
597
- #endif
598
- #ifdef USE_BONJOUR
599
- #endif
600
- #ifdef HAVE_UNIX_SOCKETS
601
- #endif
602
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
603
- #endif
604
-
605
-
606
- /*
607
- * on_proc_exit callback to close server's listen sockets
608
- */
609
-
610
-
611
- /*
612
- * on_proc_exit callback to delete external_pid_file
613
- */
614
-
615
-
616
-
617
- /*
618
- * Compute and check the directory paths to files that are part of the
619
- * installation (as deduced from the postgres executable's own location)
620
- */
621
- #ifdef EXEC_BACKEND
622
- #endif
623
-
624
- /*
625
- * Check that pg_control exists in the correct location in the data directory.
626
- *
627
- * No attempt is made to validate the contents of pg_control here. This is
628
- * just a sanity check to see if we are looking at a real data directory.
629
- */
630
-
631
-
632
- /*
633
- * Determine how long should we let ServerLoop sleep.
634
- *
635
- * In normal conditions we wait at most one minute, to ensure that the other
636
- * background tasks handled by ServerLoop get done even when no requests are
637
- * arriving. However, if there are background workers waiting to be started,
638
- * we don't actually sleep so that they are quickly serviced. Other exception
639
- * cases are as shown in the code.
640
- */
641
-
642
-
643
- /*
644
- * Main idle loop of postmaster
645
- *
646
- * NB: Needs to be called with signals blocked
647
- */
648
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
649
- #endif
650
-
651
- /*
652
- * Initialise the masks for select() for the ports we are listening on.
653
- * Return the number of sockets to listen on.
654
- */
655
-
656
-
657
-
658
- /*
659
- * Read a client's startup packet and do something according to it.
660
- *
661
- * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
662
- * not return at all.
663
- *
664
- * (Note that ereport(FATAL) stuff is sent to the client, so only use it
665
- * if that's what you want. Return STATUS_ERROR if you don't want to
666
- * send anything to the client, which would typically be appropriate
667
- * if we detect a communications failure.)
668
- *
669
- * Set ssl_done and/or gss_done when negotiation of an encrypted layer
670
- * (currently, TLS or GSSAPI) is completed. A successful negotiation of either
671
- * encryption layer sets both flags, but a rejected negotiation sets only the
672
- * flag for that layer, since the client may wish to try the other one. We
673
- * should make no assumption here about the order in which the client may make
674
- * requests.
675
- */
676
- #ifdef USE_SSL
677
- #else
678
- #endif
679
- #ifdef USE_SSL
680
- #endif
681
- #ifdef ENABLE_GSS
682
- #endif
683
- #ifdef ENABLE_GSS
684
- #endif
685
-
686
- /*
687
- * Send a NegotiateProtocolVersion to the client. This lets the client know
688
- * that they have requested a newer minor protocol version than we are able
689
- * to speak. We'll speak the highest version we know about; the client can,
690
- * of course, abandon the connection if that's a problem.
691
- *
692
- * We also include in the response a list of protocol options we didn't
693
- * understand. This allows clients to include optional parameters that might
694
- * be present either in newer protocol versions or third-party protocol
695
- * extensions without fear of having to reconnect if those options are not
696
- * understood, while at the same time making certain that the client is aware
697
- * of which options were actually accepted.
698
- */
699
-
700
-
701
- /*
702
- * The client has sent a cancel request packet, not a normal
703
- * start-a-new-connection packet. Perform the necessary processing.
704
- * Nothing is sent back to the client.
705
- */
706
- #ifndef EXEC_BACKEND
707
- #else
708
- #endif
709
- #ifndef EXEC_BACKEND
710
- #else
711
- #endif
712
- #ifndef EXEC_BACKEND /* make GNU Emacs 26.1 see brace balance */
713
- #else
714
- #endif
715
-
716
- /*
717
- * canAcceptConnections --- check to see if database state allows connections
718
- * of the specified type. backend_type can be BACKEND_TYPE_NORMAL,
719
- * BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER. (Note that we don't yet
720
- * know whether a NORMAL connection might turn into a walsender.)
721
- */
722
-
723
-
724
-
725
- /*
726
- * ConnCreate -- create a local connection data structure
727
- *
728
- * Returns NULL on failure, other than out-of-memory which is fatal.
729
- */
730
-
731
-
732
-
733
- /*
734
- * ConnFree -- free a local connection data structure
735
- *
736
- * Caller has already closed the socket if any, so there's not much
737
- * to do here.
738
- */
739
-
740
-
741
-
742
- /*
743
- * ClosePostmasterPorts -- close all the postmaster's open sockets
744
- *
745
- * This is called during child process startup to release file descriptors
746
- * that are not needed by that child process. The postmaster still has
747
- * them open, of course.
748
- *
749
- * Note: we pass am_syslogger as a boolean because we don't want to set
750
- * the global variable yet when this is called.
751
- */
752
- #ifndef WIN32
753
- #endif
754
- #ifndef WIN32
755
- #else
756
- #endif
757
- #ifdef USE_BONJOUR
758
- #endif
759
-
760
-
761
- /*
762
- * InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
763
- *
764
- * Called early in the postmaster and every backend.
765
- */
766
- #ifndef WIN32
767
- #endif
768
-
769
-
770
- /*
771
- * reset_shared -- reset shared memory and semaphores
772
- */
773
-
774
-
775
-
776
- /*
777
- * SIGHUP -- reread config files, and tell children to do same
778
- */
779
- #ifdef WIN32
780
- #endif
781
- #ifdef USE_SSL
782
- #endif
783
- #ifdef EXEC_BACKEND
784
- #endif
785
- #ifdef WIN32
786
- #endif
787
-
788
-
789
- /*
790
- * pmdie -- signal handler for processing various postmaster signals.
791
- */
792
- #ifdef WIN32
793
- #endif
794
- #ifdef USE_SYSTEMD
795
- #endif
796
- #ifdef USE_SYSTEMD
797
- #endif
798
- #ifdef USE_SYSTEMD
799
- #endif
800
- #ifdef WIN32
801
- #endif
802
-
803
- /*
804
- * Reaper -- signal handler to cleanup after a child process dies.
805
- */
806
- #ifdef WIN32
807
- #endif
808
- #ifdef USE_SYSTEMD
809
- #endif
810
- #ifdef WIN32
811
- #endif
812
-
813
- /*
814
- * Scan the bgworkers list and see if the given PID (which has just stopped
815
- * or crashed) is in it. Handle its shutdown if so, and return true. If not a
816
- * bgworker, return false.
817
- *
818
- * This is heavily based on CleanupBackend. One important difference is that
819
- * we don't know yet that the dying process is a bgworker, so we must be silent
820
- * until we're sure it is.
821
- */
822
- #ifdef WIN32
823
- #endif
824
- #ifdef EXEC_BACKEND
825
- #endif
826
-
827
- /*
828
- * CleanupBackend -- cleanup after terminated backend.
829
- *
830
- * Remove all local state associated with backend.
831
- *
832
- * If you change this, see also CleanupBackgroundWorker.
833
- */
834
- #ifdef WIN32
835
- #endif
836
- #ifdef EXEC_BACKEND
837
- #endif
838
-
839
- /*
840
- * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
841
- * walwriter, autovacuum, archiver or background worker.
842
- *
843
- * The objectives here are to clean up our local state about the child
844
- * process, and to signal all other remaining children to quickdie.
845
- */
846
- #ifdef EXEC_BACKEND
847
- #endif
848
- #ifdef EXEC_BACKEND
849
- #endif
850
-
851
- /*
852
- * Log the death of a child process.
853
- */
854
- #if defined(WIN32)
855
- #else
856
- #endif
857
-
858
- /*
859
- * Advance the postmaster's state machine and take actions as appropriate
860
- *
861
- * This is common code for pmdie(), reaper() and sigusr1_handler(), which
862
- * receive the signals that might mean we need to change state.
863
- */
864
-
865
-
866
-
867
- /*
868
- * Send a signal to a postmaster child process
869
- *
870
- * On systems that have setsid(), each child process sets itself up as a
871
- * process group leader. For signals that are generally interpreted in the
872
- * appropriate fashion, we signal the entire process group not just the
873
- * direct child process. This allows us to, for example, SIGQUIT a blocked
874
- * archive_recovery script, or SIGINT a script being run by a backend via
875
- * system().
876
- *
877
- * There is a race condition for recently-forked children: they might not
878
- * have executed setsid() yet. So we signal the child directly as well as
879
- * the group. We assume such a child will handle the signal before trying
880
- * to spawn any grandchild processes. We also assume that signaling the
881
- * child twice will not cause any problems.
882
- */
883
- #ifdef HAVE_SETSID
884
- #endif
885
-
886
- /*
887
- * Send a signal to the targeted children (but NOT special children;
888
- * dead_end children are never signaled, either).
889
- */
890
-
891
-
892
- /*
893
- * Send a termination signal to children. This considers all of our children
894
- * processes, except syslogger and dead_end backends.
895
- */
896
-
897
-
898
- /*
899
- * BackendStartup -- start backend process
900
- *
901
- * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
902
- *
903
- * Note: if you change this code, also consider StartAutovacuumWorker.
904
- */
905
- #ifdef EXEC_BACKEND
906
- #else /* !EXEC_BACKEND */
907
- #endif /* EXEC_BACKEND */
908
- #ifdef EXEC_BACKEND
909
- #endif
910
-
911
- /*
912
- * Try to report backend fork() failure to client before we close the
913
- * connection. Since we do not care to risk blocking the postmaster on
914
- * this connection, we set the connection to non-blocking and try only once.
915
- *
916
- * This is grungy special-purpose code; we cannot use backend libpq since
917
- * it's not up and running.
918
- */
919
-
920
-
921
-
922
- /*
923
- * BackendInitialize -- initialize an interactive (postmaster-child)
924
- * backend process, and collect the client's startup packet.
925
- *
926
- * returns: nothing. Will not return at all if there's any failure.
927
- *
928
- * Note: this code does not depend on having any access to shared memory.
929
- * Indeed, our approach to SIGTERM/timeout handling *requires* that
930
- * shared memory not have been touched yet; see comments within.
931
- * In the EXEC_BACKEND case, we are physically attached to shared memory
932
- * but have not yet set up most of our local pointers to shmem structures.
933
- */
934
-
935
-
936
-
937
- /*
938
- * BackendRun -- set up the backend's argument list and invoke PostgresMain()
939
- *
940
- * returns:
941
- * Doesn't return at all.
942
- */
943
-
944
-
945
-
946
- #ifdef EXEC_BACKEND
947
-
948
- /*
949
- * postmaster_forkexec -- fork and exec a postmaster subprocess
950
- *
951
- * The caller must have set up the argv array already, except for argv[2]
952
- * which will be filled with the name of the temp variable file.
953
- *
954
- * Returns the child process PID, or -1 on fork failure (a suitable error
955
- * message has been logged on failure).
956
- *
957
- * All uses of this routine will dispatch to SubPostmasterMain in the
958
- * child process.
959
- */
960
- pid_t
961
- postmaster_forkexec(int argc, char *argv[])
962
- {
963
- Port port;
964
-
965
- /* This entry point passes dummy values for the Port variables */
966
- memset(&port, 0, sizeof(port));
967
- return internal_forkexec(argc, argv, &port);
968
- }
969
-
970
- /*
971
- * backend_forkexec -- fork/exec off a backend process
972
- *
973
- * Some operating systems (WIN32) don't have fork() so we have to simulate
974
- * it by storing parameters that need to be passed to the child and
975
- * then create a new child process.
976
- *
977
- * returns the pid of the fork/exec'd process, or -1 on failure
978
- */
979
- static pid_t
980
- backend_forkexec(Port *port)
981
- {
982
- char *av[4];
983
- int ac = 0;
984
-
985
- av[ac++] = "postgres";
986
- av[ac++] = "--forkbackend";
987
- av[ac++] = NULL; /* filled in by internal_forkexec */
988
-
989
- av[ac] = NULL;
990
- Assert(ac < lengthof(av));
991
-
992
- return internal_forkexec(ac, av, port);
993
- }
994
-
995
- #ifndef WIN32
996
-
997
- /*
998
- * internal_forkexec non-win32 implementation
999
- *
1000
- * - writes out backend variables to the parameter file
1001
- * - fork():s, and then exec():s the child process
1002
- */
1003
- static pid_t
1004
- internal_forkexec(int argc, char *argv[], Port *port)
1005
- {
1006
- static unsigned long tmpBackendFileNum = 0;
1007
- pid_t pid;
1008
- char tmpfilename[MAXPGPATH];
1009
- BackendParameters param;
1010
- FILE *fp;
1011
-
1012
- if (!save_backend_variables(&param, port))
1013
- return -1; /* log made by save_backend_variables */
1014
-
1015
- /* Calculate name for temp file */
1016
- snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
1017
- PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
1018
- MyProcPid, ++tmpBackendFileNum);
1019
-
1020
- /* Open file */
1021
- fp = AllocateFile(tmpfilename, PG_BINARY_W);
1022
- if (!fp)
1023
- {
1024
- /*
1025
- * As in OpenTemporaryFileInTablespace, try to make the temp-file
1026
- * directory, ignoring errors.
1027
- */
1028
- (void) MakePGDirectory(PG_TEMP_FILES_DIR);
1029
-
1030
- fp = AllocateFile(tmpfilename, PG_BINARY_W);
1031
- if (!fp)
1032
- {
1033
- ereport(LOG,
1034
- (errcode_for_file_access(),
1035
- errmsg("could not create file \"%s\": %m",
1036
- tmpfilename)));
1037
- return -1;
1038
- }
1039
- }
1040
-
1041
- if (fwrite(&param, sizeof(param), 1, fp) != 1)
1042
- {
1043
- ereport(LOG,
1044
- (errcode_for_file_access(),
1045
- errmsg("could not write to file \"%s\": %m", tmpfilename)));
1046
- FreeFile(fp);
1047
- return -1;
1048
- }
1049
-
1050
- /* Release file */
1051
- if (FreeFile(fp))
1052
- {
1053
- ereport(LOG,
1054
- (errcode_for_file_access(),
1055
- errmsg("could not write to file \"%s\": %m", tmpfilename)));
1056
- return -1;
1057
- }
1058
-
1059
- /* Make sure caller set up argv properly */
1060
- Assert(argc >= 3);
1061
- Assert(argv[argc] == NULL);
1062
- Assert(strncmp(argv[1], "--fork", 6) == 0);
1063
- Assert(argv[2] == NULL);
1064
-
1065
- /* Insert temp file name after --fork argument */
1066
- argv[2] = tmpfilename;
1067
-
1068
- /* Fire off execv in child */
1069
- if ((pid = fork_process()) == 0)
1070
- {
1071
- if (execv(postgres_exec_path, argv) < 0)
1072
- {
1073
- ereport(LOG,
1074
- (errmsg("could not execute server process \"%s\": %m",
1075
- postgres_exec_path)));
1076
- /* We're already in the child process here, can't return */
1077
- exit(1);
1078
- }
1079
- }
1080
-
1081
- return pid; /* Parent returns pid, or -1 on fork failure */
1082
- }
1083
- #else /* WIN32 */
1084
-
1085
- /*
1086
- * internal_forkexec win32 implementation
1087
- *
1088
- * - starts backend using CreateProcess(), in suspended state
1089
- * - writes out backend variables to the parameter file
1090
- * - during this, duplicates handles and sockets required for
1091
- * inheritance into the new process
1092
- * - resumes execution of the new process once the backend parameter
1093
- * file is complete.
1094
- */
1095
- static pid_t
1096
- internal_forkexec(int argc, char *argv[], Port *port)
1097
- {
1098
- int retry_count = 0;
1099
- STARTUPINFO si;
1100
- PROCESS_INFORMATION pi;
1101
- int i;
1102
- int j;
1103
- char cmdLine[MAXPGPATH * 2];
1104
- HANDLE paramHandle;
1105
- BackendParameters *param;
1106
- SECURITY_ATTRIBUTES sa;
1107
- char paramHandleStr[32];
1108
- win32_deadchild_waitinfo *childinfo;
1109
-
1110
- /* Make sure caller set up argv properly */
1111
- Assert(argc >= 3);
1112
- Assert(argv[argc] == NULL);
1113
- Assert(strncmp(argv[1], "--fork", 6) == 0);
1114
- Assert(argv[2] == NULL);
1115
-
1116
- /* Resume here if we need to retry */
1117
- retry:
1118
-
1119
- /* Set up shared memory for parameter passing */
1120
- ZeroMemory(&sa, sizeof(sa));
1121
- sa.nLength = sizeof(sa);
1122
- sa.bInheritHandle = TRUE;
1123
- paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
1124
- &sa,
1125
- PAGE_READWRITE,
1126
- 0,
1127
- sizeof(BackendParameters),
1128
- NULL);
1129
- if (paramHandle == INVALID_HANDLE_VALUE)
1130
- {
1131
- ereport(LOG,
1132
- (errmsg("could not create backend parameter file mapping: error code %lu",
1133
- GetLastError())));
1134
- return -1;
1135
- }
1136
-
1137
- param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
1138
- if (!param)
1139
- {
1140
- ereport(LOG,
1141
- (errmsg("could not map backend parameter memory: error code %lu",
1142
- GetLastError())));
1143
- CloseHandle(paramHandle);
1144
- return -1;
1145
- }
1146
-
1147
- /* Insert temp file name after --fork argument */
1148
- #ifdef _WIN64
1149
- sprintf(paramHandleStr, "%llu", (LONG_PTR) paramHandle);
1150
- #else
1151
- sprintf(paramHandleStr, "%lu", (DWORD) paramHandle);
1152
- #endif
1153
- argv[2] = paramHandleStr;
1154
-
1155
- /* Format the cmd line */
1156
- cmdLine[sizeof(cmdLine) - 1] = '\0';
1157
- cmdLine[sizeof(cmdLine) - 2] = '\0';
1158
- snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
1159
- i = 0;
1160
- while (argv[++i] != NULL)
1161
- {
1162
- j = strlen(cmdLine);
1163
- snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
1164
- }
1165
- if (cmdLine[sizeof(cmdLine) - 2] != '\0')
1166
- {
1167
- ereport(LOG,
1168
- (errmsg("subprocess command line too long")));
1169
- UnmapViewOfFile(param);
1170
- CloseHandle(paramHandle);
1171
- return -1;
1172
- }
1173
-
1174
- memset(&pi, 0, sizeof(pi));
1175
- memset(&si, 0, sizeof(si));
1176
- si.cb = sizeof(si);
1177
-
1178
- /*
1179
- * Create the subprocess in a suspended state. This will be resumed later,
1180
- * once we have written out the parameter file.
1181
- */
1182
- if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
1183
- NULL, NULL, &si, &pi))
1184
- {
1185
- ereport(LOG,
1186
- (errmsg("CreateProcess() call failed: %m (error code %lu)",
1187
- GetLastError())));
1188
- UnmapViewOfFile(param);
1189
- CloseHandle(paramHandle);
1190
- return -1;
1191
- }
1192
-
1193
- if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
1194
- {
1195
- /*
1196
- * log made by save_backend_variables, but we have to clean up the
1197
- * mess with the half-started process
1198
- */
1199
- if (!TerminateProcess(pi.hProcess, 255))
1200
- ereport(LOG,
1201
- (errmsg_internal("could not terminate unstarted process: error code %lu",
1202
- GetLastError())));
1203
- CloseHandle(pi.hProcess);
1204
- CloseHandle(pi.hThread);
1205
- UnmapViewOfFile(param);
1206
- CloseHandle(paramHandle);
1207
- return -1; /* log made by save_backend_variables */
1208
- }
1209
-
1210
- /* Drop the parameter shared memory that is now inherited to the backend */
1211
- if (!UnmapViewOfFile(param))
1212
- ereport(LOG,
1213
- (errmsg("could not unmap view of backend parameter file: error code %lu",
1214
- GetLastError())));
1215
- if (!CloseHandle(paramHandle))
1216
- ereport(LOG,
1217
- (errmsg("could not close handle to backend parameter file: error code %lu",
1218
- GetLastError())));
1219
-
1220
- /*
1221
- * Reserve the memory region used by our main shared memory segment before
1222
- * we resume the child process. Normally this should succeed, but if ASLR
1223
- * is active then it might sometimes fail due to the stack or heap having
1224
- * gotten mapped into that range. In that case, just terminate the
1225
- * process and retry.
1226
- */
1227
- if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
1228
- {
1229
- /* pgwin32_ReserveSharedMemoryRegion already made a log entry */
1230
- if (!TerminateProcess(pi.hProcess, 255))
1231
- ereport(LOG,
1232
- (errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
1233
- GetLastError())));
1234
- CloseHandle(pi.hProcess);
1235
- CloseHandle(pi.hThread);
1236
- if (++retry_count < 100)
1237
- goto retry;
1238
- ereport(LOG,
1239
- (errmsg("giving up after too many tries to reserve shared memory"),
1240
- errhint("This might be caused by ASLR or antivirus software.")));
1241
- return -1;
1242
- }
1243
-
1244
- /*
1245
- * Now that the backend variables are written out, we start the child
1246
- * thread so it can start initializing while we set up the rest of the
1247
- * parent state.
1248
- */
1249
- if (ResumeThread(pi.hThread) == -1)
1250
- {
1251
- if (!TerminateProcess(pi.hProcess, 255))
1252
- {
1253
- ereport(LOG,
1254
- (errmsg_internal("could not terminate unstartable process: error code %lu",
1255
- GetLastError())));
1256
- CloseHandle(pi.hProcess);
1257
- CloseHandle(pi.hThread);
1258
- return -1;
1259
- }
1260
- CloseHandle(pi.hProcess);
1261
- CloseHandle(pi.hThread);
1262
- ereport(LOG,
1263
- (errmsg_internal("could not resume thread of unstarted process: error code %lu",
1264
- GetLastError())));
1265
- return -1;
1266
- }
1267
-
1268
- /*
1269
- * Queue a waiter to signal when this child dies. The wait will be handled
1270
- * automatically by an operating system thread pool.
1271
- *
1272
- * Note: use malloc instead of palloc, since it needs to be thread-safe.
1273
- * Struct will be free():d from the callback function that runs on a
1274
- * different thread.
1275
- */
1276
- childinfo = malloc(sizeof(win32_deadchild_waitinfo));
1277
- if (!childinfo)
1278
- ereport(FATAL,
1279
- (errcode(ERRCODE_OUT_OF_MEMORY),
1280
- errmsg("out of memory")));
1281
-
1282
- childinfo->procHandle = pi.hProcess;
1283
- childinfo->procId = pi.dwProcessId;
1284
-
1285
- if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
1286
- pi.hProcess,
1287
- pgwin32_deadchild_callback,
1288
- childinfo,
1289
- INFINITE,
1290
- WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
1291
- ereport(FATAL,
1292
- (errmsg_internal("could not register process for wait: error code %lu",
1293
- GetLastError())));
1294
-
1295
- /* Don't close pi.hProcess here - the wait thread needs access to it */
1296
-
1297
- CloseHandle(pi.hThread);
1298
-
1299
- return pi.dwProcessId;
1300
- }
1301
- #endif /* WIN32 */
1302
-
1303
-
1304
- /*
1305
- * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
1306
- * to what it would be if we'd simply forked on Unix, and then
1307
- * dispatch to the appropriate place.
1308
- *
1309
- * The first two command line arguments are expected to be "--forkFOO"
1310
- * (where FOO indicates which postmaster child we are to become), and
1311
- * the name of a variables file that we can read to load data that would
1312
- * have been inherited by fork() on Unix. Remaining arguments go to the
1313
- * subprocess FooMain() routine.
1314
- */
1315
- void
1316
- SubPostmasterMain(int argc, char *argv[])
1317
- {
1318
- Port port;
1319
-
1320
- /* In EXEC_BACKEND case we will not have inherited these settings */
1321
- IsPostmasterEnvironment = true;
1322
- whereToSendOutput = DestNone;
1323
-
1324
- /* Setup essential subsystems (to ensure elog() behaves sanely) */
1325
- InitializeGUCOptions();
1326
-
1327
- /* Check we got appropriate args */
1328
- if (argc < 3)
1329
- elog(FATAL, "invalid subpostmaster invocation");
1330
-
1331
- /* Read in the variables file */
1332
- memset(&port, 0, sizeof(Port));
1333
- read_backend_variables(argv[2], &port);
1334
-
1335
- /* Close the postmaster's sockets (as soon as we know them) */
1336
- ClosePostmasterPorts(strcmp(argv[1], "--forklog") == 0);
1337
-
1338
- /* Setup as postmaster child */
1339
- InitPostmasterChild();
1340
-
1341
- /*
1342
- * If appropriate, physically re-attach to shared memory segment. We want
1343
- * to do this before going any further to ensure that we can attach at the
1344
- * same address the postmaster used. On the other hand, if we choose not
1345
- * to re-attach, we may have other cleanup to do.
1346
- *
1347
- * If testing EXEC_BACKEND on Linux, you should run this as root before
1348
- * starting the postmaster:
1349
- *
1350
- * sysctl -w kernel.randomize_va_space=0
1351
- *
1352
- * This prevents using randomized stack and code addresses that cause the
1353
- * child process's memory map to be different from the parent's, making it
1354
- * sometimes impossible to attach to shared memory at the desired address.
1355
- * Return the setting to its old value (usually '1' or '2') when finished.
1356
- */
1357
- if (strcmp(argv[1], "--forkbackend") == 0 ||
1358
- strcmp(argv[1], "--forkavlauncher") == 0 ||
1359
- strcmp(argv[1], "--forkavworker") == 0 ||
1360
- strcmp(argv[1], "--forkaux") == 0 ||
1361
- strncmp(argv[1], "--forkbgworker=", 15) == 0)
1362
- PGSharedMemoryReAttach();
1363
- else
1364
- PGSharedMemoryNoReAttach();
1365
-
1366
- /* autovacuum needs this set before calling InitProcess */
1367
- if (strcmp(argv[1], "--forkavlauncher") == 0)
1368
- AutovacuumLauncherIAm();
1369
- if (strcmp(argv[1], "--forkavworker") == 0)
1370
- AutovacuumWorkerIAm();
1371
-
1372
- /* Read in remaining GUC variables */
1373
- read_nondefault_variables();
1374
-
1375
- /*
1376
- * Check that the data directory looks valid, which will also check the
1377
- * privileges on the data directory and update our umask and file/group
1378
- * variables for creating files later. Note: this should really be done
1379
- * before we create any files or directories.
1380
- */
1381
- checkDataDir();
1382
-
1383
- /*
1384
- * (re-)read control file, as it contains config. The postmaster will
1385
- * already have read this, but this process doesn't know about that.
1386
- */
1387
- LocalProcessControlFile(false);
1388
-
1389
- /*
1390
- * Reload any libraries that were preloaded by the postmaster. Since we
1391
- * exec'd this process, those libraries didn't come along with us; but we
1392
- * should load them into all child processes to be consistent with the
1393
- * non-EXEC_BACKEND behavior.
1394
- */
1395
- process_shared_preload_libraries();
1396
-
1397
- /* Run backend or appropriate child */
1398
- if (strcmp(argv[1], "--forkbackend") == 0)
1399
- {
1400
- Assert(argc == 3); /* shouldn't be any more args */
1401
-
1402
- /*
1403
- * Need to reinitialize the SSL library in the backend, since the
1404
- * context structures contain function pointers and cannot be passed
1405
- * through the parameter file.
1406
- *
1407
- * If for some reason reload fails (maybe the user installed broken
1408
- * key files), soldier on without SSL; that's better than all
1409
- * connections becoming impossible.
1410
- *
1411
- * XXX should we do this in all child processes? For the moment it's
1412
- * enough to do it in backend children.
1413
- */
1414
- #ifdef USE_SSL
1415
- if (EnableSSL)
1416
- {
1417
- if (secure_initialize(false) == 0)
1418
- LoadedSSL = true;
1419
- else
1420
- ereport(LOG,
1421
- (errmsg("SSL configuration could not be loaded in child process")));
1422
- }
1423
- #endif
1424
-
1425
- /*
1426
- * Perform additional initialization and collect startup packet.
1427
- *
1428
- * We want to do this before InitProcess() for a couple of reasons: 1.
1429
- * so that we aren't eating up a PGPROC slot while waiting on the
1430
- * client. 2. so that if InitProcess() fails due to being out of
1431
- * PGPROC slots, we have already initialized libpq and are able to
1432
- * report the error to the client.
1433
- */
1434
- BackendInitialize(&port);
1435
-
1436
- /* Restore basic shared memory pointers */
1437
- InitShmemAccess(UsedShmemSegAddr);
1438
-
1439
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1440
- InitProcess();
1441
-
1442
- /* Attach process to shared data structures */
1443
- CreateSharedMemoryAndSemaphores();
1444
-
1445
- /* And run the backend */
1446
- BackendRun(&port); /* does not return */
1447
- }
1448
- if (strcmp(argv[1], "--forkaux") == 0)
1449
- {
1450
- AuxProcType auxtype;
1451
-
1452
- Assert(argc == 4);
1453
-
1454
- /* Restore basic shared memory pointers */
1455
- InitShmemAccess(UsedShmemSegAddr);
1456
-
1457
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1458
- InitAuxiliaryProcess();
1459
-
1460
- /* Attach process to shared data structures */
1461
- CreateSharedMemoryAndSemaphores();
1462
-
1463
- auxtype = atoi(argv[3]);
1464
- AuxiliaryProcessMain(auxtype); /* does not return */
1465
- }
1466
- if (strcmp(argv[1], "--forkavlauncher") == 0)
1467
- {
1468
- /* Restore basic shared memory pointers */
1469
- InitShmemAccess(UsedShmemSegAddr);
1470
-
1471
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1472
- InitProcess();
1473
-
1474
- /* Attach process to shared data structures */
1475
- CreateSharedMemoryAndSemaphores();
1476
-
1477
- AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
1478
- }
1479
- if (strcmp(argv[1], "--forkavworker") == 0)
1480
- {
1481
- /* Restore basic shared memory pointers */
1482
- InitShmemAccess(UsedShmemSegAddr);
1483
-
1484
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1485
- InitProcess();
1486
-
1487
- /* Attach process to shared data structures */
1488
- CreateSharedMemoryAndSemaphores();
1489
-
1490
- AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
1491
- }
1492
- if (strncmp(argv[1], "--forkbgworker=", 15) == 0)
1493
- {
1494
- int shmem_slot;
1495
-
1496
- /* do this as early as possible; in particular, before InitProcess() */
1497
- IsBackgroundWorker = true;
1498
-
1499
- /* Restore basic shared memory pointers */
1500
- InitShmemAccess(UsedShmemSegAddr);
1501
-
1502
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1503
- InitProcess();
1504
-
1505
- /* Attach process to shared data structures */
1506
- CreateSharedMemoryAndSemaphores();
1507
-
1508
- /* Fetch MyBgworkerEntry from shared memory */
1509
- shmem_slot = atoi(argv[1] + 15);
1510
- MyBgworkerEntry = BackgroundWorkerEntry(shmem_slot);
1511
-
1512
- StartBackgroundWorker();
1513
- }
1514
- if (strcmp(argv[1], "--forklog") == 0)
1515
- {
1516
- /* Do not want to attach to shared memory */
1517
-
1518
- SysLoggerMain(argc, argv); /* does not return */
1519
- }
1520
-
1521
- abort(); /* shouldn't get here */
1522
- }
1523
- #endif /* EXEC_BACKEND */
1524
-
1525
-
1526
- /*
1527
- * ExitPostmaster -- cleanup
1528
- *
1529
- * Do NOT call exit() directly --- always go through here!
1530
- */
1531
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
1532
- #endif
1533
-
1534
- /*
1535
- * sigusr1_handler - handle signal conditions from child processes
1536
- */
1537
- #ifdef WIN32
1538
- #endif
1539
- #ifdef USE_SYSTEMD
1540
- #endif
1541
- #ifdef USE_SYSTEMD
1542
- #endif
1543
- #ifdef WIN32
1544
- #endif
1545
-
1546
- /*
1547
- * SIGTERM while processing startup packet.
1548
- *
1549
- * Running proc_exit() from a signal handler would be quite unsafe.
1550
- * However, since we have not yet touched shared memory, we can just
1551
- * pull the plug and exit without running any atexit handlers.
1552
- *
1553
- * One might be tempted to try to send a message, or log one, indicating
1554
- * why we are disconnecting. However, that would be quite unsafe in itself.
1555
- * Also, it seems undesirable to provide clues about the database's state
1556
- * to a client that has not yet completed authentication, or even sent us
1557
- * a startup packet.
1558
- */
1559
-
1560
-
1561
- /*
1562
- * Dummy signal handler
1563
- *
1564
- * We use this for signals that we don't actually use in the postmaster,
1565
- * but we do use in backends. If we were to SIG_IGN such signals in the
1566
- * postmaster, then a newly started backend might drop a signal that arrives
1567
- * before it's able to reconfigure its signal processing. (See notes in
1568
- * tcop/postgres.c.)
1569
- */
1570
-
1571
-
1572
- /*
1573
- * Timeout while processing startup packet.
1574
- * As for process_startup_packet_die(), we exit via _exit(1).
1575
- */
1576
-
1577
-
1578
-
1579
- /*
1580
- * Generate a random cancel key.
1581
- */
1582
-
1583
-
1584
- /*
1585
- * Count up number of child processes of specified types (dead_end children
1586
- * are always excluded).
1587
- */
1588
-
1589
-
1590
-
1591
- /*
1592
- * StartChildProcess -- start an auxiliary process for the postmaster
1593
- *
1594
- * "type" determines what kind of child will be started. All child types
1595
- * initially go to AuxiliaryProcessMain, which will handle common setup.
1596
- *
1597
- * Return value of StartChildProcess is subprocess' PID, or 0 if failed
1598
- * to start subprocess.
1599
- */
1600
- #ifdef EXEC_BACKEND
1601
- #else /* !EXEC_BACKEND */
1602
- #endif /* EXEC_BACKEND */
1603
-
1604
- /*
1605
- * StartAutovacuumWorker
1606
- * Start an autovac worker process.
1607
- *
1608
- * This function is here because it enters the resulting PID into the
1609
- * postmaster's private backends list.
1610
- *
1611
- * NB -- this code very roughly matches BackendStartup.
1612
- */
1613
- #ifdef EXEC_BACKEND
1614
- #endif
1615
-
1616
- /*
1617
- * MaybeStartWalReceiver
1618
- * Start the WAL receiver process, if not running and our state allows.
1619
- *
1620
- * Note: if WalReceiverPID is already nonzero, it might seem that we should
1621
- * clear WalReceiverRequested. However, there's a race condition if the
1622
- * walreceiver terminates and the startup process immediately requests a new
1623
- * one: it's quite possible to get the signal for the request before reaping
1624
- * the dead walreceiver process. Better to risk launching an extra
1625
- * walreceiver than to miss launching one we need. (The walreceiver code
1626
- * has logic to recognize that it should go away if not needed.)
1627
- */
1628
-
1629
-
1630
-
1631
- /*
1632
- * Create the opts file
1633
- */
1634
- #define OPTS_FILE "postmaster.opts"
1635
-
1636
-
1637
- /*
1638
- * MaxLivePostmasterChildren
1639
- *
1640
- * This reports the number of entries needed in per-child-process arrays
1641
- * (the PMChildFlags array, and if EXEC_BACKEND the ShmemBackendArray).
1642
- * These arrays include regular backends, autovac workers, walsenders
1643
- * and background workers, but not special children nor dead_end children.
1644
- * This allows the arrays to have a fixed maximum size, to wit the same
1645
- * too-many-children limit enforced by canAcceptConnections(). The exact value
1646
- * isn't too critical as long as it's more than MaxBackends.
1647
- */
1648
-
1649
-
1650
- /*
1651
- * Connect background worker to a database.
1652
- */
1653
-
1654
-
1655
- /*
1656
- * Connect background worker to a database using OIDs.
1657
- */
1658
-
1659
-
1660
- /*
1661
- * Block/unblock signals in a background worker
1662
- */
1663
-
1664
-
1665
-
1666
-
1667
- #ifdef EXEC_BACKEND
1668
- static pid_t
1669
- bgworker_forkexec(int shmem_slot)
1670
- {
1671
- char *av[10];
1672
- int ac = 0;
1673
- char forkav[MAXPGPATH];
1674
-
1675
- snprintf(forkav, MAXPGPATH, "--forkbgworker=%d", shmem_slot);
1676
-
1677
- av[ac++] = "postgres";
1678
- av[ac++] = forkav;
1679
- av[ac++] = NULL; /* filled in by postmaster_forkexec */
1680
- av[ac] = NULL;
1681
-
1682
- Assert(ac < lengthof(av));
1683
-
1684
- return postmaster_forkexec(ac, av);
1685
- }
1686
- #endif
1687
-
1688
- /*
1689
- * Start a new bgworker.
1690
- * Starting time conditions must have been checked already.
1691
- *
1692
- * Returns true on success, false on failure.
1693
- * In either case, update the RegisteredBgWorker's state appropriately.
1694
- *
1695
- * This code is heavily based on autovacuum.c, q.v.
1696
- */
1697
- #ifdef EXEC_BACKEND
1698
- #else
1699
- #endif
1700
- #ifndef EXEC_BACKEND
1701
- #endif
1702
- #ifdef EXEC_BACKEND
1703
- #endif
1704
-
1705
- /*
1706
- * Does the current postmaster state require starting a worker with the
1707
- * specified start_time?
1708
- */
1709
-
1710
-
1711
- /*
1712
- * Allocate the Backend struct for a connected background worker, but don't
1713
- * add it to the list of backends just yet.
1714
- *
1715
- * On failure, return false without changing any worker state.
1716
- *
1717
- * Some info from the Backend is copied into the passed rw.
1718
- */
1719
-
1720
-
1721
- /*
1722
- * If the time is right, start background worker(s).
1723
- *
1724
- * As a side effect, the bgworker control variables are set or reset
1725
- * depending on whether more workers may need to be started.
1726
- *
1727
- * We limit the number of workers started per call, to avoid consuming the
1728
- * postmaster's attention for too long when many such requests are pending.
1729
- * As long as StartWorkerNeeded is true, ServerLoop will not block and will
1730
- * call this function again after dealing with any other issues.
1731
- */
1732
- #define MAX_BGWORKERS_TO_LAUNCH 100
1733
-
1734
- /*
1735
- * When a backend asks to be notified about worker state changes, we
1736
- * set a flag in its backend entry. The background worker machinery needs
1737
- * to know when such backends exit.
1738
- */
1739
-
1740
-
1741
- #ifdef EXEC_BACKEND
1742
-
1743
- /*
1744
- * The following need to be available to the save/restore_backend_variables
1745
- * functions. They are marked NON_EXEC_STATIC in their home modules.
1746
- */
1747
- extern slock_t *ShmemLock;
1748
- extern slock_t *ProcStructLock;
1749
- extern PGPROC *AuxiliaryProcs;
1750
- extern PMSignalData *PMSignalState;
1751
- extern pg_time_t first_syslogger_file_time;
1752
-
1753
- #ifndef WIN32
1754
- #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
1755
- #define read_inheritable_socket(dest, src) (*(dest) = *(src))
1756
- #else
1757
- static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
1758
- static bool write_inheritable_socket(InheritableSocket *dest, SOCKET src,
1759
- pid_t childPid);
1760
- static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
1761
- #endif
1762
-
1763
-
1764
- /* Save critical backend variables into the BackendParameters struct */
1765
- #ifndef WIN32
1766
- static bool
1767
- save_backend_variables(BackendParameters *param, Port *port)
1768
- #else
1769
- static bool
1770
- save_backend_variables(BackendParameters *param, Port *port,
1771
- HANDLE childProcess, pid_t childPid)
1772
- #endif
1773
- {
1774
- memcpy(&param->port, port, sizeof(Port));
1775
- if (!write_inheritable_socket(&param->portsocket, port->sock, childPid))
1776
- return false;
1777
-
1778
- strlcpy(param->DataDir, DataDir, MAXPGPATH);
1779
-
1780
- memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
1781
-
1782
- param->MyCancelKey = MyCancelKey;
1783
- param->MyPMChildSlot = MyPMChildSlot;
1784
-
1785
- #ifdef WIN32
1786
- param->ShmemProtectiveRegion = ShmemProtectiveRegion;
1787
- #endif
1788
- param->UsedShmemSegID = UsedShmemSegID;
1789
- param->UsedShmemSegAddr = UsedShmemSegAddr;
1790
-
1791
- param->ShmemLock = ShmemLock;
1792
- param->ShmemVariableCache = ShmemVariableCache;
1793
- param->ShmemBackendArray = ShmemBackendArray;
1794
-
1795
- #ifndef HAVE_SPINLOCKS
1796
- param->SpinlockSemaArray = SpinlockSemaArray;
1797
- #endif
1798
- param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
1799
- param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
1800
- param->MainLWLockArray = MainLWLockArray;
1801
- param->ProcStructLock = ProcStructLock;
1802
- param->ProcGlobal = ProcGlobal;
1803
- param->AuxiliaryProcs = AuxiliaryProcs;
1804
- param->PreparedXactProcs = PreparedXactProcs;
1805
- param->PMSignalState = PMSignalState;
1806
-
1807
- param->PostmasterPid = PostmasterPid;
1808
- param->PgStartTime = PgStartTime;
1809
- param->PgReloadTime = PgReloadTime;
1810
- param->first_syslogger_file_time = first_syslogger_file_time;
1811
-
1812
- param->redirection_done = redirection_done;
1813
- param->IsBinaryUpgrade = IsBinaryUpgrade;
1814
- param->query_id_enabled = query_id_enabled;
1815
- param->max_safe_fds = max_safe_fds;
1816
-
1817
- param->MaxBackends = MaxBackends;
1818
-
1819
- #ifdef WIN32
1820
- param->PostmasterHandle = PostmasterHandle;
1821
- if (!write_duplicated_handle(&param->initial_signal_pipe,
1822
- pgwin32_create_signal_listener(childPid),
1823
- childProcess))
1824
- return false;
1825
- #else
1826
- memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
1827
- sizeof(postmaster_alive_fds));
1828
- #endif
1829
-
1830
- memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
1831
-
1832
- strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
1833
-
1834
- strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
1835
-
1836
- return true;
1837
- }
1838
-
1839
-
1840
- #ifdef WIN32
1841
- /*
1842
- * Duplicate a handle for usage in a child process, and write the child
1843
- * process instance of the handle to the parameter file.
1844
- */
1845
- static bool
1846
- write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
1847
- {
1848
- HANDLE hChild = INVALID_HANDLE_VALUE;
1849
-
1850
- if (!DuplicateHandle(GetCurrentProcess(),
1851
- src,
1852
- childProcess,
1853
- &hChild,
1854
- 0,
1855
- TRUE,
1856
- DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
1857
- {
1858
- ereport(LOG,
1859
- (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
1860
- GetLastError())));
1861
- return false;
1862
- }
1863
-
1864
- *dest = hChild;
1865
- return true;
1866
- }
1867
-
1868
- /*
1869
- * Duplicate a socket for usage in a child process, and write the resulting
1870
- * structure to the parameter file.
1871
- * This is required because a number of LSPs (Layered Service Providers) very
1872
- * common on Windows (antivirus, firewalls, download managers etc) break
1873
- * straight socket inheritance.
1874
- */
1875
- static bool
1876
- write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
1877
- {
1878
- dest->origsocket = src;
1879
- if (src != 0 && src != PGINVALID_SOCKET)
1880
- {
1881
- /* Actual socket */
1882
- if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
1883
- {
1884
- ereport(LOG,
1885
- (errmsg("could not duplicate socket %d for use in backend: error code %d",
1886
- (int) src, WSAGetLastError())));
1887
- return false;
1888
- }
1889
- }
1890
- return true;
1891
- }
1892
-
1893
- /*
1894
- * Read a duplicate socket structure back, and get the socket descriptor.
1895
- */
1896
- static void
1897
- read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
1898
- {
1899
- SOCKET s;
1900
-
1901
- if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
1902
- {
1903
- /* Not a real socket! */
1904
- *dest = src->origsocket;
1905
- }
1906
- else
1907
- {
1908
- /* Actual socket, so create from structure */
1909
- s = WSASocket(FROM_PROTOCOL_INFO,
1910
- FROM_PROTOCOL_INFO,
1911
- FROM_PROTOCOL_INFO,
1912
- &src->wsainfo,
1913
- 0,
1914
- 0);
1915
- if (s == INVALID_SOCKET)
1916
- {
1917
- write_stderr("could not create inherited socket: error code %d\n",
1918
- WSAGetLastError());
1919
- exit(1);
1920
- }
1921
- *dest = s;
1922
-
1923
- /*
1924
- * To make sure we don't get two references to the same socket, close
1925
- * the original one. (This would happen when inheritance actually
1926
- * works..
1927
- */
1928
- closesocket(src->origsocket);
1929
- }
1930
- }
1931
- #endif
1932
-
1933
- static void
1934
- read_backend_variables(char *id, Port *port)
1935
- {
1936
- BackendParameters param;
1937
-
1938
- #ifndef WIN32
1939
- /* Non-win32 implementation reads from file */
1940
- FILE *fp;
1941
-
1942
- /* Open file */
1943
- fp = AllocateFile(id, PG_BINARY_R);
1944
- if (!fp)
1945
- {
1946
- write_stderr("could not open backend variables file \"%s\": %s\n",
1947
- id, strerror(errno));
1948
- exit(1);
1949
- }
1950
-
1951
- if (fread(&param, sizeof(param), 1, fp) != 1)
1952
- {
1953
- write_stderr("could not read from backend variables file \"%s\": %s\n",
1954
- id, strerror(errno));
1955
- exit(1);
1956
- }
1957
-
1958
- /* Release file */
1959
- FreeFile(fp);
1960
- if (unlink(id) != 0)
1961
- {
1962
- write_stderr("could not remove file \"%s\": %s\n",
1963
- id, strerror(errno));
1964
- exit(1);
1965
- }
1966
- #else
1967
- /* Win32 version uses mapped file */
1968
- HANDLE paramHandle;
1969
- BackendParameters *paramp;
1970
-
1971
- #ifdef _WIN64
1972
- paramHandle = (HANDLE) _atoi64(id);
1973
- #else
1974
- paramHandle = (HANDLE) atol(id);
1975
- #endif
1976
- paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
1977
- if (!paramp)
1978
- {
1979
- write_stderr("could not map view of backend variables: error code %lu\n",
1980
- GetLastError());
1981
- exit(1);
1982
- }
1983
-
1984
- memcpy(&param, paramp, sizeof(BackendParameters));
1985
-
1986
- if (!UnmapViewOfFile(paramp))
1987
- {
1988
- write_stderr("could not unmap view of backend variables: error code %lu\n",
1989
- GetLastError());
1990
- exit(1);
1991
- }
1992
-
1993
- if (!CloseHandle(paramHandle))
1994
- {
1995
- write_stderr("could not close handle to backend parameter variables: error code %lu\n",
1996
- GetLastError());
1997
- exit(1);
1998
- }
1999
- #endif
2000
-
2001
- restore_backend_variables(&param, port);
2002
- }
2003
-
2004
- /* Restore critical backend variables from the BackendParameters struct */
2005
- static void
2006
- restore_backend_variables(BackendParameters *param, Port *port)
2007
- {
2008
- memcpy(port, &param->port, sizeof(Port));
2009
- read_inheritable_socket(&port->sock, &param->portsocket);
2010
-
2011
- SetDataDir(param->DataDir);
2012
-
2013
- memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
2014
-
2015
- MyCancelKey = param->MyCancelKey;
2016
- MyPMChildSlot = param->MyPMChildSlot;
2017
-
2018
- #ifdef WIN32
2019
- ShmemProtectiveRegion = param->ShmemProtectiveRegion;
2020
- #endif
2021
- UsedShmemSegID = param->UsedShmemSegID;
2022
- UsedShmemSegAddr = param->UsedShmemSegAddr;
2023
-
2024
- ShmemLock = param->ShmemLock;
2025
- ShmemVariableCache = param->ShmemVariableCache;
2026
- ShmemBackendArray = param->ShmemBackendArray;
2027
-
2028
- #ifndef HAVE_SPINLOCKS
2029
- SpinlockSemaArray = param->SpinlockSemaArray;
2030
- #endif
2031
- NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
2032
- NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
2033
- MainLWLockArray = param->MainLWLockArray;
2034
- ProcStructLock = param->ProcStructLock;
2035
- ProcGlobal = param->ProcGlobal;
2036
- AuxiliaryProcs = param->AuxiliaryProcs;
2037
- PreparedXactProcs = param->PreparedXactProcs;
2038
- PMSignalState = param->PMSignalState;
2039
-
2040
- PostmasterPid = param->PostmasterPid;
2041
- PgStartTime = param->PgStartTime;
2042
- PgReloadTime = param->PgReloadTime;
2043
- first_syslogger_file_time = param->first_syslogger_file_time;
2044
-
2045
- redirection_done = param->redirection_done;
2046
- IsBinaryUpgrade = param->IsBinaryUpgrade;
2047
- query_id_enabled = param->query_id_enabled;
2048
- max_safe_fds = param->max_safe_fds;
2049
-
2050
- MaxBackends = param->MaxBackends;
2051
-
2052
- #ifdef WIN32
2053
- PostmasterHandle = param->PostmasterHandle;
2054
- pgwin32_initial_signal_pipe = param->initial_signal_pipe;
2055
- #else
2056
- memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
2057
- sizeof(postmaster_alive_fds));
2058
- #endif
2059
-
2060
- memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
2061
-
2062
- strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
2063
-
2064
- strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
2065
-
2066
- /*
2067
- * We need to restore fd.c's counts of externally-opened FDs; to avoid
2068
- * confusion, be sure to do this after restoring max_safe_fds. (Note:
2069
- * BackendInitialize will handle this for port->sock.)
2070
- */
2071
- #ifndef WIN32
2072
- if (postmaster_alive_fds[0] >= 0)
2073
- ReserveExternalFD();
2074
- if (postmaster_alive_fds[1] >= 0)
2075
- ReserveExternalFD();
2076
- #endif
2077
- }
2078
-
2079
-
2080
- Size
2081
- ShmemBackendArraySize(void)
2082
- {
2083
- return mul_size(MaxLivePostmasterChildren(), sizeof(Backend));
2084
- }
2085
-
2086
- void
2087
- ShmemBackendArrayAllocation(void)
2088
- {
2089
- Size size = ShmemBackendArraySize();
2090
-
2091
- ShmemBackendArray = (Backend *) ShmemAlloc(size);
2092
- /* Mark all slots as empty */
2093
- memset(ShmemBackendArray, 0, size);
2094
- }
2095
-
2096
- static void
2097
- ShmemBackendArrayAdd(Backend *bn)
2098
- {
2099
- /* The array slot corresponding to my PMChildSlot should be free */
2100
- int i = bn->child_slot - 1;
2101
-
2102
- Assert(ShmemBackendArray[i].pid == 0);
2103
- ShmemBackendArray[i] = *bn;
2104
- }
2105
-
2106
- static void
2107
- ShmemBackendArrayRemove(Backend *bn)
2108
- {
2109
- int i = bn->child_slot - 1;
2110
-
2111
- Assert(ShmemBackendArray[i].pid == bn->pid);
2112
- /* Mark the slot as empty */
2113
- ShmemBackendArray[i].pid = 0;
2114
- }
2115
- #endif /* EXEC_BACKEND */
2116
-
2117
-
2118
- #ifdef WIN32
2119
-
2120
- /*
2121
- * Subset implementation of waitpid() for Windows. We assume pid is -1
2122
- * (that is, check all child processes) and options is WNOHANG (don't wait).
2123
- */
2124
- static pid_t
2125
- waitpid(pid_t pid, int *exitstatus, int options)
2126
- {
2127
- DWORD dwd;
2128
- ULONG_PTR key;
2129
- OVERLAPPED *ovl;
2130
-
2131
- /*
2132
- * Check if there are any dead children. If there are, return the pid of
2133
- * the first one that died.
2134
- */
2135
- if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
2136
- {
2137
- *exitstatus = (int) key;
2138
- return dwd;
2139
- }
2140
-
2141
- return -1;
2142
- }
2143
-
2144
- /*
2145
- * Note! Code below executes on a thread pool! All operations must
2146
- * be thread safe! Note that elog() and friends must *not* be used.
2147
- */
2148
- static void WINAPI
2149
- pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
2150
- {
2151
- win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
2152
- DWORD exitcode;
2153
-
2154
- if (TimerOrWaitFired)
2155
- return; /* timeout. Should never happen, since we use
2156
- * INFINITE as timeout value. */
2157
-
2158
- /*
2159
- * Remove handle from wait - required even though it's set to wait only
2160
- * once
2161
- */
2162
- UnregisterWaitEx(childinfo->waitHandle, NULL);
2163
-
2164
- if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
2165
- {
2166
- /*
2167
- * Should never happen. Inform user and set a fixed exitcode.
2168
- */
2169
- write_stderr("could not read exit code for process\n");
2170
- exitcode = 255;
2171
- }
2172
-
2173
- if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
2174
- write_stderr("could not post child completion status\n");
2175
-
2176
- /*
2177
- * Handle is per-process, so we close it here instead of in the
2178
- * originating thread
2179
- */
2180
- CloseHandle(childinfo->procHandle);
2181
-
2182
- /*
2183
- * Free struct that was allocated before the call to
2184
- * RegisterWaitForSingleObject()
2185
- */
2186
- free(childinfo);
2187
-
2188
- /* Queue SIGCHLD signal */
2189
- pg_queue_signal(SIGCHLD);
2190
- }
2191
- #endif /* WIN32 */
2192
-
2193
- /*
2194
- * Initialize one and only handle for monitoring postmaster death.
2195
- *
2196
- * Called once in the postmaster, so that child processes can subsequently
2197
- * monitor if their parent is dead.
2198
- */
2199
- #ifndef WIN32
2200
- #else
2201
- #endif /* WIN32 */