mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-22 17:06:29 +00:00
* Add a new exec_simple_query method This adds a new `exec_simple_query` method so we can make 'out of band' queries to servers that don't interfere with pools at all. In order to reuse startup code for making these simple queries, we need to set the stats (`Reporter`) optional, so using these simple queries wont interfere with stats. * Add auth passthough (auth_query) Adds a feature that allows setting auth passthrough for md5 auth. It adds 3 new (general and pool) config parameters: - `auth_query`: An string containing a query that will be executed on boot to obtain the hash of a given user. This query have to use a placeholder `$1`, so pgcat can replace it with the user its trying to fetch the hash from. - `auth_query_user`: The user to use for connecting to the server and executing the auth_query. - `auth_query_password`: The password to use for connecting to the server and executing the auth_query. The configuration can be done either on the general config (so pools share them) or in a per-pool basis. The behavior is, at boot time, when validating server connections, a hash is fetched per server and stored in the pool. When new server connections are created, and no cleartext password is specified, the obtained hash is used for creating them, if the hash could not be obtained for whatever reason, it retries it. When client authentication is tried, it uses cleartext passwords if specified, it not, it checks whether we have query_auth set up, if so, it tries to use the obtained hash for making client auth. If there is no hash (we could not obtain one when validating the connection), a new fetch is tried. Once we have a hash, we authenticate using it against whathever the client has sent us, if there is a failure we refetch the hash and retry auth (so password changes can be done). The idea with this 'retrial' mechanism is to make it fault tolerant, so if for whatever reason hash could not be obtained during connection validation, or the password has change, we can still connect later. * Add documentation for Auth passthrough
156 lines
5.1 KiB
Ruby
156 lines
5.1 KiB
Ruby
require 'json'
|
|
require 'ostruct'
|
|
require_relative 'pgcat_process'
|
|
require_relative 'pg_instance'
|
|
|
|
class ::Hash
|
|
def deep_merge(second)
|
|
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
|
|
self.merge(second, &merger)
|
|
end
|
|
end
|
|
|
|
module Helpers
|
|
module Pgcat
|
|
def self.three_shard_setup(pool_name, pool_size, pool_mode="transaction", lb_mode="random", log_level="info")
|
|
user = {
|
|
"password" => "sharding_user",
|
|
"pool_size" => pool_size,
|
|
"statement_timeout" => 0,
|
|
"username" => "sharding_user"
|
|
}
|
|
|
|
pgcat = PgcatProcess.new(log_level)
|
|
primary0 = PgInstance.new(5432, user["username"], user["password"], "shard0")
|
|
primary1 = PgInstance.new(7432, user["username"], user["password"], "shard1")
|
|
primary2 = PgInstance.new(8432, user["username"], user["password"], "shard2")
|
|
|
|
pgcat_cfg = pgcat.current_config
|
|
pgcat_cfg["pools"] = {
|
|
"#{pool_name}" => {
|
|
"default_role" => "any",
|
|
"pool_mode" => pool_mode,
|
|
"load_balancing_mode" => lb_mode,
|
|
"primary_reads_enabled" => true,
|
|
"query_parser_enabled" => true,
|
|
"automatic_sharding_key" => "data.id",
|
|
"sharding_function" => "pg_bigint_hash",
|
|
"shards" => {
|
|
"0" => { "database" => "shard0", "servers" => [["localhost", primary0.port.to_s, "primary"]] },
|
|
"1" => { "database" => "shard1", "servers" => [["localhost", primary1.port.to_s, "primary"]] },
|
|
"2" => { "database" => "shard2", "servers" => [["localhost", primary2.port.to_s, "primary"]] },
|
|
},
|
|
"users" => { "0" => user }
|
|
}
|
|
}
|
|
pgcat.update_config(pgcat_cfg)
|
|
|
|
pgcat.start
|
|
pgcat.wait_until_ready
|
|
|
|
OpenStruct.new.tap do |struct|
|
|
struct.pgcat = pgcat
|
|
struct.shards = [primary0, primary1, primary2]
|
|
struct.all_databases = [primary0, primary1, primary2]
|
|
end
|
|
end
|
|
|
|
def self.single_instance_setup(pool_name, pool_size, pool_mode="transaction", lb_mode="random", log_level="trace")
|
|
user = {
|
|
"password" => "sharding_user",
|
|
"pool_size" => pool_size,
|
|
"statement_timeout" => 0,
|
|
"username" => "sharding_user"
|
|
}
|
|
|
|
pgcat = PgcatProcess.new(log_level)
|
|
pgcat_cfg = pgcat.current_config
|
|
|
|
primary = PgInstance.new(5432, user["username"], user["password"], "shard0")
|
|
|
|
# Main proxy configs
|
|
pgcat_cfg["pools"] = {
|
|
"#{pool_name}" => {
|
|
"default_role" => "primary",
|
|
"pool_mode" => pool_mode,
|
|
"load_balancing_mode" => lb_mode,
|
|
"primary_reads_enabled" => false,
|
|
"query_parser_enabled" => false,
|
|
"sharding_function" => "pg_bigint_hash",
|
|
"shards" => {
|
|
"0" => {
|
|
"database" => "shard0",
|
|
"servers" => [
|
|
["localhost", primary.port.to_s, "primary"]
|
|
]
|
|
},
|
|
},
|
|
"users" => { "0" => user }
|
|
}
|
|
}
|
|
pgcat_cfg["general"]["port"] = pgcat.port
|
|
pgcat.update_config(pgcat_cfg)
|
|
pgcat.start
|
|
pgcat.wait_until_ready
|
|
|
|
OpenStruct.new.tap do |struct|
|
|
struct.pgcat = pgcat
|
|
struct.primary = primary
|
|
struct.all_databases = [primary]
|
|
end
|
|
end
|
|
|
|
def self.single_shard_setup(pool_name, pool_size, pool_mode="transaction", lb_mode="random", log_level="info")
|
|
user = {
|
|
"password" => "sharding_user",
|
|
"pool_size" => pool_size,
|
|
"statement_timeout" => 0,
|
|
"username" => "sharding_user"
|
|
}
|
|
|
|
pgcat = PgcatProcess.new(log_level)
|
|
pgcat_cfg = pgcat.current_config
|
|
|
|
primary = PgInstance.new(5432, user["username"], user["password"], "shard0")
|
|
replica0 = PgInstance.new(7432, user["username"], user["password"], "shard0")
|
|
replica1 = PgInstance.new(8432, user["username"], user["password"], "shard0")
|
|
replica2 = PgInstance.new(9432, user["username"], user["password"], "shard0")
|
|
|
|
# Main proxy configs
|
|
pgcat_cfg["pools"] = {
|
|
"#{pool_name}" => {
|
|
"default_role" => "any",
|
|
"pool_mode" => pool_mode,
|
|
"load_balancing_mode" => lb_mode,
|
|
"primary_reads_enabled" => false,
|
|
"query_parser_enabled" => false,
|
|
"sharding_function" => "pg_bigint_hash",
|
|
"shards" => {
|
|
"0" => {
|
|
"database" => "shard0",
|
|
"servers" => [
|
|
["localhost", primary.port.to_s, "primary"],
|
|
["localhost", replica0.port.to_s, "replica"],
|
|
["localhost", replica1.port.to_s, "replica"],
|
|
["localhost", replica2.port.to_s, "replica"]
|
|
]
|
|
},
|
|
},
|
|
"users" => { "0" => user }
|
|
}
|
|
}
|
|
pgcat_cfg["general"]["port"] = pgcat.port
|
|
pgcat.update_config(pgcat_cfg)
|
|
pgcat.start
|
|
pgcat.wait_until_ready
|
|
|
|
OpenStruct.new.tap do |struct|
|
|
struct.pgcat = pgcat
|
|
struct.primary = primary
|
|
struct.replicas = [replica0, replica1, replica2]
|
|
struct.all_databases = [primary, replica0, replica1, replica2]
|
|
end
|
|
end
|
|
end
|
|
end
|