Re: Re: proposal: schema variables

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: jian he <jian(dot)universality(at)gmail(dot)com>
Cc: Dmitry Dolgov <9erthalion6(at)gmail(dot)com>, Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>, Erik Rijkers <er(at)xs4all(dot)nl>, Michael Paquier <michael(at)paquier(dot)xyz>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, DUVAL REMI <REMI(dot)DUVAL(at)cheops(dot)fr>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Re: proposal: schema variables
Date: 2025-01-07 21:21:38
Message-ID: CAFj8pRDtxmQ62FfRBxdTkLwFiGJeRcgSNaRFc+whRbBG3gHb3w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-performance

Hi

út 7. 1. 2025 v 10:07 odesílatel jian he <jian(dot)universality(at)gmail(dot)com>
napsal:

> hi.
>
> some minor issues.
> 'transformMergeStmt also needs
> "qry->hasSessionVariables = pstate->p_hasSessionVariables;"
> ?
>
>
yes, done

>
> <function>acldefault</function> in doc/src/sgml/func.sgml
> Need an update for SESSION VARIABLE?
>
> Table 9.70. Access Privilege Inquiry Functions
> sorting order: has_session_variable_privilege should after
> has_server_privilege?
>
>
swapped

>
> Similar to src/test/regress/sql/event_trigger.sql,
> we can use the following query to test four functions in
> Table 9.79. Object Information and Addressing Functions
> (9.27.5. Object Information and Addressing Functions)
> for session variables.
>
> SELECT
> e.varname,
> pg_describe_object('pg_variable'::regclass, e.oid, 0) as descr,
> b.type, b.object_names, b.object_args,
> pg_identify_object(a.classid, a.objid, a.objsubid) as ident
> FROM pg_variable as e,
> LATERAL pg_identify_object_as_address('pg_variable'::regclass, e.oid, 0)
> as b,
> LATERAL pg_get_object_address(b.type, b.object_names, b.object_args) as a;
>
>
done

>
> in function transformColumnRef
> if (expr_kind_allows_session_variables(pstate->p_expr_kind))
> {
> }
> can change to
> if (!node &&
> !(relname && crerr == CRERR_NO_COLUMN) &&
> expr_kind_allows_session_variables(pstate->p_expr_kind))
> {
> }
> can reduce the function call of expr_kind_allows_session_variables,
> also not lose readability.
>

In this case, I prefer current code. It is more accented so the code is
related to places where variables are allowed.
+ following patches like GUC-session_variables_ambiguity_warning or
variable-fence-syntax~upport-and-variable-fence-usa
are less invasive and much more readable.

My opinion about it is not extra strong, and surely it can be subjective. I
think the current form is quite well readable
and the proposed change is not significantly better.

>
>
> typedef struct SVariableData says:
> /*
> * Stored value and type description can be outdated when we receive a
> * sinval message. We then have to check if the stored data are still
> * trustworthy.
> */
> bool is_valid;
>
> CREATE VARIABLE var3 AS int;
> LET var3 = 10;
> GRANT SELECT ON VARIABLE var3 TO regress_noowner;
> I don't understand why the last statement GRANT SELECT needs to call
> pg_variable_cache_callback?
> and it will invalidate the original var3.
>

Reason is simple - you did the change of pg_variable. Unfortunately, the
system of system cache invalidation is sometimes too simple and too
aggressive.
Inside pg_variable_cache_callback I cannot identify if a related row in
pg_variable was removed (by another session) or just updated.
Theoretically I can miss the information of what row was touched too.
Important note - invalidation in this case doesn't means so variable or the
value of variable should be thrown. It just means, so before any usage of
the current variable's value, we should recheck our cached data against
fresh syscache. Cache invalidation is relative common in Postgres, but
validation is very quick - just the check of varcreate_lsn

> +/*
> + * Returns a copy of the value of the session variable (in the current
> memory
> + * context). The caller is responsible for permission checks.
> + */
> +Datum
> +GetSessionVariable(Oid varid, bool *isNull, Oid *typid)
> +{
> + SVariable svar;
> +
> + svar = get_session_variable(varid);
> +
> + /*
> + * Although "svar" is freshly validated in this point, svar->is_valid can
> + * be false, if an invalidation message ws processed during the domain
> check.
> + * But the variable and all its dependencies are locked now, so we don't
> need
> + * to repeat the validation.
> + */
> + Assert(svar);
> +
> + *typid = svar->typid;
> +
> + return copy_session_variable_value(svar, isNull);
> +}
> typo, "ws processed" should be "was processed".
>

