pg_query 5.0.0 → 5.1.0

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