fixed

> also the Assert is not helpful? since svar is NULL,
> get_session_variable would have segfault.
> also SVariable svar; is not initialized to NULL, so generally it will
> not be NULL by default.
>

assert removed

> also the comments seem to imply that before
> copy_session_variable_value, svar->is_valid can be false.
> if svar->is_valid is false, then why do copy_session_variable_value.
>

When the flag is_valid is true, then we are sure, so the stored value is
valid. When this flag is false, then we are not sure, so this value is
valid. But inside one statement,
and if the variable was locked, then we don't need to repeat validation (if
we did it immediately before), because we are sure, so the related system
catalog should be valid, because it is locked, and then we can safely
return the value. The flag is_valid can be changed any time when we are
using catalog cache - in this case when we do domain check. So although it
can look
weird after execution get_session_variable we can return the correct value,
but the flag is_valid can be false. It doesn't mean, so the current value
is wrong. It means, so the next command
should repeat validation.

In the attached version I rewrote the check if we can or cannot to ACL
SELECT check for variable used as base node of assignment indirection. I
significantly simplified the code - it is based on fact so we know varid of
the base node variable. It should be the same session variable like
resultVariable. Then I don't need to use bitmapset of used session
variables. Just I can check if this variable is used by param that is not
basenode param.

Regards

Pavel

Attachment Content-Type Size
v20250107-0021-transactional-variables.patch text/x-patch 37.3 KB
v20250107-0018-plpgsql-implementation-for-LET-statement.patch text/x-patch 17.2 KB
v20250107-0022-pg_restore-A-variable.patch text/x-patch 2.8 KB
v20250107-0019-expression-with-session-variables-can-be-inlined.patch text/x-patch 4.2 KB
v20250107-0020-this-patch-changes-error-message-column-doesn-t-exis.patch text/x-patch 29.2 KB
v20250107-0017-allow-parallel-execution-queries-with-session-variab.patch text/x-patch 13.1 KB
v20250107-0016-allow-read-an-value-of-session-variable-directly-fro.patch text/x-patch 15.9 KB
v20250107-0015-Implementation-of-NOT-NULL-and-IMMUTABLE-clauses.patch text/x-patch 33.9 KB
v20250107-0013-Implementation-ON-TRANSACTION-END-RESET-clause.patch text/x-patch 14.6 KB
v20250107-0014-Implementation-of-DEFAULT-clause-default-expressions.patch text/x-patch 32.1 KB
v20250107-0012-implementation-of-temporary-session-variables.patch text/x-patch 40.6 KB
v20250107-0011-PREPARE-LET-support.patch text/x-patch 7.4 KB
v20250107-0010-EXPLAIN-LET-support.patch text/x-patch 8.2 KB
v20250107-0009-dynamic-check-of-usage-of-session-variable-fences.patch text/x-patch 16.3 KB
v20250107-0008-variable-fence-syntax-support-and-variable-fence-usa.patch text/x-patch 19.5 KB
v20250107-0007-GUC-session_variables_ambiguity_warning.patch text/x-patch 15.2 KB
v20250107-0006-plpgsql-tests.patch text/x-patch 16.9 KB
v20250107-0005-memory-cleaning-after-DROP-VARIABLE.patch text/x-patch 21.0 KB
v20250107-0004-DISCARD-VARIABLES.patch text/x-patch 9.6 KB
v20250107-0003-function-pg_session_variables-for-cleaning-tests.patch text/x-patch 4.3 KB
v20250107-0002-Storage-for-session-variables-and-SQL-interface.patch text/x-patch 158.1 KB
v20250107-0001-Enhancing-catalog-for-support-session-variables-and-.patch text/x-patch 169.7 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2025-01-07 21:36:37 Re: Small patch to use pqMsg_Query instead of `Q`
Previous Message Tomas Vondra 2025-01-07 21:19:36 Re: PoC: history of recent vacuum/checkpoint runs (using new hooks)

Browse pgsql-performance by date

  From Date Subject
Next Message jian he 2025-01-08 09:31:11 Re: Re: proposal: schema variables
Previous Message Jeff Janes 2025-01-07 19:30:33 Re: Aggressive vacuum